-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcircleci_test.go
73 lines (61 loc) · 1.66 KB
/
circleci_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
package circleci
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
)
func setup() (client *Client, mux *http.ServeMux, serverURL string, teardown func()) {
mux = http.NewServeMux()
server := httptest.NewServer(mux)
url, err := url.Parse(server.URL)
if err != nil {
panic(fmt.Sprintf("failed to parse test server URL: %s, error: %v", server.URL, err))
}
cfg := DefaultConfig()
cfg.Token = "fake-token"
client, err = NewClient(cfg)
if err != nil {
panic(fmt.Sprintf("failed to initialize client, error: %v", err))
}
client.baseURL = url
return client, mux, server.URL, server.Close
}
func testMethod(t *testing.T, r *http.Request, want string) {
t.Helper()
if got := r.Method; got != want {
t.Errorf("Method got %v, want %v", got, want)
}
}
func testHeader(t *testing.T, r *http.Request, header string, want string) {
t.Helper()
if got := r.Header.Get(header); got != want {
t.Errorf("Header.Get(%q) got %q, want %q", header, got, want)
}
}
func testBody(t *testing.T, r *http.Request, want string) {
t.Helper()
var mgot map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&mgot)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
var mwant map[string]interface{}
err = json.Unmarshal([]byte(want), &mwant)
if err != nil {
t.Errorf("Error Unmarshalling want string: %v", err)
}
if !cmp.Equal(mgot, mwant) {
t.Errorf("request Body is %s, want %s", mgot, mwant)
}
}
func testQuery(t *testing.T, r *http.Request, key, want string) {
t.Helper()
got := r.URL.Query().Get(key)
if got != want {
t.Errorf("URL.Query(%q) got %q, want %q", key, got, want)
}
}