forked from Ableton/go-travis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
travis_test.go
178 lines (148 loc) · 3.68 KB
/
travis_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
// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package travis
import (
"net/url"
"testing"
)
func TestClient_NewDefaultClient(t *testing.T) {
c := NewDefaultClient("")
assert(
t,
c.BaseURL.String() == TRAVIS_API_DEFAULT_URL,
"Client.BaseURL = %s; expected %s", c.BaseURL.String(), TRAVIS_API_DEFAULT_URL,
)
}
func TestClient_NewRequest(t *testing.T) {
c := NewClient(TRAVIS_API_DEFAULT_URL, "")
req, err := c.NewRequest("GET", "/test", nil, nil)
if err != nil {
t.Fatal(err)
}
assert(
t,
req.Method == "GET",
"Wrong Request Method set",
)
assert(
t,
req.URL.String() == TRAVIS_API_DEFAULT_URL+"test",
"Wrong Request URL set",
)
}
func TestClient_NewRequest_with_nil_headers_provided(t *testing.T) {
baseUrl, _ := url.Parse(TRAVIS_API_DEFAULT_URL)
c := NewClient(TRAVIS_API_DEFAULT_URL, "")
req, err := c.NewRequest("GET", "/users", nil, nil)
if err != nil {
t.Fatal(err)
}
assert(
t,
req.Header.Get("User-Agent") == TRAVIS_USER_AGENT,
"Wrong Request User-Agent header set",
)
assert(
t,
req.Header.Get("Accept") == TRAVIS_REQUEST_ACCEPT_HEADER,
"Wrong Request Accept header set",
)
assert(
t,
req.Header.Get("Host") == baseUrl.Host,
"Wrong Request Host header set",
)
}
func TestClient_NewRequest_with_non_overriding_headers_provided(t *testing.T) {
baseUrl, _ := url.Parse(TRAVIS_API_DEFAULT_URL)
c := NewClient(TRAVIS_API_DEFAULT_URL, "")
h := map[string]string{
"Abc": "123",
}
req, err := c.NewRequest("GET", "/users", nil, h)
if err != nil {
t.Fatal(err)
}
assert(
t,
req.Header.Get("Abc") == "123",
"Wrong Request Abc header set",
)
assert(
t,
req.Header.Get("User-Agent") == TRAVIS_USER_AGENT,
"Wrong Request User-Agent header set",
)
assert(
t,
req.Header.Get("Accept") == TRAVIS_REQUEST_ACCEPT_HEADER,
"Wrong Request Accept header set",
)
assert(
t,
req.Header.Get("Host") == baseUrl.Host,
"Wrong Request Host header set",
)
}
func TestClient_NewRequest_with_overriding_headers_provided(t *testing.T) {
c := NewClient(TRAVIS_API_DEFAULT_URL, "")
h := map[string]string{
"Host": "api.travis-ci.com",
}
req, err := c.NewRequest("GET", "/users", nil, h)
if err != nil {
t.Fatal(err)
}
assert(
t,
req.Header.Get("User-Agent") == TRAVIS_USER_AGENT,
"Wrong Request User-Agent header set",
)
assert(
t,
req.Header.Get("Accept") == TRAVIS_REQUEST_ACCEPT_HEADER,
"Wrong Request Accept header set",
)
assert(
t,
req.Header.Get("Host") == "api.travis-ci.com",
"Wrong Request Host header set",
)
}
func TestGetNextPage_with_non_slice_value_provided_errors(t *testing.T) {
opt := &BuildListOptions{}
err := opt.GetNextPage(struct{}{})
notOk(t, err)
}
func TestGetNextPage_with_empty_slice_value_provided_errors(t *testing.T) {
opt := &BuildListOptions{}
err := opt.GetNextPage([]string{})
notOk(t, err)
}
func TestGetNextPage_with_invalid_type_slice_provided_errors(t *testing.T) {
opt := &BuildListOptions{}
err := opt.GetNextPage([]string{"abc", "123"})
notOk(t, err)
}
func TestGetNextPage_with_valid_slice_and_positive_number_provided(t *testing.T) {
opt := &BuildListOptions{}
err := opt.GetNextPage([]Build{Build{Number: "27"}})
ok(t, err)
assert(
t,
opt.AfterNumber == 27,
"returned next page after_number is %d; expected %d", opt.AfterNumber, 27,
)
}
func TestGetNextPage_with_valid_slice_and_negative_number_provided(t *testing.T) {
opt := &BuildListOptions{}
err := opt.GetNextPage([]Build{Build{Number: "0"}})
ok(t, err)
assert(
t,
opt.AfterNumber == 0,
"returned next page after_number is %d; expected %d", opt.AfterNumber, 0,
)
}