-
Notifications
You must be signed in to change notification settings - Fork 59
/
buildkite_test.go
182 lines (151 loc) · 5.04 KB
/
buildkite_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
package buildkite
import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
)
type httpCall struct {
Method string
Path string
}
type mockServer struct {
mux *http.ServeMux
calls []httpCall
}
func (ms *mockServer) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
ms.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
ms.calls = append(ms.calls, httpCall{
Method: r.Method,
Path: r.URL.Path,
})
handler(w, r)
})
}
// newMockServerAndClient sets up a test HTTP server along with a buildkite.Client that is
// configured to talk to that test server. Tests should register handlers on
// mux which provide mock responses for the API method being tested.
func newMockServerAndClient(t *testing.T) (*mockServer, *Client, func()) {
// test server
mux := http.NewServeMux()
// Fail test if unexpected request is received, "/" matches any request not matched by a more specific handler
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("unexpected %s request for %s", r.Method, r.URL.Path)
})
server := httptest.NewServer(mux)
client, err := NewOpts(WithBaseURL(server.URL))
if err != nil {
t.Fatalf("unexpected NewOpts() error: %v", err)
}
ms := &mockServer{mux: mux, calls: make([]httpCall, 0, 10)}
return ms, client, func() { server.Close() }
}
func testMethod(t *testing.T, r *http.Request, want string) {
if got := r.Method; got != want {
t.Errorf("Request method: %v, want %v", got, want)
}
}
type values map[string]string
type valuesList []struct{ key, val string }
func testFormValues(t *testing.T, r *http.Request, values values) {
want := url.Values{}
for k, v := range values {
want.Add(k, v)
}
err := r.ParseForm()
if err != nil {
t.Fatalf("parsing HTTP form body: %v", err)
}
if diff := cmp.Diff(r.Form, want); diff != "" {
t.Errorf("Request parameters diff: (-got +want)\n%s", diff)
}
}
func testFormValuesList(t *testing.T, r *http.Request, values valuesList) {
want := url.Values{}
for _, v := range values {
want.Add(v.key, v.val)
}
err := r.ParseForm()
if err != nil {
t.Fatalf("parsing HTTP form body: %v", err)
}
if diff := cmp.Diff(r.Form, want); diff != "" {
t.Errorf("Request parameters diff: (-got +want)\n%s", diff)
}
}
func TestNewClient(t *testing.T) {
c, err := NewClient()
if err != nil {
t.Fatalf("unexpected NewClient() error: %v", err)
}
if got, want := c.BaseURL.String(), DefaultBaseURL; got != want {
t.Errorf("NewClient BaseURL is %v, want %v", got, want)
}
if got, want := c.UserAgent, DefaultUserAgent; got != want {
t.Errorf("NewClient UserAgent is %v, want %v", got, want)
}
}
func TestNewRequest(t *testing.T) {
c, err := NewClient()
if err != nil {
t.Fatalf("unexpected NewClient() error: %v", err)
}
inURL, outURL := "/foo", DefaultBaseURL+"foo"
inBody := User{ID: "123", Name: "Jane Doe", Email: "[email protected]"}
outBody := `{"id":"123","name":"Jane Doe","email":"[email protected]"}` + "\n"
req, _ := c.NewRequest(context.Background(), "GET", inURL, inBody)
// test that relative URL was expanded
if got, want := req.URL.String(), outURL; got != want {
t.Errorf("NewRequest(%q) URL is %v, want %v", inURL, got, want)
}
// test that body was JSON encoded
body, _ := io.ReadAll(req.Body)
if got, want := string(body), outBody; got != want {
t.Errorf("NewRequest(%v) Body is %v, want %v", inBody, got, want)
}
// test that content-type said it was JSON too
if got, want := req.Header.Get("Content-Type"), "application/json"; got != want {
t.Errorf("NewRequest() Content-Type is %v, want %v", got, want)
}
// test that default user-agent is attached to the request
if got, want := req.Header.Get("User-Agent"), c.UserAgent; got != want {
t.Errorf("NewRequest() User-Agent is %v, want %v", got, want)
}
}
func TestNewRequest_WhenTokenAuthIsConfigured_AddsBearerTokenToHeaders(t *testing.T) {
c, err := NewOpts(WithTokenAuth("hunter2"))
if err != nil {
t.Fatalf("unexpected NewOpts() error: %v", err)
}
req, _ := c.NewRequest(context.Background(), "GET", "/foo", nil)
if got, want := req.Header.Get("Authorization"), "Bearer hunter2"; got != want {
t.Errorf("NewRequest() Authorization is %v, want %v", got, want)
}
}
func TestResponse_populatePageValues(t *testing.T) {
r := http.Response{
Header: http.Header{
"Link": {`<https://api.buildkite.com/?page=1>; rel="first",` +
` <https://api.buildkite.com/?page=2>; rel="prev",` +
` <https://api.buildkite.com/?page=4>; rel="next",` +
` <https://api.buildkite.com/?page=5>; rel="last"`,
},
},
}
response := newResponse(&r)
if got, want := response.FirstPage, 1; got != want {
t.Errorf("response.FirstPage: %v, want %v", got, want)
}
if got, want := response.PrevPage, 2; want != got {
t.Errorf("response.PrevPage: %v, want %v", got, want)
}
if got, want := response.NextPage, 4; want != got {
t.Errorf("response.NextPage: %v, want %v", got, want)
}
if got, want := response.LastPage, 5; want != got {
t.Errorf("response.LastPage: %v, want %v", got, want)
}
}