forked from xanzy/go-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab_test.go
152 lines (128 loc) · 3.52 KB
/
gitlab_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
package gitlab
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
)
// setup sets up a test HTTP server along with a gitlab.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 setup() (*http.ServeMux, *httptest.Server, *Client) {
// mux is the HTTP request multiplexer used with the test server.
mux := http.NewServeMux()
// server is a test HTTP server used to provide mock API responses.
server := httptest.NewServer(mux)
// client is the Gitlab client being tested.
client := NewClient(nil, "")
client.SetBaseURL(server.URL)
return mux, server, client
}
// teardown closes the test HTTP server.
func teardown(server *httptest.Server) {
server.Close()
}
func testURL(t *testing.T, r *http.Request, want string) {
if got := r.RequestURI; got != want {
t.Errorf("Request url: %+v, want %s", got, want)
}
}
func testMethod(t *testing.T, r *http.Request, want string) {
if got := r.Method; got != want {
t.Errorf("Request method: %s, want %s", got, want)
}
}
type values map[string]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.Errorf("Error parsing form: %v", err)
}
if got := r.Form; !reflect.DeepEqual(got, want) {
t.Errorf("Request parameters: %v, want %v", got, want)
}
}
func testJSONBody(t *testing.T, r *http.Request, want values) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
var got values
err = json.Unmarshal(b, &got)
if err != nil {
t.Errorf("Error unmarshalling request body: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Request parameters: %v, want %v", got, want)
}
}
func TestNewClient(t *testing.T) {
c := NewClient(nil, "")
if c.BaseURL().String() != defaultBaseURL {
t.Errorf("NewClient BaseURL is %s, want %s", c.BaseURL().String(), defaultBaseURL)
}
if c.UserAgent != userAgent {
t.Errorf("NewClient UserAgent is %s, want %s", c.UserAgent, userAgent)
}
}
func TestCheckResponse(t *testing.T) {
req, err := NewClient(nil, "").NewRequest("GET", "test", nil, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
resp := &http.Response{
Request: req,
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(strings.NewReader(`
{
"message": {
"prop1": [
"message 1",
"message 2"
],
"prop2":[
"message 3"
],
"embed1": {
"prop3": [
"msg 1",
"msg2"
]
},
"embed2": {
"prop4": [
"some msg"
]
}
},
"error": "message 1"
}`)),
}
errResp := CheckResponse(resp)
if errResp == nil {
t.Fatal("Expected error response.")
}
want := "GET https://gitlab.com/api/v3/test: 400 {error: message 1}, {message: {embed1: {prop3: [msg 1, msg2]}}, {embed2: {prop4: [some msg]}}, {prop1: [message 1, message 2]}, {prop2: [message 3]}}"
if errResp.Error() != want {
t.Errorf("Expected error: %s, got %s", want, errResp.Error())
}
}
func TestRequestWithContext(t *testing.T) {
ctx := context.WithValue(context.Background(), interface{}("myKey"), interface{}("myValue"))
req, err := NewClient(nil, "").NewRequest("GET", "test", nil, []OptionFunc{WithContext(ctx)})
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
if req.Context() != ctx {
t.Fatal("Context was not set correctly")
}
}