Skip to content

Commit

Permalink
Merge pull request #386 from Ilhasoft/feature/external-channel-header…
Browse files Browse the repository at this point in the history
…s-configs

Support more than one header in External Channel
  • Loading branch information
rowanseymour authored Nov 23, 2021
2 parents 7466834 + a1f5a5d commit 6123ec0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
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",
"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)

}

0 comments on commit 6123ec0

Please sign in to comment.