Skip to content

Commit

Permalink
Merge pull request #81 from Ilhasoft/feature/external-channel-headers…
Browse files Browse the repository at this point in the history
…-config

refactor external channel handler to use  headers config on send
  • Loading branch information
rasoro authored Nov 9, 2021
2 parents 8d70f22 + 464d64d commit 258b740
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 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
9 changes: 6 additions & 3 deletions handlers/external/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,12 @@ func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStat
}
req.Header.Set("Content-Type", contentTypeHeader)

authorization := msg.Channel().StringConfigForKey(courier.ConfigSendAuthorization, "")
if authorization != "" {
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)
Expand Down
34 changes: 17 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 Down

0 comments on commit 258b740

Please sign in to comment.