message_service_test.go   A
last analyzed

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 37
dl 0
loc 68
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A httpsms.TestMessagesService_SendWithError 0 19 1
A httpsms.TestMessagesService_Send 0 28 1
1
package httpsms
2
3
import (
4
	"context"
5
	"encoding/json"
6
	"net/http"
7
	"testing"
8
9
	"github.com/NdoleStudio/httpsms-go/internal/helpers"
10
	"github.com/NdoleStudio/httpsms-go/internal/stubs"
11
	"github.com/stretchr/testify/assert"
12
)
13
14
const (
15
	fromNumber     = "+18005550199"
16
	toNumber       = "+18005550100"
17
	messageContent = "This is a sample text message"
18
)
19
20
func TestMessagesService_Send(t *testing.T) {
21
	// Setup
22
	t.Parallel()
23
24
	// Arrange
25
	apiKey := "test-api-key"
26
	server := helpers.MakeTestServer(http.StatusOK, stubs.MessagesSendResponse())
27
	client := New(WithBaseURL(server.URL), WithAPIKey(apiKey))
28
29
	sendParams := &MessageSendParams{
30
		Content: messageContent,
31
		From:    fromNumber,
32
		To:      toNumber,
33
	}
34
35
	// Act
36
	message, response, err := client.Messages.Send(context.Background(), sendParams)
37
38
	// Assert
39
	assert.Nil(t, err)
40
41
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
42
43
	jsonContent, _ := json.Marshal(message)
44
	assert.JSONEq(t, string(stubs.MessagesSendResponse()), string(jsonContent))
45
46
	// Teardown
47
	server.Close()
48
}
49
50
func TestMessagesService_SendWithError(t *testing.T) {
51
	// Setup
52
	t.Parallel()
53
54
	// Arrange
55
	apiKey := "test-api-key"
56
	server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.MessagesSendErrorResponse())
57
	client := New(WithBaseURL(server.URL), WithAPIKey(apiKey))
58
59
	// Act
60
	_, response, err := client.Messages.Send(context.Background(), &MessageSendParams{})
61
62
	// Assert
63
	assert.NotNil(t, err)
64
	assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode)
65
	assert.Equal(t, string(stubs.MessagesSendErrorResponse()), string(*response.Body))
66
67
	// Teardown
68
	server.Close()
69
}
70