-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlists_test.go
More file actions
117 lines (107 loc) · 3.07 KB
/
lists_test.go
File metadata and controls
117 lines (107 loc) · 3.07 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package loops
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestListMailingLists(t *testing.T) {
tests := []struct {
name string
statusCode int
body string
wantAPIErr *APIError
wantErrMsg string
wantCount int
}{
{
name: "success",
statusCode: http.StatusOK,
body: `[{"id":"list_1","name":"Newsletter","description":"Weekly updates","isPublic":true},{"id":"list_2","name":"Announcements","description":"","isPublic":false}]`,
wantCount: 2,
},
{
name: "empty list",
statusCode: http.StatusOK,
body: `[]`,
wantCount: 0,
},
{
name: "unauthorized",
statusCode: http.StatusUnauthorized,
body: `{"error":"Invalid API key"}`,
wantAPIErr: &APIError{StatusCode: http.StatusUnauthorized, Message: "Invalid API key"},
},
{
name: "invalid json",
statusCode: http.StatusOK,
body: `not json`,
wantErrMsg: "failed to decode response",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tt.statusCode)
w.Write([]byte(tt.body))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
lists, err := client.ListMailingLists()
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)
}
if tt.wantAPIErr.Message != "" && apiErr.Message != tt.wantAPIErr.Message {
t.Errorf("Message = %q, want %q", apiErr.Message, tt.wantAPIErr.Message)
}
return
}
if tt.wantErrMsg != "" {
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.wantErrMsg)
}
if !strings.Contains(err.Error(), tt.wantErrMsg) {
t.Errorf("error = %q, want it to contain %q", err.Error(), tt.wantErrMsg)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(lists) != tt.wantCount {
t.Errorf("len(lists) = %d, want %d", len(lists), tt.wantCount)
}
})
}
}
func TestListMailingLists_ResponseData(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`[{"id":"list_1","name":"Newsletter","description":"Weekly updates","isPublic":true}]`))
}))
defer server.Close()
client := NewClient("test-key", WithBaseURL(server.URL))
lists, err := client.ListMailingLists()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
l := lists[0]
if l.ID != "list_1" {
t.Errorf("ID = %q, want %q", l.ID, "list_1")
}
if l.Name != "Newsletter" {
t.Errorf("Name = %q, want %q", l.Name, "Newsletter")
}
if l.Description != "Weekly updates" {
t.Errorf("Description = %q, want %q", l.Description, "Weekly updates")
}
if !l.IsPublic {
t.Errorf("IsPublic = false, want true")
}
}