@@ -23,18 +23,29 @@ var testChannels = []courier.Channel{
23
23
24
24
const (
25
25
receiveURL = "/c/mbd/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive"
26
- validReceive = `{"receiver":"18005551515","sender":"18885551515","message":"Test again","date":1690386569,"date_utc":1690418969,"reference":"1","id":"b6aae1b5dfb2427a8f7ea6a717ba31a9","message_id":"3b53c137369242138120d6b0b2122607","recipient":"18005551515","originator":"18885551515","body":"Test 3","createdDatetime":"2023-07-27T00:49:29+00:00","mms":false}`
27
- validReceiveShortCode = `{"receiver":"18005551515","sender":"18885551515","message":"Test again","date":1690386569,"date_utc":1690418969,"reference":"1","id":"b6aae1b5dfb2427a8f7ea6a717ba31a9","message_id":"3b53c137369242138120d6b0b2122607","recipient":"18005551515","originator":"18885551515","body":"Test 3","createdDatetime":"20230727004929","mms":false}`
28
- validReceiveMMS = `{"receiver":"18005551515","sender":"18885551515","message":"Test again","date":1690386569,"date_utc":1690418969,"reference":"1","id":"b6aae1b5dfb2427a8f7ea6a717ba31a9","message_id":"3b53c137369242138120d6b0b2122607","recipient":"18005551515","originator":"18885551515","mediaURLs":["https://foo.bar/image.jpg"],"createdDatetime":"2023-07-27T00:49:29+00:00","mms":true}`
26
+ validReceive = "receiver=18005551515&sender=18885551515&body=Test+again&date=1690386569&date_utc=1690418969&reference=1&id=b6aae1b5dfb2427a8f7ea6a717ba31a9&id=3b53c137369242138120d6b0b2122607&recipient=18005551515&originator=18885551515&body=Test+3&receive_datetime=2023-07-27T00%3A49%3A29%2B00%3A00&mms=false"
27
+ invalidReceive = "recipient=18005551515&&receive_datetime=2023-07-27T00%3A49%3A29%2B00%3A00"
28
+ validReceiveShortCode = "shortcode=51515&sender=18885551515&message=Test+again&date=1690386569&date_utc=1690418969&reference=1&id=b6aae1b5dfb2427a8f7ea6a717ba31a9&mid=3b53c137369242138120d6b0b2122607&recipient=18005551515&originator=18885551515&body=Test+3&receive_datetime=20230727004929&mms=false"
29
+ validReceiveMMS = "receiver=18005551515&sender=18885551515&message=Test+again&date=1690386569&date_utc=1690418969&reference=1&id=b6aae1b5dfb2427a8f7ea6a717ba31a9&message_id=3b53c137369242138120d6b0b2122607&recipient=18005551515&originator=18885551515&mediaURLs=https%3A%2F%2Ffoo.bar%2Fimage.jpg&receive_datetime=2023-07-27T00%3A49%3A29%2B00%3A00&mms=true"
29
30
statusBaseURL = "/c/mbd/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?datacoding=plain&id=b6aae1b5dfb2427a8f7ea6a717ba31a9&mccmnc=310010&messageLength=4&messagePartCount=1&ported=0&price%5Bamount%5D=0.000&price%5Bcurrency%5D=USD&recipient=18885551515&reference=26&statusDatetime=2023-07-28T17%3A57%3A12%2B00%3A00"
30
31
validSecret = "my_super_secret"
31
32
validResponse = `{"id":"efa6405d518d4c0c88cce11f7db775fb","href":"https://rest.messagebird.com/mms/efa6405d518d4c0c88cce11f7db775fb","direction":"mt","originator":"+188885551515","subject":"Great logo","body":"Hi! Please have a look at this very nice logo of this cool company.","reference":"the-customers-reference","mediaUrls":["https://www.messagebird.com/assets/images/og/messagebird.gif"],"scheduledDatetime":null,"createdDatetime":"2017-09-01T10:00:00+00:00","recipients":{"totalCount":1,"totalSentCount":1,"totalDeliveredCount":0,"totalDeliveryFailedCount":0,"items":[{"recipient":18005551515,"status":"sent","statusDatetime":"2017-09-01T10:00:00+00:00"}]}}`
32
33
invalidSecret = "bad_secret"
33
34
)
34
35
35
36
func addValidSignature (r * http.Request ) {
36
- body , _ := ReadBody (r , maxRequestBodyBytes )
37
- bodysig := calculateSignature (body )
37
+ var bodysig string
38
+ if r .Body == nil {
39
+ bodysig = calculateSignature ([]byte ("" ))
40
+ } else {
41
+ body , _ := ReadBody (r , maxRequestBodyBytes )
42
+ if len (body ) > 0 {
43
+ bodysig = calculateSignature (body )
44
+ } else {
45
+ bodysig = calculateSignature ([]byte ("" ))
46
+ }
47
+ }
48
+
38
49
urlsig := calculateSignature ([]byte ("https://localhost" + r .URL .Path ))
39
50
t := jwt .NewWithClaims (jwt .SigningMethodHS256 ,
40
51
jwt.MapClaims {
@@ -88,43 +99,43 @@ func addInvalidBodyHash(r *http.Request) {
88
99
var defaultReceiveTestCases = []IncomingTestCase {
89
100
{
90
101
Label : "Receive Valid text w Signature" ,
91
- Headers : map [string ]string {"Content-Type" : "application/json " },
102
+ Headers : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded " },
92
103
URL : receiveURL ,
93
104
Data : validReceive ,
94
105
ExpectedRespStatus : 200 ,
95
- ExpectedBodyContains : "Message Accepted " ,
106
+ ExpectedBodyContains : "OK " ,
96
107
ExpectedMsgText : Sp ("Test 3" ),
97
108
ExpectedURN : "tel:+18885551515" ,
98
109
ExpectedDate : time .Date (2023 , time .July , 27 , 00 , 49 , 29 , 0 , time .UTC ),
99
110
PrepRequest : addValidSignature ,
100
111
},
101
112
{
102
113
Label : "Receive Valid text w shortcode date" ,
103
- Headers : map [string ]string {"Content-Type" : "application/json " },
114
+ Headers : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded " },
104
115
URL : receiveURL ,
105
116
Data : validReceiveShortCode ,
106
117
ExpectedRespStatus : 200 ,
107
- ExpectedBodyContains : "Message Accepted " ,
108
- ExpectedMsgText : Sp ("Test 3 " ),
118
+ ExpectedBodyContains : "OK " ,
119
+ ExpectedMsgText : Sp ("Test again " ),
109
120
ExpectedURN : "tel:+18885551515" ,
110
121
ExpectedDate : time .Date (2023 , time .July , 27 , 00 , 49 , 29 , 0 , time .UTC ),
111
122
PrepRequest : addValidSignature ,
112
123
},
113
124
{
114
125
Label : "Receive Valid w image w Signature" ,
115
- Headers : map [string ]string {"Content-Type" : "application/json " },
126
+ Headers : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded " },
116
127
URL : receiveURL ,
117
128
Data : validReceiveMMS ,
118
129
ExpectedRespStatus : 200 ,
119
- ExpectedBodyContains : "Message Accepted " ,
130
+ ExpectedBodyContains : "OK " ,
120
131
ExpectedAttachments : []string {"https://foo.bar/image.jpg" },
121
132
ExpectedURN : "tel:+18885551515" ,
122
133
ExpectedDate : time .Date (2023 , time .July , 27 , 00 , 49 , 29 , 0 , time .UTC ),
123
134
PrepRequest : addValidSignature ,
124
135
},
125
136
{
126
137
Label : "Bad JWT Signature" ,
127
- Headers : map [string ]string {"Content-Type" : "application/json " },
138
+ Headers : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded " },
128
139
URL : receiveURL ,
129
140
Data : validReceive ,
130
141
ExpectedRespStatus : 400 ,
@@ -133,28 +144,28 @@ var defaultReceiveTestCases = []IncomingTestCase{
133
144
},
134
145
{
135
146
Label : "Missing JWT Signature Header" ,
136
- Headers : map [string ]string {"Content-Type" : "application/json " },
147
+ Headers : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded " },
137
148
URL : receiveURL ,
138
149
Data : validReceive ,
139
150
ExpectedRespStatus : 400 ,
140
151
ExpectedBodyContains : `{"message":"Error","data":[{"type":"error","error":"missing request signature"}]}` ,
141
152
},
142
153
{
143
154
Label : "Receive Valid w Signature but non-matching body hash" ,
144
- Headers : map [string ]string {"Content-Type" : "application/json " },
155
+ Headers : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded " },
145
156
URL : receiveURL ,
146
157
Data : validReceive ,
147
158
ExpectedRespStatus : 400 ,
148
159
ExpectedBodyContains : `{"message":"Error","data":[{"type":"error","error":"invalid request signature, signature doesn't match expected signature for body."}]}` ,
149
160
PrepRequest : addInvalidBodyHash ,
150
161
},
151
162
{
152
- Label : "Bad JSON " ,
153
- Headers : map [string ]string {"Content-Type" : "application/json " },
163
+ Label : "Empty Body " ,
164
+ Headers : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded " },
154
165
URL : receiveURL ,
155
- Data : "empty" ,
166
+ Data : invalidReceive ,
156
167
ExpectedRespStatus : 400 ,
157
- ExpectedBodyContains : `{"message":"Error","data":[{"type":"error","error":"unable to parse request JSON: invalid character 'e' looking for beginning of value "}]}` ,
168
+ ExpectedBodyContains : `{"message":"Error","data":[{"type":"error","error":"no text or media "}]}` ,
158
169
PrepRequest : addValidSignature ,
159
170
},
160
171
{
0 commit comments