From 464d64ded48d8888d155effdbeacc8d1c4502eb9 Mon Sep 17 00:00:00 2001 From: Rafael Soares Date: Thu, 28 Oct 2021 18:42:42 -0300 Subject: [PATCH] refactor external channel handler to use headers config on send --- channel.go | 3 +++ handlers/external/external.go | 9 +++++--- handlers/external/external_test.go | 34 +++++++++++++++--------------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/channel.go b/channel.go index d64394cca..7a71a40d1 100644 --- a/channel.go +++ b/channel.go @@ -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 diff --git a/handlers/external/external.go b/handlers/external/external.go index c447228d2..97f3f6c2b 100644 --- a/handlers/external/external.go +++ b/handlers/external/external.go @@ -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) diff --git a/handlers/external/external_test.go b/handlers/external/external_test.go index 1c4b84685..bbed3d214 100644 --- a/handlers/external/external_test.go +++ b/handlers/external/external_test.go @@ -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", @@ -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: `{{to}}{{text}}{{from}}{{quick_replies}}`, - courier.ConfigContentType: contentXML, - courier.ConfigSendMethod: http.MethodPost, - courier.ConfigSendAuthorization: "Token ABCDEF", + "send_path": "", + "max_length": 30, + courier.ConfigSendBody: `{{to}}{{text}}{{from}}{{quick_replies}}`, + courier.ConfigContentType: contentXML, + courier.ConfigSendMethod: http.MethodPost, + courier.ConfigSendHeaders: map[string]interface{}{"Authorization": "Token ABCDEF", "foo": "bar"}, }) RunChannelSendTestCases(t, getChannel30IntLength, newHandler(), longSendTestCases, nil)