-
Notifications
You must be signed in to change notification settings - Fork 25
/
client_test.go
221 lines (183 loc) · 5.57 KB
/
client_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
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
219
220
221
package unifi
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"reflect"
"strconv"
"strings"
"testing"
"time"
)
func TestClientBadContentType(t *testing.T) {
c, done := testClient(t, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`foo`))
})
defer done()
req, err := c.newRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Not the best possible check but verifies that the content type is incorrect
_, err = c.do(req, nil)
if want, got := `received "text/plain; charset=utf-8"`, err.Error(); !strings.Contains(got, want) {
t.Fatalf("unexpected error message: %v", got)
}
}
func TestClientBadHTTPStatusCode(t *testing.T) {
c, done := testClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", jsonContentType)
w.WriteHeader(http.StatusInternalServerError)
})
defer done()
req, err := c.newRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Not the best possible check but verifies that the content type is incorrect
_, err = c.do(req, nil)
if want, got := `unexpected HTTP status code: 500`, err.Error(); !strings.Contains(got, want) {
t.Fatalf("unexpected error message: %v", got)
}
}
func TestClientBadJSON(t *testing.T) {
c, done := testClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", jsonContentType)
_, _ = w.Write([]byte(`foo`))
})
defer done()
req, err := c.newRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Pass empty struct to trigger JSON unmarshaling path
var v struct{}
_, err = c.do(req, &v)
if _, ok := err.(*json.SyntaxError); !ok {
t.Fatalf("unexpected error type: %T", err)
}
}
func TestClientRetainsCookies(t *testing.T) {
const cookieName = "foo"
wantCookie := &http.Cookie{
Name: cookieName,
Value: "bar",
}
var i int
c, done := testClient(t, func(w http.ResponseWriter, r *http.Request) {
defer func() { i++ }()
w.Header().Set("Content-Type", jsonContentType)
switch i {
case 0:
http.SetCookie(w, wantCookie)
case 1:
c, err := r.Cookie(cookieName)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if want, got := wantCookie, c; !reflect.DeepEqual(want, got) {
t.Fatalf("unexpected cookie:\n- want: %v\n- got: %v",
want, got)
}
}
_, _ = w.Write([]byte(`{}`))
})
defer done()
for i := 0; i < 2; i++ {
req, err := c.newRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
_, err = c.do(req, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
}
func TestClientLogin(t *testing.T) {
const (
wantUsername = "test"
wantPassword = "test"
)
wantBody := &login{
Username: wantUsername,
Password: wantPassword,
}
c, done := testClient(t, testHandler(t, http.MethodPost, "/api/login", wantBody, nil))
defer done()
if err := c.Login(wantUsername, wantPassword); err != nil {
t.Fatalf("unexpected error from Client.Login: %v", err)
}
}
func TestInsecureHTTPClient(t *testing.T) {
timeout := 5 * time.Second
c := InsecureHTTPClient(timeout)
if want, got := c.Timeout, timeout; want != got {
t.Fatalf("unexpected client timeout:\n- want: %v\n- got: %v",
want, got)
}
got := c.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify
if want := true; want != got {
t.Fatalf("unexpected client insecure skip verify value:\n- want: %v\n- got: %v",
want, got)
}
}
func testClient(t *testing.T, fn func(w http.ResponseWriter, r *http.Request)) (*Client, func()) {
s := httptest.NewServer(http.HandlerFunc(fn))
c, err := NewClient(s.URL, nil)
if err != nil {
t.Fatalf("error creating Client: %v", err)
}
return c, func() { s.Close() }
}
func testHandler(t *testing.T, method string, path string, body interface{}, out interface{}) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if want, got := method, r.Method; want != got {
t.Fatalf("unexpected HTTP method:\n- want: %v\n- got: %v", want, got)
}
if want, got := path, r.URL.Path; want != got {
t.Fatalf("unexpected URL path:\n- want: %v\n- got: %v", want, got)
}
if r.Method != http.MethodPost && r.Method != http.MethodPut {
w.Header().Set("Content-Type", jsonContentType)
if err := json.NewEncoder(w).Encode(out); err != nil {
t.Fatalf("error marshaling JSON response body: %v", err)
}
return
}
// Content-Length must always be set for POST/PUT
cls := r.Header.Get("Content-Length")
if cls == "" {
t.Fatal("Content-Length header is not present or empty")
}
cl, err := strconv.Atoi(cls)
if err != nil {
t.Fatalf("unexpected error parsing Content-Length: %v", err)
}
// Content-Length must match body length
b := make([]byte, cl)
if n, err := io.ReadFull(r.Body, b); err != nil {
t.Fatalf("failed to read entire JSON body: read %d bytes, err: %v", n, err)
}
// Body must be valid JSON
var v struct{}
if err := json.Unmarshal(b, &v); err != nil {
t.Fatalf("error unmarshaling JSON body: %v", err)
}
// Request body must match input JSON
wantJSON, err := json.Marshal(body)
if err != nil {
t.Fatalf("error marshaling input body to JSON: %v", err)
}
if want, got := string(wantJSON), strings.TrimSpace(string(b)); want != got {
t.Fatalf("unexpected JSON request body:\n- want: %v\n- got: %v",
want, got)
}
// Write output JSON if set
w.Header().Set("Content-Type", jsonContentType)
if err := json.NewEncoder(w).Encode(out); err != nil {
t.Fatalf("error marshaling JSON response body: %v", err)
}
}
}