This repository was archived by the owner on Oct 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_test.go
236 lines (185 loc) Β· 7.26 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package oura
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
// TestNewClient confirms that a client can be created with the default baseURL
// and default User-Agent.
func TestNewClient(t *testing.T) {
c := NewClient(nil)
assert.Equal(t, BaseURL, c.baseURL.String(), "should configure the client to use the default url")
assert.Equal(t, "go-oura", c.UserAgent, "should configure the client to use the default user-agent")
}
// TestNewRequest confirms that NewRequest returns an API request with the
// correct URL, a correctly encoded body and the correct User-Agent and
// Content-Type headers set.
func TestNewRequest(t *testing.T) {
c := NewClient(nil)
t.Run("valid request", func(tc *testing.T) {
inURL, outURL := "foo", BaseURL+"foo"
inBody, outBody := &UserInfo{
Age: 99,
Weight: 102,
Gender: "ano",
Email: "user@example.com",
Height: 184,
}, `{"age":99,"email":"user@example.com","gender":"ano","height":184,"weight":102}`+"\n"
req, err := c.NewRequest(context.Background(), "GET", inURL, inBody)
assert.Nil(tc, err, "should not return an error")
assert.Equal(tc, outURL, req.URL.String(), "should expand relative URLs to absolute URLs")
body, _ := io.ReadAll(req.Body)
assert.Equal(tc, outBody, string(body), "should encode the request body as JSON")
assert.Equal(tc, c.UserAgent, req.Header.Get("User-Agent"), "should pass the correct user agent")
assert.Equal(tc, "application/json", req.Header.Get("Content-Type"), "should set the content-type as application/json")
})
t.Run("request with invalid JSON", func(tc *testing.T) {
type T struct{ A map[interface{}]interface{} }
_, err := c.NewRequest(context.Background(), "GET", ".", &T{})
assert.Error(tc, err, "should return an error")
})
t.Run("request with an invalid URL", func(tc *testing.T) {
_, err := c.NewRequest(context.Background(), "GET", ":", nil)
assert.Error(tc, err, "should return an error")
})
t.Run("request with an invalid Method", func(tc *testing.T) {
_, err := c.NewRequest(context.Background(), "\n", "/", nil)
assert.Error(tc, err, "should return an error")
})
t.Run("request with an empty body", func(tc *testing.T) {
req, err := c.NewRequest(context.Background(), "GET", ".", nil)
assert.Nil(tc, err, "should not return an error")
assert.Nil(tc, req.Body, "should return an empty body")
})
}
// TestDo confirms that Do returns a JSON decoded value when making a request. It
// confirms the correct verb was used and that the decoded response value matches
// the expected result.
func TestDo(t *testing.T) {
t.Run("successful GET request", func(tc *testing.T) {
client, mux, teardown := setup()
defer teardown()
type foo struct{ A string }
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(tc, "GET", r.Method)
fmt.Fprint(w, `{"A":"a"}`)
})
want := &foo{"a"}
got := new(foo)
req, _ := client.NewRequest(context.Background(), "GET", ".", nil)
client.do(req, got)
assert.ObjectsAreEqual(want, got)
})
t.Run("GET request that returns an HTTP error", func(tc *testing.T) {
client, mux, teardown := setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(tc, http.MethodGet, r.Method)
w.WriteHeader(http.StatusInternalServerError)
})
req, _ := client.NewRequest(context.Background(), "GET", ".", nil)
resp, err := client.do(req, nil)
assert.Equal(tc, http.StatusInternalServerError, resp.StatusCode)
assert.Error(tc, err, "should return an error")
})
t.Run("GET request that receives an empty payload", func(tc *testing.T) {
client, mux, teardown := setup()
defer teardown()
type foo struct{ A string }
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(tc, http.MethodGet, r.Method)
w.WriteHeader(http.StatusOK)
})
req, _ := client.NewRequest(context.Background(), "GET", ".", nil)
got := new(foo)
resp, err := client.do(req, got)
assert.Equal(tc, http.StatusOK, resp.StatusCode)
assert.Nil(tc, err, "should not return an error")
})
t.Run("GET request that receives an HTML response", func(tc *testing.T) {
client, mux, teardown := setup()
defer teardown()
type foo struct{ A string }
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(tc, http.MethodGet, r.Method)
w.WriteHeader(http.StatusOK)
html := `<!doctype html>
<html lang="en-GB">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Default Page Title</title>
<link rel="shortcut icon" href="favicon.ico">
<link rel="icon" href="favicon.ico">
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
</body>
</html> `
fmt.Fprintln(w, html)
})
req, _ := client.NewRequest(context.Background(), "GET", ".", nil)
got := new(foo)
resp, err := client.do(req, got)
assert.Equal(tc, http.StatusOK, resp.StatusCode)
assert.Error(tc, err, "should return an error")
})
t.Run("GET request on a cancelled context", func(tc *testing.T) {
client, mux, teardown := setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(tc, http.MethodGet, r.Method)
w.WriteHeader(http.StatusOK)
})
ctx, cancel := context.WithCancel(context.Background())
req, _ := client.NewRequest(ctx, "GET", ".", nil)
cancel()
resp, err := client.do(req, nil)
assert.Error(tc, err, "should return an error")
assert.Nil(tc, resp, "should not return a response")
})
t.Run("GET request that returns a forbidden response", func(tc *testing.T) {
client, mux, teardown := setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(tc, http.MethodGet, r.Method)
w.WriteHeader(http.StatusForbidden)
})
req, _ := client.NewRequest(context.Background(), "GET", ".", nil)
resp, err := client.do(req, nil)
assert.Equal(tc, http.StatusForbidden, resp.StatusCode)
assert.Error(tc, err, "should return an error")
})
t.Run("GET request that returns an error response", func(tc *testing.T) {
client, mux, teardown := setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(tc, http.MethodGet, r.Method)
w.WriteHeader(http.StatusBadRequest)
resp := `{
"detail": "Start date is greater than end date: [start_date: 2020-01-25; end_date: 2020-01-22]"
}`
fmt.Fprintln(w, resp)
})
req, _ := client.NewRequest(context.Background(), "GET", ".", nil)
resp, err := client.do(req, nil)
assert.Equal(tc, http.StatusBadRequest, resp.StatusCode)
assert.Error(tc, err, "should return an error")
assert.EqualError(tc, err, "Bad Request: Start date is greater than end date: [start_date: 2020-01-25; end_date: 2020-01-22]")
})
}
// Setup establishes a test Server that can be used to provide mock responses during testing.
// It returns a pointer to a client, a mux, the server URL and a teardown function that
// must be called when testing is complete.
func setup() (client *Client, mux *http.ServeMux, teardown func()) {
mux = http.NewServeMux()
server := httptest.NewServer(mux)
c := NewClient(nil)
surl, _ := url.Parse(server.URL + "/")
c.baseURL = surl
return c, mux, server.Close
}