-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathips_test.go
182 lines (138 loc) · 4.17 KB
/
ips_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 scaleway
import (
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"reflect"
"testing"
)
func TestIPsService_Create(t *testing.T) {
setup()
defer teardown()
inBody := &IPRequest{
Organization: "000a115d-2852-4b0a-9ce8-47f1134ba95a",
}
data := testOpenFixture(t, filepath.Join(fixtureDir, "ips_create.json"))
mux.HandleFunc("/ips", func(w http.ResponseWriter, r *http.Request) {
v := new(IPRequest)
json.NewDecoder(r.Body).Decode(v)
testMethod(t, r, "POST")
if !reflect.DeepEqual(v, inBody) {
t.Errorf("Request body = %+v, want %+v", v, inBody)
}
w.Header().Add("Content-Type", contentType)
fmt.Fprint(w, string(data))
})
ip, _, err := client.IPs.Create(inBody)
if err != nil {
t.Errorf("IPs.Create returned error: %v", err)
}
want := &IP{
Address: "212.47.226.88",
ID: "b50cd740-892d-47d3-8cbf-88510ef626e7",
Organization: "000a115d-2852-4b0a-9ce8-47f1134ba95a",
}
if !reflect.DeepEqual(ip, want) {
t.Errorf("IPs.Create returned %+v\n, want %+v", ip, want)
}
}
func TestIPsService_List(t *testing.T) {
setup()
defer teardown()
client.AuthToken = "654c95b0-2cf5-41a3-b3cc-733ffba4b4b7"
data := testOpenFixture(t, filepath.Join(fixtureDir, "ips_list.json"))
mux.HandleFunc("/ips", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Header().Add("Content-Type", contentType)
fmt.Fprint(w, string(data))
})
ips, _, err := client.IPs.List()
if err != nil {
t.Errorf("IPs.List returned error: %v", err)
}
want := []*IP{
{
Address: "212.47.226.88",
ID: "b50cd740-892d-47d3-8cbf-88510ef626e7",
Organization: "000a115d-2852-4b0a-9ce8-47f1134ba95a",
},
}
if !reflect.DeepEqual(ips, want) {
t.Errorf("IPs.List returned %+v\n, want %+v", ips, want)
}
}
func TestIPsService_Get(t *testing.T) {
setup()
defer teardown()
client.AuthToken = "654c95b0-2cf5-41a3-b3cc-733ffba4b4b7"
data := testOpenFixture(t, filepath.Join(fixtureDir, "ips_get.json"))
want := &IP{
Address: "212.47.226.88",
ID: "b50cd740-892d-47d3-8cbf-88510ef626e7",
Organization: "000a115d-2852-4b0a-9ce8-47f1134ba95a",
}
mux.HandleFunc(fmt.Sprintf("/ips/%s", want.ID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Header().Add("Content-Type", contentType)
fmt.Fprint(w, string(data))
})
ip, _, err := client.IPs.Get(want.ID)
if err != nil {
t.Errorf("IPs.Get returned error: %v", err)
}
if !reflect.DeepEqual(ip, want) {
t.Errorf("IPS.Get returned %+v\n, want %+v", ip, want)
}
}
func TestIPsService_Attach(t *testing.T) {
setup()
defer teardown()
inBody := &IPRequest{
Address: "212.47.226.88",
ID: "b50cd740-892d-47d3-8cbf-88510ef626e7",
Organization: "000a115d-2852-4b0a-9ce8-47f1134ba95a",
Server: "c2d8994f-1582-413e-8d48-c53076db06cc",
}
data := testOpenFixture(t, filepath.Join(fixtureDir, "ips_attach.json"))
mux.HandleFunc(fmt.Sprintf("/ips/%s", inBody.ID), func(w http.ResponseWriter, r *http.Request) {
v := new(IPRequest)
json.NewDecoder(r.Body).Decode(v)
testMethod(t, r, "PUT")
if !reflect.DeepEqual(v, inBody) {
t.Errorf("Request body = %+v, want %+v", v, inBody)
}
w.Header().Add("Content-Type", contentType)
fmt.Fprint(w, string(data))
})
ip, _, err := client.IPs.Attach(inBody, inBody.ID)
if err != nil {
t.Errorf("IPs.Attach returned error: %v", err)
}
want := &IP{
Address: "212.47.226.88",
ID: "b50cd740-892d-47d3-8cbf-88510ef626e7",
Organization: "000a115d-2852-4b0a-9ce8-47f1134ba95a",
Server: &Server{
ID: "c2d8994f-1582-413e-8d48-c53076db06cc",
Name: "default_server_name - acfb51",
},
}
if !reflect.DeepEqual(ip, want) {
t.Errorf("IPs.Attach returned %+v, want %+v", ip, want)
}
}
func TestIPsService_Delete(t *testing.T) {
setup()
defer teardown()
client.AuthToken = "654c95b0-2cf5-41a3-b3cc-733ffba4b4b7"
ipID := "654c95b0-2cf5-41a3-b3cc-733ffba4b4b7"
mux.HandleFunc(fmt.Sprintf("/ips/%s", ipID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
w.Header().Add("Content-Type", contentType)
})
_, err := client.IPs.Delete(ipID)
if err != nil {
t.Errorf("IPs.Delete returned error: %v", err)
}
}