Skip to content

Commit

Permalink
Added tests for AddOverrideHeader()
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 committed May 27, 2022
1 parent b63d482 commit d25e820
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion httphelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"io/ioutil"
"mime/multipart"
Expand All @@ -15,6 +14,8 @@ import (
"path"
"regexp"
"strings"

"github.com/pkg/errors"
)

var validURL = regexp.MustCompile(`/v[2-4].*`)
Expand Down Expand Up @@ -274,6 +275,11 @@ func (r *httpRequest) NewRequest(ctx context.Context, method string, payload pay
}

for header, value := range r.Headers {
// Special case, override the Host header
if header == "Host" {
req.Host = value
continue
}
req.Header.Add(header, value)
}
return req, nil
Expand Down
35 changes: 35 additions & 0 deletions messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,41 @@ func TestResendStored(t *testing.T) {
ensure.DeepEqual(t, id, exampleID)
}

func TestAddOverrideHeader(t *testing.T) {
const (
exampleDomain = "testDomain"
exampleAPIKey = "testAPIKey"
toUser = "[email protected]"
exampleMessage = "Queue. Thank you"
exampleID = "<[email protected]>"
)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ensure.DeepEqual(t, req.Method, http.MethodPost)
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/v3/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.Header.Get("CustomHeader"), "custom-value")
ensure.DeepEqual(t, req.Host, "example.com")

rsp := fmt.Sprintf(`{"message":"%s", "id":"%s"}`, exampleMessage, exampleID)
fmt.Fprint(w, rsp)
}))
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL + "/v3")
mg.AddOverrideHeader("Host", "example.com")
mg.AddOverrideHeader("CustomHeader", "custom-value")
ctx := context.Background()

m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser)
m.SetRequireTLS(true)
m.SetSkipVerification(true)

msg, id, err := mg.Send(ctx, m)
ensure.Nil(t, err)
ensure.DeepEqual(t, msg, exampleMessage)
ensure.DeepEqual(t, id, exampleID)
}

func TestSendTLSOptions(t *testing.T) {
const (
exampleDomain = "testDomain"
Expand Down

0 comments on commit d25e820

Please sign in to comment.