This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
sms_test.go
87 lines (73 loc) · 1.84 KB
/
sms_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
package vonage
import (
"net/http"
"testing"
"github.com/jarcoal/httpmock"
)
func TestSmsNewSMSClient(*testing.T) {
auth := CreateAuthFromKeySecret("123", "456")
NewSMSClient(auth)
}
func TestSmsSend(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("POST", "https://rest.nexmo.com/sms/json",
func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(200, `
{
"message-count": "1",
"messages": [
{
"to": "447700900000",
"message-id": "0A0000000123ABCD1",
"status": "0",
"remaining-balance": "3.14159265",
"message-price": "0.03330000",
"network": "12345",
"account-ref": "customer1234"
}
]
}
`,
)
resp.Header.Add("Content-Type", "application/json")
return resp, nil
},
)
auth := CreateAuthFromKeySecret("12345678", "456")
client := NewSMSClient(auth)
result, _, _ := client.Send("44777000777", "44777000888", "hello", SMSOpts{})
messageId := result.Messages[0].MessageId
if messageId != "0A0000000123ABCD1" {
t.Error("Test SMS not sent")
}
}
func TestSmsSendFail(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("POST", "https://rest.nexmo.com/sms/json",
func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(200, `
{
"message-count": "1",
"messages": [
{
"status": "4",
"error-text": "This is an error"
}
]
}
`,
)
resp.Header.Add("Content-Type", "application/json")
return resp, nil
},
)
auth := CreateAuthFromKeySecret("12345678", "456")
client := NewSMSClient(auth)
_, errResp, _ := client.Send("44777000777", "44777000888", "hello", SMSOpts{})
msg := errResp.Messages[0].ErrorText
if msg != "This is an error" {
t.Error("The failure failed")
}
}