Skip to content

Commit

Permalink
Merge pull request rapidpro#310 from nyaruka/fix-HX
Browse files Browse the repository at this point in the history
HX channel sends MO using ISO 8859-1 encoding
  • Loading branch information
nicpottier authored Jun 29, 2020
2 parents b8346fd + 56533b2 commit e981fb6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 8 additions & 1 deletion handlers/highconnection/highconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package highconnection
import (
"context"
"fmt"
"mime"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -66,8 +67,14 @@ func (h *handler) receiveMessage(ctx context.Context, channel courier.Channel, w
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}

// Hign connection URL encodes escapes ISO 8859 escape sequences
text, _ := url.QueryUnescape(form.Message)
// decode from ISO 8859
text = mime.BEncoding.Encode("ISO-8859-1", text)
text, _ = new(mime.WordDecoder).DecodeHeader(text)

// build our Message
msg := h.Backend().NewIncomingMsg(channel, urn, form.Message).WithReceivedOn(date.UTC())
msg := h.Backend().NewIncomingMsg(channel, urn, utils.CleanString(text)).WithReceivedOn(date.UTC())

// and finally write our message
return handlers.WriteMsgsAndResponse(ctx, h, []courier.Msg{msg}, w, r)
Expand Down
5 changes: 5 additions & 0 deletions handlers/highconnection/highconnection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
statusURL = "/c/hx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status/"

validReceive = "FROM=+33610346460&TO=5151&MESSAGE=Hello+World&RECEPTION_DATE=2015-04-02T14%3A26%3A06"
validAccentReceive = "FROM=+33610346460&TO=5151&MESSAGE=je+suis+tr%E8s+satisfait+&RECEPTION_DATE=2015-04-02T14%3A26%3A06"
invalidURN = "FROM=MTN&TO=5151&MESSAGE=Hello+World&RECEPTION_DATE=2015-04-02T14%3A26%3A06"
invalidDateReceive = "FROM=+33610346460&TO=5151&MESSAGE=Hello+World&RECEPTION_DATE=2015-04-02T14:26"
validStatus = statusURL + "?ret_id=12345&status=6"
Expand All @@ -27,6 +28,10 @@ var testCases = []ChannelHandleTestCase{
{Label: "Receive Valid Message", URL: receiveURL, Data: validReceive, Status: 200, Response: "Accepted",
Text: Sp("Hello World"), URN: Sp("tel:+33610346460"),
Date: Tp(time.Date(2015, 04, 02, 14, 26, 06, 0, time.UTC))},
{Label: "Receive Valid Message with accents", URL: receiveURL, Data: validAccentReceive, Status: 200, Response: "Accepted",
Text: Sp("je suis très satisfait "), URN: Sp("tel:+33610346460"),
Date: Tp(time.Date(2015, 04, 02, 14, 26, 06, 0, time.UTC))},

{Label: "Invalid URN", URL: receiveURL, Data: invalidURN, Status: 400, Response: "phone number supplied is not a number"},
{Label: "Receive Missing Params", URL: receiveURL, Data: " ", Status: 400, Response: "validation for 'From' failed"},
{Label: "Receive Invalid Date", URL: receiveURL, Data: invalidDateReceive, Status: 400, Response: "cannot parse"},
Expand Down

0 comments on commit e981fb6

Please sign in to comment.