-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscaleway_test.go
More file actions
239 lines (197 loc) · 5.96 KB
/
scaleway_test.go
File metadata and controls
239 lines (197 loc) · 5.96 KB
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
237
238
239
package scaleway
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"reflect"
"testing"
)
var (
// mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux
// client is the GitHub client being tested.
client *Client
// server is a test HTTP server used to provide mock API responses.
server *httptest.Server
)
// setup sets up a test HTTP server along with a scaleway.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() {
// test server
mux = http.NewServeMux()
server = httptest.NewServer(mux)
// scaleway client configured to use test server
client = NewClient(nil)
url, _ := url.Parse(server.URL)
client.AccountBaseURL = url
client.ComputeBaseURL = url
}
// teardown closes the test HTTP server.
func teardown() {
server.Close()
}
// testMethod check if the method request if what you want.
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)
}
}
// This is the directory where our test fixtures are
const fixtureDir = "./test-fixtures"
// Test Helpers
func testOpenFixture(t *testing.T, name string) []byte {
f, err := os.Open(name)
defer f.Close()
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(f)
if err != nil {
t.Fatal(err)
}
return data
}
func TestNewClient(t *testing.T) {
c := NewClient(nil)
if got, want := c.AccountBaseURL.String(), defaultAccountBaseURL; got != want {
t.Errorf("NewClient AccountBaseURL is %v, want %v", got, want)
}
if got, want := c.ComputeBaseURL.String(), defaultComputeBaseURL; got != want {
t.Errorf("NewClient ComputeBaseURL is %v, want %v", got, want)
}
if got, want := c.UserAgent, userAgent; got != want {
t.Errorf("NewClient AccountBaseURL is %v, want %v", got, want)
}
}
type inBody struct {
A interface{}
}
type requestAccountTest struct {
inURL string
outURL string
inBody inBody
outBody string
wantErr bool
}
var requestAccountTests = []requestAccountTest{
{"/foo", defaultAccountBaseURL + "foo", inBody{A: "a"}, `{"A":"a"}` + "\n", false},
{"%zzzzzz", defaultAccountBaseURL + "foo", inBody{A: "a"}, ``, true},
{"/foo", defaultAccountBaseURL + "foo", inBody{A: func() {}}, ``, true},
}
func TestNewRequestAccount(t *testing.T) {
for _, tt := range requestAccountTests {
c := NewClient(nil)
req, err := c.NewRequestAccount("GET", tt.inURL, tt.inBody)
if (err != nil) != tt.wantErr {
t.Errorf("NewRequest error: %v", err)
continue
}
if req == nil {
continue
}
if got, want := req.URL.String(), tt.outURL; got != want {
t.Errorf("Newrequest (%q) URL is %v, want %v", tt.inURL, got, want)
}
body, _ := ioutil.ReadAll(req.Body)
if got, want := string(body), tt.outBody; got != want {
t.Errorf("Newrequest (%v) Body is %#v, want %#v", tt.inBody, got, want)
}
if got, want := req.Header.Get("User-Agent"), c.UserAgent; got != want {
t.Errorf("Newrequest () User-Agent %v, want %v", got, want)
}
if got, want := req.Header.Get("Content-Type"), contentType; got != want {
t.Errorf("Newrequest () Content-Type %v, want %v", got, want)
}
}
}
type requestComputeTest struct {
inURL string
outURL string
inBody inBody
outBody string
wantErr bool
}
var requestComputeTests = []requestComputeTest{
{"/foo", defaultComputeBaseURL + "foo", inBody{A: "a"}, `{"A":"a"}` + "\n", false},
{"%zzzzzz", defaultComputeBaseURL + "foo", inBody{A: "a"}, ``, true},
{"/foo", defaultComputeBaseURL + "foo", inBody{A: func() {}}, ``, true},
}
var requestTests = []requestComputeTest{
{"/foo", "/foo", inBody{A: "a"}, `{"A":"a"}` + "\n", false},
{"%zzzzzz", "", inBody{A: "a"}, ``, true},
{"/foo", "/foo", inBody{A: func() {}}, ``, true},
}
func TestNewRequest(t *testing.T) {
for _, tt := range requestTests {
c := NewClient(nil)
req, err := c.newRequest("GET", tt.inURL, tt.inBody)
if (err != nil) != tt.wantErr {
t.Errorf("NewRequest error: %v", err)
continue
}
if req == nil {
continue
}
if got, want := req.URL.String(), tt.outURL; got != want {
t.Errorf("Newrequest (%q) URL is %v, want %v", tt.inURL, got, want)
}
body, _ := ioutil.ReadAll(req.Body)
if got, want := string(body), tt.outBody; got != want {
t.Errorf("Newrequest (%v) Body is %#v, want %#v", tt.inBody, got, want)
}
if got, want := req.Header.Get("User-Agent"), c.UserAgent; got != want {
t.Errorf("Newrequest () User-Agent %v, want %v", got, want)
}
if got, want := req.Header.Get("Content-Type"), contentType; got != want {
t.Errorf("Newrequest () Content-Type %v, want %v", got, want)
}
}
}
func TestNewRequestCompute(t *testing.T) {
for _, tt := range requestComputeTests {
c := NewClient(nil)
req, err := c.NewRequestCompute("GET", tt.inURL, tt.inBody)
if (err != nil) != tt.wantErr {
t.Errorf("NewRequest error: %v", err)
continue
}
if req == nil {
continue
}
if got, want := req.URL.String(), tt.outURL; got != want {
t.Errorf("Newrequest (%q) URL is %v, want %v", tt.inURL, got, want)
}
body, _ := ioutil.ReadAll(req.Body)
if got, want := string(body), tt.outBody; got != want {
t.Errorf("Newrequest (%v) Body is %#v, want %#v", tt.inBody, got, want)
}
if got, want := req.Header.Get("User-Agent"), c.UserAgent; got != want {
t.Errorf("Newrequest () User-Agent %v, want %v", got, want)
}
if got, want := req.Header.Get("Content-Type"), contentType; got != want {
t.Errorf("Newrequest () Content-Type %v, want %v", got, want)
}
}
}
func TestDo(t *testing.T) {
setup()
defer teardown()
type foo struct {
A string
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"A":"a"}`)
})
req, _ := client.NewRequestAccount("GET", "/", nil)
body := new(foo)
client.Do(req, body)
want := &foo{"a"}
if !reflect.DeepEqual(body, want) {
t.Errorf("Response body = %v, want %v", body, want)
}
}