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

Support more than one header in External Channel #386

Merged
Merged
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
3 changes: 3 additions & 0 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const (

// ConfigUseNational is a constant key for channel configs
ConfigUseNational = "use_national"

// ConfigSendHeaders is a constant key for channel configs
ConfigSendHeaders = "headers"
)

// ChannelType is our typing of the two char channel types
Expand Down
8 changes: 8 additions & 0 deletions handlers/external/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStat
req.Header.Set("Authorization", authorization)
}

headers := msg.Channel().ConfigForKey(courier.ConfigSendHeaders, map[string]interface{}{}).(map[string]interface{})

if len(headers) > 0 {
for hKey, hValue := range headers {
req.Header.Set(hKey, fmt.Sprint(hValue))
}
}

rr, err := utils.MakeHTTPRequest(req)

// record our status and log
Expand Down
44 changes: 27 additions & 17 deletions handlers/external/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,11 @@ func TestSending(t *testing.T) {

var jsonChannel = courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "EX", "2020", "US",
map[string]interface{}{
"send_path": "",
courier.ConfigSendBody: `{ "to":{{to}}, "text":{{text}}, "from":{{from}}, "quick_replies":{{quick_replies}} }`,
courier.ConfigContentType: contentJSON,
courier.ConfigSendMethod: http.MethodPost,
courier.ConfigSendAuthorization: "Token ABCDEF",
"send_path": "",
courier.ConfigSendBody: `{ "to":{{to}}, "text":{{text}}, "from":{{from}}, "quick_replies":{{quick_replies}} }`,
courier.ConfigContentType: contentJSON,
courier.ConfigSendMethod: http.MethodPost,
courier.ConfigSendHeaders: map[string]interface{}{"Authorization": "Token ABCDEF", "foo": "bar"},
})

var xmlChannel = courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "EX", "2020", "US",
Expand Down Expand Up @@ -472,22 +472,22 @@ func TestSending(t *testing.T) {

var jsonChannel30IntLength = courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "EX", "2020", "US",
map[string]interface{}{
"send_path": "",
"max_length": 30,
courier.ConfigSendBody: `{ "to":{{to}}, "text":{{text}}, "from":{{from}}, "quick_replies":{{quick_replies}} }`,
courier.ConfigContentType: contentJSON,
courier.ConfigSendMethod: http.MethodPost,
courier.ConfigSendAuthorization: "Token ABCDEF",
"send_path": "",
"max_length": 30,
courier.ConfigSendBody: `{ "to":{{to}}, "text":{{text}}, "from":{{from}}, "quick_replies":{{quick_replies}} }`,
courier.ConfigContentType: contentJSON,
courier.ConfigSendMethod: http.MethodPost,
courier.ConfigSendHeaders: map[string]interface{}{"Authorization": "Token ABCDEF", "foo": "bar"},
})

var xmlChannel30IntLength = courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "EX", "2020", "US",
map[string]interface{}{
"send_path": "",
"max_length": 30,
courier.ConfigSendBody: `<msg><to>{{to}}</to><text>{{text}}</text><from>{{from}}</from><quick_replies>{{quick_replies}}</quick_replies></msg>`,
courier.ConfigContentType: contentXML,
courier.ConfigSendMethod: http.MethodPost,
courier.ConfigSendAuthorization: "Token ABCDEF",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should make sure one of these tests is still using the old send_authorization key

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcbalmeida can you just make sure one of these is using the old config key and then we'll get this merged

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean tweak one of these tests so we know it still works with the old config, until that migration is run

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a commit just now with this tweak to ensure compatibility

"send_path": "",
"max_length": 30,
courier.ConfigSendBody: `<msg><to>{{to}}</to><text>{{text}}</text><from>{{from}}</from><quick_replies>{{quick_replies}}</quick_replies></msg>`,
courier.ConfigContentType: contentXML,
courier.ConfigSendMethod: http.MethodPost,
courier.ConfigSendHeaders: map[string]interface{}{"Authorization": "Token ABCDEF", "foo": "bar"},
})

RunChannelSendTestCases(t, getChannel30IntLength, newHandler(), longSendTestCases, nil)
Expand All @@ -503,4 +503,14 @@ func TestSending(t *testing.T) {

RunChannelSendTestCases(t, nationalChannel, newHandler(), nationalGetSendTestCases, nil)

var jsonChannelWithSendAuthorization = courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "EX", "2020", "US",
map[string]interface{}{
"send_path": "",
courier.ConfigSendBody: `{ "to":{{to}}, "text":{{text}}, "from":{{from}}, "quick_replies":{{quick_replies}} }`,
courier.ConfigContentType: contentJSON,
courier.ConfigSendMethod: http.MethodPost,
courier.ConfigSendAuthorization: "Token ABCDEF",
})
RunChannelSendTestCases(t, jsonChannelWithSendAuthorization, newHandler(), jsonSendTestCases, nil)

}