generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms_service_test.go
135 lines (103 loc) · 3.21 KB
/
sms_service_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package smsto
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"testing"
"github.com/NdoleStudio/smsto-go/internal/helpers"
"github.com/NdoleStudio/smsto-go/internal/stubs"
"github.com/stretchr/testify/assert"
)
func TestSmsService_SendSingleRequest(t *testing.T) {
// Setup
t.Parallel()
// Arrange
requests := make([]*http.Request, 0)
apiKey := "apiKey"
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, [][]byte{stubs.SmsSendSingle()}, &requests)
client := New(WithBaseURL(server.URL), WithAPIKey(apiKey))
msg := "This is test and \\n this is a new line"
recipient := "+35799999999999"
bypassOptOut := true
senderID := "SMSto"
callbackURL := "https://example.com/callback/handler"
params := SmsSendSingleRequest{
Message: msg,
To: recipient,
BypassOptOut: &bypassOptOut,
SenderID: &senderID,
CallbackURL: &callbackURL,
}
expectedRequest := map[string]interface{}{
"message": msg,
"to": recipient,
"bypass_optout": bypassOptOut,
"sender_id": senderID,
"callback_url": callbackURL,
}
// Act
_, _, err := client.SMS.SendSingle(context.Background(), ¶ms)
// Assert
assert.Nil(t, err)
assert.Equal(t, 1, len(requests))
buf, err := ioutil.ReadAll(requests[0].Body)
assert.NoError(t, err)
assert.Equal(t, "Bearer "+apiKey, requests[0].Header.Get("Authorization"))
requestBody := map[string]interface{}{}
err = json.Unmarshal(buf, &requestBody)
assert.NoError(t, err)
assert.Equal(t, expectedRequest, requestBody)
}
func TestSmsService_SendSingleResponse(t *testing.T) {
// Setup
t.Parallel()
// Arrange
server := helpers.MakeTestServer(http.StatusOK, stubs.SmsSendSingle())
client := New(WithBaseURL(server.URL))
// Act
result, response, err := client.SMS.SendSingle(context.Background(), &SmsSendSingleRequest{})
// Assert
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
assert.Equal(t, &SmsSendSingleResponse{
Message: "Message is queued for sending! Please check report for update",
Success: true,
MessageID: "11ec-832f-a6f3fcfe-9fea-02420a0002ab",
}, result)
// Teardown
server.Close()
}
func TestSmsService_LastMessageRequest(t *testing.T) {
// Setup
t.Parallel()
// Arrange
requests := make([]*http.Request, 0)
apiKey := "apiKey"
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, [][]byte{stubs.SmsLastMessage()}, &requests)
client := New(WithBaseURL(server.URL), WithAPIKey(apiKey))
// Act
_, _, err := client.SMS.LastMessage(context.Background())
// Assert
assert.Nil(t, err)
assert.Equal(t, 1, len(requests))
assert.Equal(t, "Bearer "+apiKey, requests[0].Header.Get("Authorization"))
}
func TestSmsService_LastMessageResponse(t *testing.T) {
// Setup
t.Parallel()
// Arrange
server := helpers.MakeTestServer(http.StatusOK, stubs.SmsLastMessage())
client := New(WithBaseURL(server.URL))
// Act
result, response, err := client.SMS.LastMessage(context.Background())
// Assert
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
var smsMessage SmsMessage
err = json.Unmarshal(stubs.SmsLastMessage(), &smsMessage)
assert.Nil(t, err)
assert.Equal(t, &smsMessage, result)
// Teardown
server.Close()
}