Skip to content

Commit

Permalink
Add a few tests, handle POST
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier committed Dec 6, 2017
1 parent 64044c5 commit 1d3b214
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion handlers/kannel/kannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (h *handler) SendMsg(msg courier.Msg) (courier.MsgStatus, error) {
}

callbackDomain := msg.Channel().CallbackDomain(h.Server().Config().Domain)
dlrURL := fmt.Sprintf("https://%s%s%s/status?id=%s&status=%%d", callbackDomain, "/c/kn/", msg.Channel().UUID(), msg.ID().String())
dlrURL := fmt.Sprintf("https://%s/c/kn/%s/status?id=%s&status=%%d", callbackDomain, msg.Channel().UUID(), msg.ID().String())

// build our request
form := url.Values{
Expand Down
18 changes: 7 additions & 11 deletions handlers/nexmo/nexmo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const configNexmoAppPrivateKey = "nexmo_app_private_key"

var maxMsgLength = 1600
var sendURL = "https://rest.nexmo.com/sms/json"
var throttledRE = regexp.MustCompile(`.*Throughput Rate Exceeded - please wait \[ (\d+) \] and retry.*`)

func init() {
courier.RegisterHandler(NewHandler())
Expand All @@ -52,6 +53,10 @@ func (h *handler) Initialize(s courier.Server) error {
if err != nil {
return err
}
err = s.AddHandlerRoute(h, "POST", "receive", h.ReceiveMessage)
if err != nil {
return err
}
return s.AddHandlerRoute(h, "GET", "status", h.StatusMessage)
}

Expand All @@ -76,11 +81,6 @@ func (h *handler) StatusMessage(channel courier.Channel, w http.ResponseWriter,
nexmoDeliveryReport := &nexmoDeliveryReport{}
handlers.DecodeAndValidateQueryParams(nexmoDeliveryReport, r)

// if this is a post, also try to parse the form body
if r.Method == http.MethodPost {
handlers.DecodeAndValidateForm(nexmoDeliveryReport, r)
}

if nexmoDeliveryReport.MessageID == "" {
return nil, courier.WriteIgnored(w, r, "no messageId parameter, ignored")
}
Expand Down Expand Up @@ -173,18 +173,14 @@ func (h *handler) SendMsg(msg courier.Msg) (courier.MsgStatus, error) {
"type": []string{textType},
}

encodedForm := form.Encode()
partSendURL := fmt.Sprintf("%s?%s", sendURL, encodedForm)

req, err := http.NewRequest(http.MethodGet, partSendURL, nil)
req, err := http.NewRequest(http.MethodPost, sendURL, strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
re := regexp.MustCompile(`.*Throughput Rate Exceeded - please wait \[ (\d+) \] and retry.*`)

rr := &utils.RequestResponse{}
var requestErr error
for i := 0; i < 3; i++ {
rr, requestErr = utils.MakeHTTPRequest(req)
matched := re.FindAllStringSubmatch(string([]byte(rr.Body)), -1)
matched := throttledRE.FindAllStringSubmatch(string([]byte(rr.Body)), -1)
if len(matched) > 0 && len(matched[0]) > 0 {
sleepTime, _ := strconv.Atoi(matched[0][1])
time.Sleep(time.Duration(sleepTime) * time.Millisecond)
Expand Down
25 changes: 17 additions & 8 deletions handlers/nexmo/nexmo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var (
statusURL = "/c/nx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status"
receiveURL = "/c/nx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive"

receiveValidMessage = "/c/nx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive?to=2020&msisdn=2349067554729&text=Join&messageId=external1"
receiveValidMessage = "/c/nx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive?to=2020&msisdn=2349067554729&text=Join&messageId=external1"
receiveValidMessageBody = "to=2020&msisdn=2349067554729&text=Join&messageId=external1"

statusDelivered = "/c/nx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?to=2020&messageId=external1&status=delivered"
statusExpired = "/c/nx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?to=2020&messageId=external1&status=expired"
Expand All @@ -29,6 +30,8 @@ var (
var testCases = []ChannelHandleTestCase{
{Label: "Valid Receive", URL: receiveValidMessage, Status: 200, Response: "Accepted",
Text: Sp("Join"), URN: Sp("tel:+2349067554729")},
{Label: "Valid Receive Post", URL: receiveURL, Status: 200, Response: "Accepted", Data: receiveValidMessageBody,
Text: Sp("Join"), URN: Sp("tel:+2349067554729")},
{Label: "Receive URL check", URL: receiveURL, Status: 200, Response: "no to parameter, ignored"},
{Label: "Status URL check", URL: statusURL, Status: 200, Response: "no messageId parameter, ignored"},

Expand Down Expand Up @@ -57,44 +60,50 @@ var defaultSendTestCases = []ChannelSendTestCase{
{Label: "Plain Send",
Text: "Simple Message", URN: "tel:+250788383383",
Status: "W", ExternalID: "1002",
URLParams: map[string]string{"text": "Simple Message", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
PostParams: map[string]string{"text": "Simple Message", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
ResponseBody: `{"messages":[{"status":"0","message-id":"1002"}]}`, ResponseStatus: 200,
SendPrep: setSendURL},
{Label: "Unicode Send",
Text: "Unicode ☺", URN: "tel:+250788383383",
Status: "W", ExternalID: "1002",
URLParams: map[string]string{"text": "Unicode ☺", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "unicode"},
PostParams: map[string]string{"text": "Unicode ☺", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "unicode"},
ResponseBody: `{"messages":[{"status":"0","message-id":"1002"}]}`, ResponseStatus: 200,
SendPrep: setSendURL},
{Label: "Long Send",
Text: "This is a longer message than 160 characters and will cause us to split it into two separate parts, isn't that right but it is even longer than before I say, I need to keep adding more things to make it work",
URN: "tel:+250788383383",
Status: "W", ExternalID: "1002",
URLParams: map[string]string{"text": "I need to keep adding more things to make it work", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
PostParams: map[string]string{"text": "I need to keep adding more things to make it work", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
ResponseBody: `{"messages":[{"status":"0","message-id":"1002"}]}`, ResponseStatus: 200,
SendPrep: setSendURL},
{Label: "Send Attachment",
Text: "My pic!", URN: "tel:+250788383383", Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
Status: "W", ExternalID: "1002",
URLParams: map[string]string{"text": "My pic!\nhttps://foo.bar/image.jpg", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
PostParams: map[string]string{"text": "My pic!\nhttps://foo.bar/image.jpg", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
ResponseBody: `{"messages":[{"status":"0","message-id":"1002"}]}`, ResponseStatus: 200,
SendPrep: setSendURL},
{Label: "Error Status",
Text: "Error status", URN: "tel:+250788383383",
Status: "E",
PostParams: map[string]string{"text": "Error status", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
ResponseBody: `{"messages":[{"status":"10"}]}`, ResponseStatus: 200,
SendPrep: setSendURL},
{Label: "Error Sending",
Text: "Error Message", URN: "tel:+250788383383",
Status: "E",
URLParams: map[string]string{"text": "Error Message", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
PostParams: map[string]string{"text": "Error Message", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
ResponseBody: `Error`, ResponseStatus: 400,
SendPrep: setSendURL},
{Label: "Invalid Token",
Text: "Simple Message", URN: "tel:+250788383383",
Status: "E",
URLParams: map[string]string{"text": "Simple Message", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
PostParams: map[string]string{"text": "Simple Message", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
ResponseBody: "Invalid API token", ResponseStatus: 401,
SendPrep: setSendURL},
{Label: "Throttled by Nexmo",
Text: "Simple Message", URN: "tel:+250788383383",
Status: "E",
URLParams: map[string]string{"text": "Simple Message", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
PostParams: map[string]string{"text": "Simple Message", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
ResponseBody: `{"messages":[{"status":"1","error-text":"Throughput Rate Exceeded - please wait [ 250 ] and retry"}]}`, ResponseStatus: 200,
SendPrep: setSendURL},
}
Expand Down

0 comments on commit 1d3b214

Please sign in to comment.