forked from minchao/go-mitake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_test.go
170 lines (157 loc) · 4.13 KB
/
message_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package mitake
import (
"net/http"
"reflect"
"strings"
"testing"
)
func TestMessage_ToINI(t *testing.T) {
message1 := Message{
Dstaddr: "0987654321",
Smbody: "Test",
Dlvtime: "20170101010000",
Vldtime: "20170101012300",
Response: "https://example.com/callback",
}
want1 := "dstaddr=0987654321\nsmbody=Test\ndlvtime=20170101010000\nvldtime=20170101012300\nresponse=https://example.com/callback\n"
if got := message1.ToINI(); got != want1 {
t.Errorf("Message INI is %v, want %v", got, want1)
}
message2 := Message{
Dstaddr: "0987654321",
Smbody: "Test",
}
want2 := "dstaddr=0987654321\nsmbody=Test\n"
if got := message2.ToINI(); got != want2 {
t.Errorf("Message INI is %v, want %v", got, want1)
}
}
func Test_parseMessageResponse(t *testing.T) {
body := strings.NewReader(`[0]
msgid=1010079522
statuscode=1
[1]
msgid=1010079523
statuscode=4
AccountPoint=98`)
resp, err := parseMessageResponse(body)
if err != nil {
t.Errorf("parseMessageResponse returned unexpected error: %v", err)
}
if len(resp.Results) != 2 {
t.Errorf("MessageResponse.Result len is %d, want %d", len(resp.Results), 2)
}
if resp.AccountPoint != 98 {
t.Errorf("MessageResponse.AccountPoint is %d, want %d", resp.AccountPoint, 98)
}
want := []*MessageResult{
{
Msgid: "1010079522",
Statuscode: "1",
Statusstring: StatusCode("1"),
},
{
Msgid: "1010079523",
Statuscode: "4",
Statusstring: StatusCode("4"),
},
}
if !reflect.DeepEqual(resp.Results, want) {
t.Errorf("MessageResult returned %+v, want %+v", resp.Results, want)
}
}
func Test_parseMessageStatusResponse(t *testing.T) {
body := strings.NewReader(`1010079522 1 20170101010010
1010079523 4 20170101010011`)
resp, err := parseMessageStatusResponse(body)
if err != nil {
t.Errorf("parseMessageStatusResponse returned unexpected error: %v", err)
}
if len(resp.Statuses) != 2 {
t.Errorf("MessageStatusResponse.Statuses len is %d, want %d", len(resp.Statuses), 2)
}
want := []*MessageStatus{
{
MessageResult: MessageResult{
Msgid: "1010079522",
Statuscode: "1",
Statusstring: StatusCode("1"),
},
StatusTime: "20170101010010",
},
{
MessageResult: MessageResult{
Msgid: "1010079523",
Statuscode: "4",
Statusstring: StatusCode("4"),
},
StatusTime: "20170101010011",
},
}
if !reflect.DeepEqual(resp.Statuses, want) {
t.Errorf("MessageStatus returned %+v, want %+v", resp.Statuses, want)
}
}
func Test_parseMessageCancelStatusResponse(t *testing.T) {
body := strings.NewReader(`1010079522=8
1010079523=9`)
resp, err := parseCancelMessageStatusResponse(body)
if err != nil {
t.Errorf("parseMessageStatusResponse returned unexpected error: %v", err)
}
if len(resp.Statuses) != 2 {
t.Errorf("MessageStatusResponse.Statuses len is %d, want %d", len(resp.Statuses), 2)
}
want := []*MessageStatus{
{
MessageResult: MessageResult{
Msgid: "1010079522",
Statuscode: "8",
Statusstring: StatusCode("8"),
},
},
{
MessageResult: MessageResult{
Msgid: "1010079523",
Statuscode: "9",
Statusstring: StatusCode("9"),
},
},
}
if !reflect.DeepEqual(resp.Statuses, want) {
t.Errorf("MessageStatus returned %+v, want %+v", resp.Statuses, want)
}
}
func Test_ParseMessageReceipt(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
receipt, err := ParseMessageReceipt(r)
if err != nil {
t.Errorf("ParseMessageReceipt returned unexpected error: %v", err)
return
}
want := &MessageReceipt{
Msgid: "8091234567",
Dstaddr: "09001234567",
Dlvtime: "20060810125612",
Donetime: "20060810165612",
Statuscode: "0",
Statusstring: StatusCode("0"),
Statusstr: "DELIVRD",
StatusFlag: "4",
}
if !reflect.DeepEqual(receipt, want) {
t.Errorf("Message received: %v, want %v", receipt, want)
}
})
// Simulate the mitake server response.
client.Get("/callback" +
"?msgid=8091234567" +
"&dstaddr=09001234567" +
"&dlvtime=20060810125612" +
"&donetime=20060810165612" +
"&statusstr=DELIVRD" +
"&statuscode=0" +
"&StatusFlag=4")
}