-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdedicated_sending_ips_test.go
More file actions
78 lines (72 loc) · 1.87 KB
/
Copy pathdedicated_sending_ips_test.go
File metadata and controls
78 lines (72 loc) · 1.87 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
package loops
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetDedicatedSendingIPs(t *testing.T) {
tests := []struct {
name string
statusCode int
body string
wantAPIErr *APIError
wantIPs []string
}{
{
name: "success",
statusCode: http.StatusOK,
body: `["1.2.3.4","5.6.7.8"]`,
wantIPs: []string{"1.2.3.4", "5.6.7.8"},
},
{
name: "empty",
statusCode: http.StatusOK,
body: `[]`,
wantIPs: []string{},
},
{
name: "server error",
statusCode: http.StatusInternalServerError,
body: `{"success":false,"message":"oops"}`,
wantAPIErr: &APIError{StatusCode: http.StatusInternalServerError, Message: "oops"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var gotPath string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
w.WriteHeader(tt.statusCode)
w.Write([]byte(tt.body))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
ips, err := client.GetDedicatedSendingIPs()
if tt.wantAPIErr != nil {
var apiErr *APIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected *APIError, got %T: %v", err, err)
}
if apiErr.StatusCode != tt.wantAPIErr.StatusCode {
t.Errorf("StatusCode = %d, want %d", apiErr.StatusCode, tt.wantAPIErr.StatusCode)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gotPath != "/dedicated-sending-ips" {
t.Errorf("path = %q, want /dedicated-sending-ips", gotPath)
}
if len(ips) != len(tt.wantIPs) {
t.Errorf("len(ips) = %d, want %d", len(ips), len(tt.wantIPs))
}
for i := range tt.wantIPs {
if ips[i] != tt.wantIPs[i] {
t.Errorf("ips[%d] = %q, want %q", i, ips[i], tt.wantIPs[i])
}
}
})
}
}