-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathmailgun_test.go
More file actions
85 lines (70 loc) · 1.92 KB
/
Copy pathmailgun_test.go
File metadata and controls
85 lines (70 loc) · 1.92 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package mailgun_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/mailgun/mailgun-go/v5"
"github.com/mailgun/mailgun-go/v5/mocks"
"github.com/mailgun/mailgun-go/v5/mtypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TODO(vtopc): merge constants:
const (
domain = "valid-mailgun-domain"
testDomain = "mailgun.test"
)
// TODO(vtopc): merge constants:
const (
apiKey = "valid-mailgun-api-key" //nolint:gosec // This is a test
testKey = "api-fake-key"
)
var server *mocks.Server
// Setup and shutdown the mailgun mock server for the entire test suite
func TestMain(m *testing.M) {
server = mocks.NewServer()
defer server.Stop()
// TODO: os.Exit will exit, and `defer server.Stop()` will not run
// switch to testify suite
//nolint:gocritic // ignored till switched to testify suite
os.Exit(m.Run())
}
func TestMailgun(t *testing.T) {
m := mailgun.NewMailgun(apiKey)
assert.Equal(t, apiKey, m.APIKey())
assert.Equal(t, http.DefaultClient, m.HTTPClient())
client := new(http.Client)
m.SetHTTPClient(client)
assert.Equal(t, m.HTTPClient(), client)
}
func TestInvalidBaseAPI(t *testing.T) {
mg := mailgun.NewMailgun(testKey)
err := mg.SetAPIBase("https://localhost/v3")
assert.EqualError(t, err, `APIBase must not contain a version; SetAPIBase("https://host")`)
}
func TestValidBaseAPI(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
var resp mtypes.GetDomainResponse
b, err := json.Marshal(resp)
require.NoError(t, err)
_, err = w.Write(b)
require.NoError(t, err)
}))
apiBases := []string{
mailgun.APIBase,
mailgun.APIBaseEU,
testServer.URL,
}
for _, apiBase := range apiBases {
t.Run(apiBase, func(t *testing.T) {
mg := mailgun.NewMailgun(testKey)
err := mg.SetAPIBase(apiBase)
require.NoError(t, err)
})
}
}
func ptr[T any](v T) *T {
return &v
}