-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhandler_test.go
170 lines (162 loc) · 5.65 KB
/
handler_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 discord
import (
"testing"
"github.com/nyaruka/courier"
. "github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/test"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/urns"
)
func TestIncoming(t *testing.T) {
RunIncomingTestCases(t, testChannels, newHandler(), testCases)
}
func BenchmarkHandler(b *testing.B) {
RunChannelBenchmarks(b, testChannels, newHandler(), testCases)
}
var testChannels = []courier.Channel{
test.NewMockChannel("bac782c2-7aeb-4389-92f5-97887744f573", "DS", "discord", "US", []string{urns.Discord.Prefix}, map[string]any{courier.ConfigSendAuthorization: "sesame", courier.ConfigSendURL: "http://example.com/discord/rp/send"}),
}
var testCases = []IncomingTestCase{
{
Label: "Receive Message",
URL: "/c/ds/bac782c2-7aeb-4389-92f5-97887744f573/receive",
Data: `from=694634743521607802&text=hello`,
ExpectedRespStatus: 200,
ExpectedMsgText: Sp("hello"),
ExpectedURN: "discord:694634743521607802",
},
{
Label: "Receive Message with attachment",
URL: "/c/ds/bac782c2-7aeb-4389-92f5-97887744f573/receive",
Data: `from=694634743521607802&text=hello&attachments=https://test.test/foo.png`,
ExpectedRespStatus: 200,
ExpectedMsgText: Sp("hello"),
ExpectedURN: "discord:694634743521607802",
ExpectedAttachments: []string{"https://test.test/foo.png"},
},
{
Label: "Invalid ID",
URL: "/c/ds/bac782c2-7aeb-4389-92f5-97887744f573/receive",
Data: `from=somebody&text=hello`,
ExpectedRespStatus: 400,
ExpectedBodyContains: "Error",
},
{
Label: "Garbage Body",
URL: "/c/ds/bac782c2-7aeb-4389-92f5-97887744f573/receive",
Data: `sdfaskdfajsdkfajsdfaksdf`,
ExpectedRespStatus: 400,
ExpectedBodyContains: "Error",
},
{
Label: "Missing Text",
URL: "/c/ds/bac782c2-7aeb-4389-92f5-97887744f573/receive",
Data: `from=694634743521607802`,
ExpectedRespStatus: 400,
ExpectedBodyContains: "Error",
},
{
Label: "Message Sent Handler",
URL: "/c/ds/bac782c2-7aeb-4389-92f5-97887744f573/sent/",
Data: `id=12345`,
ExpectedRespStatus: 200,
ExpectedBodyContains: `"status":"S"`,
ExpectedStatuses: []ExpectedStatus{{MsgID: 12345, Status: courier.MsgStatusSent}},
},
{
Label: "Message Sent Handler Garbage",
URL: "/c/ds/bac782c2-7aeb-4389-92f5-97887744f573/sent/",
Data: `nothing`,
ExpectedRespStatus: 400,
},
}
var sendTestCases = []OutgoingTestCase{
{
Label: "Simple Send",
MsgText: "Hello World",
MsgURN: "discord:694634743521607802",
MockResponses: map[string][]*httpx.MockResponse{
"http://example.com/discord/rp/send": {
httpx.NewMockResponse(200, nil, []byte(``)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Path: "/discord/rp/send",
Body: `{"id":"10","text":"Hello World","to":"694634743521607802","channel":"bac782c2-7aeb-4389-92f5-97887744f573","attachments":[],"quick_replies":[]}`,
},
},
},
{
Label: "Attachment",
MsgText: "Hello World",
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
MsgURN: "discord:694634743521607802",
MockResponses: map[string][]*httpx.MockResponse{
"http://example.com/discord/rp/send": {
httpx.NewMockResponse(200, nil, []byte(``)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Path: "/discord/rp/send",
Body: `{"id":"10","text":"Hello World","to":"694634743521607802","channel":"bac782c2-7aeb-4389-92f5-97887744f573","attachments":["https://foo.bar/image.jpg"],"quick_replies":[]}`,
},
},
},
{
Label: "Attachement and quick replies",
MsgText: "Hello World",
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
MsgQuickReplies: []courier.QuickReply{{Text: "hello"}, {Text: "world"}},
MsgURN: "discord:694634743521607802",
MockResponses: map[string][]*httpx.MockResponse{
"http://example.com/discord/rp/send": {
httpx.NewMockResponse(200, nil, []byte(``)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Path: "/discord/rp/send",
Body: `{"id":"10","text":"Hello World","to":"694634743521607802","channel":"bac782c2-7aeb-4389-92f5-97887744f573","attachments":["https://foo.bar/image.jpg"],"quick_replies":["hello","world"]}`,
},
},
},
{
Label: "Error Sending",
MsgText: "Error Sending",
MsgURN: "discord:694634743521607802",
MockResponses: map[string][]*httpx.MockResponse{
"http://example.com/discord/rp/send": {
httpx.NewMockResponse(400, nil, []byte(``)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Path: "/discord/rp/send",
Body: `{"id":"10","text":"Error Sending","to":"694634743521607802","channel":"bac782c2-7aeb-4389-92f5-97887744f573","attachments":[],"quick_replies":[]}`,
},
},
ExpectedError: courier.ErrResponseStatus,
},
{
Label: "Connection Error",
MsgText: "Error",
MsgURN: "discord:694634743521607802",
MockResponses: map[string][]*httpx.MockResponse{
"http://example.com/discord/rp/send": {
httpx.NewMockResponse(500, nil, []byte(``)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Path: "/discord/rp/send",
Body: `{"id":"10","text":"Error","to":"694634743521607802","channel":"bac782c2-7aeb-4389-92f5-97887744f573","attachments":[],"quick_replies":[]}`,
},
},
ExpectedError: courier.ErrConnectionFailed,
},
}
func TestOutgoing(t *testing.T) {
RunOutgoingTestCases(t, testChannels[0], newHandler(), sendTestCases, []string{"sesame"}, nil)
}