Skip to content

Commit

Permalink
BaseAPI is now required to end with a version like /v3
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 committed Jul 17, 2019
1 parent 5d6d5c9 commit 1a5575e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
8 changes: 8 additions & 0 deletions httphelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import (
"net/url"
"os"
"path"
"regexp"
"strings"
)

var validURL = regexp.MustCompile(`^/v[2-4].*`)

type httpRequest struct {
URL string
Parameters map[string][]string
Expand Down Expand Up @@ -291,6 +294,11 @@ func (r *httpRequest) generateUrlWithParameters() (string, error) {
if err != nil {
return "", err
}

if !validURL.MatchString(url.Path) {
return "", errors.New(`BaseAPI must end with a /v2, /v3 or /v4; setBaseAPI("https://host/v3")`)
}

q := url.Query()
if r.Parameters != nil && len(r.Parameters) > 0 {
for name, values := range r.Parameters {
Expand Down
16 changes: 14 additions & 2 deletions mailgun_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package mailgun
package mailgun_test

import (
"context"
"net/http"
"testing"

"github.com/facebookgo/ensure"
"github.com/mailgun/mailgun-go/v3"
)

const domain = "valid-mailgun-domain"
const apiKey = "valid-mailgun-api-key"

func TestMailgun(t *testing.T) {
m := NewMailgun(domain, apiKey)
m := mailgun.NewMailgun(domain, apiKey)

ensure.DeepEqual(t, m.Domain(), domain)
ensure.DeepEqual(t, m.APIKey(), apiKey)
Expand All @@ -21,3 +23,13 @@ func TestMailgun(t *testing.T) {
m.SetClient(client)
ensure.DeepEqual(t, client, m.Client())
}

func TestInvalidBaseAPI(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase("https://localhost")

ctx := context.Background()
_, err := mg.GetDomain(ctx, "unknown.domain")
ensure.NotNil(t, err)
ensure.DeepEqual(t, err.Error(), `BaseAPI must end with a /v2, /v3 or /v4; setBaseAPI("https://host/v3")`)
}
32 changes: 16 additions & 16 deletions messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestSendMGOffline(t *testing.T) {
)
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("/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/v3/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.FormValue("from"), fromUser)
ensure.DeepEqual(t, req.FormValue("subject"), exampleSubject)
ensure.DeepEqual(t, req.FormValue("text"), exampleText)
Expand All @@ -231,7 +231,7 @@ func TestSendMGOffline(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")
ctx := context.Background()

m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser)
Expand All @@ -253,7 +253,7 @@ func TestSendMGSeparateDomain(t *testing.T) {
)
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("/%s/messages", signingDomain))
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/v3/%s/messages", signingDomain))
ensure.DeepEqual(t, req.FormValue("from"), fromUser)
ensure.DeepEqual(t, req.FormValue("subject"), exampleSubject)
ensure.DeepEqual(t, req.FormValue("text"), exampleText)
Expand All @@ -264,7 +264,7 @@ func TestSendMGSeparateDomain(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")

ctx := context.Background()
m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser)
Expand Down Expand Up @@ -295,7 +295,7 @@ func TestSendMGMessageVariables(t *testing.T) {
)
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("/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/v3/%s/messages", exampleDomain))

ensure.DeepEqual(t, req.FormValue("from"), fromUser)
ensure.DeepEqual(t, req.FormValue("subject"), exampleSubject)
Expand All @@ -310,7 +310,7 @@ func TestSendMGMessageVariables(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")

m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser)
m.AddVariable(exampleStrVarKey, exampleStrVarVal)
Expand Down Expand Up @@ -368,7 +368,7 @@ func TestSendEOFError(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")

m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser)
_, _, err := mg.Send(context.Background(), m)
Expand All @@ -386,13 +386,13 @@ func TestHasRecipient(t *testing.T) {

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("/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/v3/%s/messages", exampleDomain))
fmt.Fprint(w, `{"message":"Queued, Thank you", "id":"<[email protected]>"}`)
}))
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")

// No recipient
m := mg.NewMessage(fromUser, exampleSubject, exampleText)
Expand Down Expand Up @@ -423,7 +423,7 @@ func TestResendStored(t *testing.T) {
)
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, "/some-url")
ensure.DeepEqual(t, req.URL.Path, "/v3/some-url")
ensure.DeepEqual(t, req.FormValue("to"), toUser)

rsp := fmt.Sprintf(`{"message":"%s", "id":"%s"}`, exampleMessage, exampleID)
Expand All @@ -432,13 +432,13 @@ func TestResendStored(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")

msg, id, err := mg.ReSend(context.Background(), srv.URL+"/some-url")
msg, id, err := mg.ReSend(context.Background(), srv.URL+"/v3/some-url")
ensure.NotNil(t, err)
ensure.DeepEqual(t, err.Error(), "must provide at least one recipient")

msg, id, err = mg.ReSend(context.Background(), srv.URL+"/some-url", toUser)
msg, id, err = mg.ReSend(context.Background(), srv.URL+"/v3/some-url", toUser)
ensure.Nil(t, err)
ensure.DeepEqual(t, msg, exampleMessage)
ensure.DeepEqual(t, id, exampleID)
Expand All @@ -454,7 +454,7 @@ func TestSendTLSOptions(t *testing.T) {
)
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("/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/v3/%s/messages", exampleDomain))
ensure.DeepEqual(t, req.FormValue("from"), fromUser)
ensure.DeepEqual(t, req.FormValue("subject"), exampleSubject)
ensure.DeepEqual(t, req.FormValue("text"), exampleText)
Expand All @@ -467,7 +467,7 @@ func TestSendTLSOptions(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")
ctx := context.Background()

m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser)
Expand Down Expand Up @@ -497,7 +497,7 @@ func TestSendTemplate(t *testing.T) {
defer srv.Close()

mg := NewMailgun(exampleDomain, exampleAPIKey)
mg.SetAPIBase(srv.URL)
mg.SetAPIBase(srv.URL + "/v3")
ctx := context.Background()

m := mg.NewMessage(fromUser, exampleSubject, "", toUser)
Expand Down

0 comments on commit 1a5575e

Please sign in to comment.