-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient_test.go
More file actions
218 lines (181 loc) · 5.13 KB
/
client_test.go
File metadata and controls
218 lines (181 loc) · 5.13 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package sendgrid
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestNew(t *testing.T) {
client := New("test-key")
if client == nil {
t.Fatal("expected client to be non-nil")
return
}
if client.apiKey != "test-key" {
t.Errorf("expected apiKey to be 'test-key', got %s", client.apiKey)
}
if client.baseURL.String() != "https://api.sendgrid.com/v3" {
t.Errorf("expected baseURL to be 'https://api.sendgrid.com/v3', got %s", client.baseURL.String())
}
}
func TestNewWithOptions(t *testing.T) {
client := New("test-key",
OptionBaseURL("https://custom.api.com"),
OptionSubuser("test-subuser"),
OptionDebug(true),
)
if client.baseURL.String() != "https://custom.api.com" {
t.Errorf("expected baseURL to be 'https://custom.api.com', got %s", client.baseURL.String())
}
if client.subuser != "test-subuser" {
t.Errorf("expected subuser to be 'test-subuser', got %s", client.subuser)
}
if !client.debug {
t.Error("expected debug to be true")
}
}
func TestClient_NewRequest(t *testing.T) {
client := New("test-key")
req, err := client.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
if req.Method != "GET" {
t.Errorf("expected method to be 'GET', got %s", req.Method)
}
if req.URL.Path != "/v3/test" {
t.Errorf("expected path to be '/v3/test', got %s", req.URL.Path)
}
if req.Header.Get("Authorization") != "Bearer test-key" {
t.Errorf("expected authorization header to be 'Bearer test-key', got %s", req.Header.Get("Authorization"))
}
}
func TestClient_NewRequestWithBody(t *testing.T) {
client := New("test-key")
body := map[string]string{"test": "value"}
req, err := client.NewRequest("POST", "/test", body)
if err != nil {
t.Fatal(err)
}
if req.Method != "POST" {
t.Errorf("expected method to be 'POST', got %s", req.Method)
}
if req.Header.Get("Content-Type") != "application/json" {
t.Errorf("expected content-type to be 'application/json', got %s", req.Header.Get("Content-Type"))
}
}
func TestClient_NewRequestWithSubuser(t *testing.T) {
client := New("test-key", OptionSubuser("test-subuser"))
req, err := client.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
if req.Header.Get("On-Behalf-Of") != "test-subuser" {
t.Errorf("expected On-Behalf-Of header to be 'test-subuser', got %s", req.Header.Get("On-Behalf-Of"))
}
}
func TestClient_Do(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"test": "value"}`))
}))
defer server.Close()
client := New("test-key", OptionBaseURL(server.URL))
req, err := client.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
var result map[string]string
err = client.Do(context.Background(), req, &result)
if err != nil {
t.Fatal(err)
}
if result["test"] != "value" {
t.Errorf("expected result to be 'value', got %s", result["test"])
}
}
func TestClient_DoWithError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"error": "test error"}`))
}))
defer server.Close()
client := New("test-key", OptionBaseURL(server.URL))
req, err := client.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
var result map[string]string
err = client.Do(context.Background(), req, &result)
if err == nil {
t.Fatal("expected error but got none")
}
}
func TestClient_Debug(t *testing.T) {
client := New("test-key", OptionDebug(true))
if !client.Debug() {
t.Error("expected debug to be true")
}
client = New("test-key", OptionDebug(false))
if client.Debug() {
t.Error("expected debug to be false")
}
}
func TestClient_Debugf(t *testing.T) {
var buf bytes.Buffer
client := New("test-key", OptionDebug(true))
client.log = &mockLogger{writer: &buf}
client.Debugf("test %s", "message")
if buf.Len() == 0 {
t.Error("expected debug message to be written")
}
}
func TestClient_Debugln(t *testing.T) {
var buf bytes.Buffer
client := New("test-key", OptionDebug(true))
client.log = &mockLogger{writer: &buf}
client.Debugln("test", "message")
if buf.Len() == 0 {
t.Error("expected debug message to be written")
}
}
type mockLogger struct {
writer io.Writer
}
func (m *mockLogger) Output(calldepth int, s string) error {
_, err := m.writer.Write([]byte(s))
return err
}
func (m *mockLogger) Print(v ...interface{}) {
_, _ = m.writer.Write([]byte(fmt.Sprint(v...)))
}
func (m *mockLogger) Printf(format string, v ...interface{}) {
_, _ = m.writer.Write([]byte(fmt.Sprintf(format, v...)))
}
func (m *mockLogger) Println(v ...interface{}) {
_, _ = m.writer.Write([]byte(fmt.Sprintln(v...)))
}
func TestBool(t *testing.T) {
b := Bool(true)
if b == nil {
t.Fatal("expected non-nil bool pointer")
return
}
if *b != true {
t.Error("expected bool value to be true")
}
}
func TestString(t *testing.T) {
s := String("test")
if s == nil {
t.Fatal("expected non-nil string pointer")
return
}
if *s != "test" {
t.Error("expected string value to be 'test'")
}
}