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