Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for receiving MO JSON body #614

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions handlers/external/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"time"

"github.com/antchfx/xmlquery"
"github.com/buger/jsonparser"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/gsm7"
Expand All @@ -28,6 +29,8 @@
configFromXPath = "from_xpath"
configTextXPath = "text_xpath"

configMOBody = "mo_body_content_type"

configMOFromField = "mo_from_field"
configMOTextField = "mo_text_field"
configMODateField = "mo_date_field"
Expand Down Expand Up @@ -148,6 +151,8 @@
fromXPath := channel.StringConfigForKey(configFromXPath, "")
textXPath := channel.StringConfigForKey(configTextXPath, "")

moBody := channel.StringConfigForKey(configMOBody, "")

if fromXPath != "" && textXPath != "" {
// we are reading from an XML body, pull out our fields
body, err := io.ReadAll(io.LimitReader(r.Body, 100000))
Expand All @@ -168,6 +173,24 @@

from = fromNode.InnerText()
text = textNode.InnerText()
} else if moBody == "json" {
// we are reading from an JSON body, pull out our fields
body, err := io.ReadAll(io.LimitReader(r.Body, 100000))
defer r.Body.Close()
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("unable to read request body: %s", err))
}

Check warning on line 182 in handlers/external/external.go

View check run for this annotation

Codecov / codecov/patch

handlers/external/external.go#L181-L182

Added lines #L181 - L182 were not covered by tests

from, err = jsonparser.GetString(body, channel.StringConfigForKey(configMOFromField, "from"))
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("unable to find field 'from' in request body: %s", err))
}

text, err = jsonparser.GetString(body, channel.StringConfigForKey(configMOTextField, "text"))
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("unable to find field 'text' in request body: %s", err))
}

Check warning on line 192 in handlers/external/external.go

View check run for this annotation

Codecov / codecov/patch

handlers/external/external.go#L191-L192

Added lines #L191 - L192 were not covered by tests
dateString, _ = jsonparser.GetString(body, channel.StringConfigForKey(configMODateField, "date"))
} else {
// parse our form
contentType := r.Header.Get("Content-Type")
Expand Down
38 changes: 38 additions & 0 deletions handlers/external/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var handleTestCases = []ChannelHandleTestCase{
ExpectedMsgText: Sp("Join"),
ExpectedURN: "tel:+2349067554729",
},

{
Label: "Receive Valid Post multipart form",
URL: receiveURL,
Expand Down Expand Up @@ -264,11 +265,48 @@ var customTestCases = []ChannelHandleTestCase{
},
}

var MOJSONChannels = []courier.Channel{
test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "EX", "2020", "US",
map[string]interface{}{
configMOFromField: "from_number",
configMOTextField: "messageText",
configMOBody: "json",
},
),
}

var MOJSONTextCases = []ChannelHandleTestCase{
{
Label: "Receive Valid Post JSON body",
URL: receiveURL,
Data: `{"from_number":"2349067554729", "messageText": "Join"}`,
ExpectedRespStatus: 200,
ExpectedBodyContains: "Accepted",
ExpectedMsgText: Sp("Join"),
ExpectedURN: "tel:+2349067554729",
},
{
Label: "Receive missing field JSON body",
URL: receiveURL,
Data: `{"old":"2349067554729", "new": "Join"}`,
ExpectedRespStatus: 400,
ExpectedBodyContains: "unable to find field 'from' in request body",
},
{
Label: "Receive invalid Post JSON body",
URL: receiveURL,
Data: `not_valid`,
ExpectedRespStatus: 400,
ExpectedBodyContains: "unable to find field 'from' in request body",
},
}

func TestHandler(t *testing.T) {
RunChannelTestCases(t, testChannels, newHandler(), handleTestCases)
RunChannelTestCases(t, testSOAPReceiveChannels, newHandler(), handleSOAPReceiveTestCases)
RunChannelTestCases(t, gmChannels, newHandler(), gmTestCases)
RunChannelTestCases(t, customChannels, newHandler(), customTestCases)
RunChannelTestCases(t, MOJSONChannels, newHandler(), MOJSONTextCases)
}

func BenchmarkHandler(b *testing.B) {
Expand Down
Loading