@@ -2,6 +2,7 @@ package twiml
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"net/http"
6
7
"net/url"
7
8
"testing"
@@ -1051,7 +1052,25 @@ var waSendTestCases = []OutgoingTestCase{
1051
1052
},
1052
1053
},
1053
1054
ExpectedRequests : []ExpectedRequest {{
1054
- Form : url.Values {"Body" : {"Simple Message ☺" }, "To" : {"whatsapp:+250788383383" }, "From" : {"whatsapp:+12065551212" }, "StatusCallback" : {"https://localhost/c/t/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback" }},
1055
+ Form : url.Values {"Body" : {"Simple Message ☺" }, "To" : {"whatsapp:+250788383383" }, "From" : {"whatsapp:+12065551212" }, "StatusCallback" : {"https://localhost/c/sw/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback" }},
1056
+ Headers : map [string ]string {"Authorization" : "Basic YWNjb3VudFNJRDphdXRoVG9rZW4=" },
1057
+ }},
1058
+ ExpectedExtIDs : []string {"1002" },
1059
+ },
1060
+ {
1061
+ Label : "Template Send" ,
1062
+ MsgText : "templated message" ,
1063
+ MsgURN : "whatsapp:250788383383" ,
1064
+ MsgLocale : "eng" ,
1065
+ MsgMetadata : json .RawMessage (`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "external_id": "ext_id_revive_issue", "components": [{"type":"body", "params": [{"type":"text", "name": "1", "value":"Chef"}, {"type": "text" , "name": "2", "value": "tomorrow"}]}]}}` ),
1066
+ MockResponses : map [string ][]* httpx.MockResponse {
1067
+ "http://example.com/sigware_api/2010-04-01/Accounts/accountSID/Messages.json" : {
1068
+ httpx .NewMockResponse (200 , nil , []byte (`{ "sid": "1002" }` )),
1069
+ },
1070
+ },
1071
+
1072
+ ExpectedRequests : []ExpectedRequest {{
1073
+ Form : url.Values {"To" : {"whatsapp:+250788383383" }, "From" : {"whatsapp:+12065551212" }, "StatusCallback" : {"https://localhost/c/sw/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback" }, "ContentSid" : {"ext_id_revive_issue" }, "ContentVariables" : {"{\" 1\" :\" Chef\" ,\" 2\" :\" tomorrow\" }" }},
1055
1074
Headers : map [string ]string {"Authorization" : "Basic YWNjb3VudFNJRDphdXRoVG9rZW4=" },
1056
1075
}},
1057
1076
ExpectedExtIDs : []string {"1002" },
@@ -1074,6 +1093,92 @@ var twaSendTestCases = []OutgoingTestCase{
1074
1093
}},
1075
1094
ExpectedExtIDs : []string {"1002" },
1076
1095
},
1096
+ {
1097
+ Label : "Template Send" ,
1098
+ MsgText : "templated message" ,
1099
+ MsgURN : "whatsapp:250788383383" ,
1100
+ MsgLocale : "eng" ,
1101
+ MsgMetadata : json .RawMessage (`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "external_id": "ext_id_revive_issue", "components": [{"type":"body", "params": [{"type":"text", "name": "1", "value":"Chef"}, {"type": "text" , "name": "2", "value": "tomorrow"}]}]}}` ),
1102
+ MockResponses : map [string ][]* httpx.MockResponse {
1103
+ "https://api.twilio.com/2010-04-01/Accounts/accountSID/Messages.json" : {
1104
+ httpx .NewMockResponse (200 , nil , []byte (`{ "sid": "1002" }` )),
1105
+ },
1106
+ },
1107
+ ExpectedRequests : []ExpectedRequest {{
1108
+ Form : url.Values {"To" : {"whatsapp:+250788383383" }, "From" : {"whatsapp:+12065551212" }, "StatusCallback" : {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback" }, "ContentSid" : {"ext_id_revive_issue" }, "ContentVariables" : {"{\" 1\" :\" Chef\" ,\" 2\" :\" tomorrow\" }" }},
1109
+ Headers : map [string ]string {"Authorization" : "Basic YWNjb3VudFNJRDphdXRoVG9rZW4=" },
1110
+ }},
1111
+ ExpectedExtIDs : []string {"1002" },
1112
+ },
1113
+
1114
+ {
1115
+ Label : "Error Code" ,
1116
+ MsgText : "Error Code" ,
1117
+ MsgURN : "whatsapp:250788383383" ,
1118
+ MsgLocale : "eng" ,
1119
+ MsgMetadata : json .RawMessage (`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "external_id": "ext_id_revive_issue", "components": [{"type":"body", "params": [{"type":"text", "name": "1", "value":"Chef"}, {"type": "text" , "name": "2", "value": "tomorrow"}]}]}}` ),
1120
+ MockResponses : map [string ][]* httpx.MockResponse {
1121
+ "https://api.twilio.com/2010-04-01/Accounts/accountSID/Messages.json" : {
1122
+ httpx .NewMockResponse (400 , nil , []byte (`{ "code": 1001 }` )),
1123
+ },
1124
+ },
1125
+ ExpectedRequests : []ExpectedRequest {{
1126
+ Form : url.Values {"To" : {"whatsapp:+250788383383" }, "From" : {"whatsapp:+12065551212" }, "StatusCallback" : {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback" }, "ContentSid" : {"ext_id_revive_issue" }, "ContentVariables" : {"{\" 1\" :\" Chef\" ,\" 2\" :\" tomorrow\" }" }},
1127
+ Headers : map [string ]string {"Authorization" : "Basic YWNjb3VudFNJRDphdXRoVG9rZW4=" },
1128
+ }},
1129
+ ExpectedError : courier .ErrFailedWithReason ("1001" , "Service specific error: 1001." ),
1130
+ },
1131
+ {
1132
+ Label : "Stopped Contact Code" ,
1133
+ MsgText : "Stopped Contact" ,
1134
+ MsgURN : "whatsapp:250788383383" ,
1135
+ MsgLocale : "eng" ,
1136
+ MsgMetadata : json .RawMessage (`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "external_id": "ext_id_revive_issue", "components": [{"type":"body", "params": [{"type":"text", "name": "1", "value":"Chef"}, {"type": "text" , "name": "2", "value": "tomorrow"}]}]}}` ),
1137
+ MockResponses : map [string ][]* httpx.MockResponse {
1138
+ "https://api.twilio.com/2010-04-01/Accounts/accountSID/Messages.json" : {
1139
+ httpx .NewMockResponse (400 , nil , []byte (`{ "code": 21610 }` )),
1140
+ },
1141
+ },
1142
+ ExpectedRequests : []ExpectedRequest {{
1143
+ Form : url.Values {"To" : {"whatsapp:+250788383383" }, "From" : {"whatsapp:+12065551212" }, "StatusCallback" : {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback" }, "ContentSid" : {"ext_id_revive_issue" }, "ContentVariables" : {"{\" 1\" :\" Chef\" ,\" 2\" :\" tomorrow\" }" }},
1144
+ Headers : map [string ]string {"Authorization" : "Basic YWNjb3VudFNJRDphdXRoVG9rZW4=" },
1145
+ }},
1146
+ ExpectedError : courier .ErrContactStopped ,
1147
+ },
1148
+ {
1149
+ Label : "No SID" ,
1150
+ MsgText : "No SID" ,
1151
+ MsgURN : "whatsapp:250788383383" ,
1152
+ MsgLocale : "eng" ,
1153
+ MsgMetadata : json .RawMessage (`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "external_id": "ext_id_revive_issue", "components": [{"type":"body", "params": [{"type":"text", "name": "1", "value":"Chef"}, {"type": "text" , "name": "2", "value": "tomorrow"}]}]}}` ),
1154
+ MockResponses : map [string ][]* httpx.MockResponse {
1155
+ "https://api.twilio.com/2010-04-01/Accounts/accountSID/Messages.json" : {
1156
+ httpx .NewMockResponse (200 , nil , []byte (`{ }` )),
1157
+ },
1158
+ },
1159
+ ExpectedRequests : []ExpectedRequest {{
1160
+ Form : url.Values {"To" : {"whatsapp:+250788383383" }, "From" : {"whatsapp:+12065551212" }, "StatusCallback" : {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback" }, "ContentSid" : {"ext_id_revive_issue" }, "ContentVariables" : {"{\" 1\" :\" Chef\" ,\" 2\" :\" tomorrow\" }" }},
1161
+ Headers : map [string ]string {"Authorization" : "Basic YWNjb3VudFNJRDphdXRoVG9rZW4=" },
1162
+ }},
1163
+ ExpectedLogErrors : []* courier.ChannelError {courier .ErrorResponseValueMissing ("sid" )},
1164
+ },
1165
+ {
1166
+ Label : "Error Sending" ,
1167
+ MsgText : "Error Message" ,
1168
+ MsgURN : "whatsapp:250788383383" ,
1169
+ MsgLocale : "eng" ,
1170
+ MsgMetadata : json .RawMessage (`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "external_id": "ext_id_revive_issue", "components": [{"type":"body", "params": [{"type":"text", "name": "1", "value":"Chef"}, {"type": "text" , "name": "2", "value": "tomorrow"}]}]}}` ),
1171
+ MockResponses : map [string ][]* httpx.MockResponse {
1172
+ "https://api.twilio.com/2010-04-01/Accounts/accountSID/Messages.json" : {
1173
+ httpx .NewMockResponse (401 , nil , []byte (`{ "error": "out of credits" }` )),
1174
+ },
1175
+ },
1176
+ ExpectedRequests : []ExpectedRequest {{
1177
+ Form : url.Values {"To" : {"whatsapp:+250788383383" }, "From" : {"whatsapp:+12065551212" }, "StatusCallback" : {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback" }, "ContentSid" : {"ext_id_revive_issue" }, "ContentVariables" : {"{\" 1\" :\" Chef\" ,\" 2\" :\" tomorrow\" }" }},
1178
+ Headers : map [string ]string {"Authorization" : "Basic YWNjb3VudFNJRDphdXRoVG9rZW4=" },
1179
+ }},
1180
+ ExpectedError : courier .ErrResponseStatus ,
1181
+ },
1077
1182
}
1078
1183
1079
1184
func TestOutgoing (t * testing.T ) {
@@ -1117,7 +1222,7 @@ func TestOutgoing(t *testing.T) {
1117
1222
)
1118
1223
waChannel .SetScheme (urns .WhatsAppScheme )
1119
1224
1120
- RunOutgoingTestCases (t , waChannel , newTWIMLHandler ("T " , "Twilio Whatsapp " , true ), waSendTestCases , []string {httpx .BasicAuth ("accountSID" , "authToken" )}, nil )
1225
+ RunOutgoingTestCases (t , waChannel , newTWIMLHandler ("SW " , "SignalWire " , true ), waSendTestCases , []string {httpx .BasicAuth ("accountSID" , "authToken" )}, nil )
1121
1226
1122
1227
twaChannel := test .NewMockChannel ("8eb23e93-5ecb-45ba-b726-3b064e0c56ab" , "TWA" , "+12065551212" , "US" ,
1123
1228
map [string ]any {
0 commit comments