-
Notifications
You must be signed in to change notification settings - Fork 143
/
mailgun_test.go
67 lines (53 loc) · 1.55 KB
/
mailgun_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package mailgun_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/mailgun/mailgun-go/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const domain = "valid-mailgun-domain"
const apiKey = "valid-mailgun-api-key" //nolint:gosec // This is a test
func TestMailgun(t *testing.T) {
m := mailgun.NewMailgun(domain, apiKey)
assert.Equal(t, domain, m.Domain())
assert.Equal(t, apiKey, m.APIKey())
assert.Equal(t, http.DefaultClient, m.Client())
client := new(http.Client)
m.SetClient(client)
assert.Equal(t, m.Client(), client)
}
func TestInvalidBaseAPI(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase("https://localhost")
ctx := context.Background()
_, err := mg.GetDomain(ctx, "unknown.domain")
assert.EqualError(t, err, `APIBase must end with a /v1, /v2, /v3 or /v4; SetAPIBase("https://host/v3")`)
}
func TestValidBaseAPI(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var resp mailgun.DomainResponse
b, err := json.Marshal(resp)
require.NoError(t, err)
_, err = w.Write(b)
require.NoError(t, err)
}))
apiBases := []string{
fmt.Sprintf("%s/v3", testServer.URL),
fmt.Sprintf("%s/proxy/v3", testServer.URL),
}
for _, apiBase := range apiBases {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(apiBase)
ctx := context.Background()
_, err := mg.GetDomain(ctx, "unknown.domain")
require.NoError(t, err)
}
}
func ptr[T any](v T) *T {
return &v
}