-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_test.go
199 lines (186 loc) · 6.29 KB
/
api_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package foreman
import (
"encoding/json"
"errors"
"net/http"
"net/url"
"reflect"
"strings"
"testing"
)
func TestIndex(t *testing.T) {
tt := []struct {
resource string
query string
}{
{},
{"hosts", "search=myhost.local"},
{"bla", "a=1&b=2&c=10"},
{"", "a=1&b=2&c=10"},
{"blabla", ""},
}
opts := Options{
Address: "http://foreman.acme.io:89",
APIVersion: "v3",
}
c := New(opts)
address := opts.Address + "/api/" + opts.APIVersion
for _, te := range tt {
c.httpClient = newTestingHTTPClient(func(req *http.Request) (*http.Response, error) {
fURL := address + "/" + te.resource
if te.query != "" {
fURL = fURL + "?" + te.query
}
if req.URL.String() != fURL {
t.Errorf("expected request URL to be '%s' but got '%s'", fURL, req.URL)
}
if req.URL.Scheme != "http" {
t.Errorf("expected request URL Scheme to be 'http' but got '%s'", req.URL.Scheme)
}
if req.URL.Host != strings.TrimPrefix(opts.Address, "http://") {
t.Errorf("expected request URL Host to be '%s' but got '%s'", fURL, req.URL.Host)
}
if req.URL.Path != "/api/v3/"+te.resource {
t.Errorf("expected request URL Path to be '%s' but got '%s'", "/api/v3/"+te.resource, req.URL.Path)
}
if req.URL.RawQuery != te.query {
t.Errorf("expected request URL RawQuery to be '%s' but got '%s'", te.query, req.URL.RawQuery)
}
if req.Method != http.MethodGet {
t.Errorf("expected request Method to be '%s' but got '%s'", http.MethodGet, req.Method)
}
return &http.Response{}, nil
})
qVal, err := url.ParseQuery(te.query)
if err != nil {
t.Errorf("expected ParseQuery not to return error: %s", err)
}
_, err = c.Index(Query{te.resource, qVal})
if err != nil {
t.Errorf("expected Index not to return error: %s", err)
}
}
}
func TestIndexError(t *testing.T) {
c := New(Options{})
if _, err := c.Index(Query{":wrongURL", url.Values{}}); err == nil {
t.Error("expected Index to return error")
}
}
func TestCreate(t *testing.T) {
tt := []struct {
name string
parameters map[string]interface{}
expected map[string]interface{}
}{
{"architectures", map[string]interface{}{"name": "arch"}, map[string]interface{}{"name": "arch"}},
{"media", map[string]interface{}{"name": "Arch"}, map[string]interface{}{"name": "Arch"}},
}
c := New(Options{})
fURL := defaultAddress + "/api/" + defaultAPIVersion
for _, te := range tt {
c.httpClient = newTestingHTTPClient(func(req *http.Request) (*http.Response, error) {
if req.URL.String() != fURL+"/"+te.name {
t.Errorf("expected request URL to be '%s' but got '%s'", fURL+"/"+te.name, req.URL)
}
if req.Method != http.MethodPost {
t.Errorf("expected request Method to be '%s' but got '%s'", http.MethodPost, req.Method)
}
defer req.Body.Close()
var jsonMap map[string]interface{}
if err := json.NewDecoder(req.Body).Decode(&jsonMap); err != nil {
t.Errorf("expected json.NewDecoder.Decode to not return error: %s", err)
}
if !reflect.DeepEqual(jsonMap, te.expected) {
t.Errorf("expected body to be '%s' but got '%s'", te.expected, jsonMap)
}
return &http.Response{}, nil
})
c.Create(Resource{Name: te.name, Parameters: te.parameters})
}
}
type testFailedMarshaler map[string]string
func (tfm testFailedMarshaler) MarshalJSON() ([]byte, error) {
return nil, errors.New("")
}
func TestAPIError(t *testing.T) {
c := New(Options{})
tt := []struct {
resource Resource
expErr string
apiF func(Resource) (*http.Response, error)
}{
{Resource{Name: ":bla"}, "missing protocol scheme", c.Create},
{Resource{Name: ":bla"}, "missing protocol scheme", c.Update},
{Resource{Name: ":bla"}, "missing protocol scheme", c.Delete},
{Resource{Name: "bla", Parameters: testFailedMarshaler(map[string]string{"h": "h"})}, "json: error calling MarshalJSON", c.Create},
{Resource{Name: "bla", Parameters: testFailedMarshaler(map[string]string{"h": "h"})}, "json: error calling MarshalJSON", c.Update},
}
c.httpClient = newTestingHTTPClient(func(req *http.Request) (*http.Response, error) { return &http.Response{}, nil })
for _, te := range tt {
_, err := te.apiF(te.resource)
if err == nil {
t.Error("expected api function to return error")
t.FailNow()
}
if !strings.Contains(err.Error(), te.expErr) {
t.Errorf("expected error to contain '%s' but got '%s'", te.expErr, err)
}
}
}
func TestUpdate(t *testing.T) {
tt := []struct {
name string
id string
parameters map[string]interface{}
expected map[string]interface{}
}{
{"architectures", "1", map[string]interface{}{"name": "arch"}, map[string]interface{}{"name": "arch"}},
{"media", "Arch", map[string]interface{}{"name": "Arch2016"}, map[string]interface{}{"name": "Arch2016"}},
}
c := New(Options{})
fURL := defaultAddress + "/api/" + defaultAPIVersion
for _, te := range tt {
c.httpClient = newTestingHTTPClient(func(req *http.Request) (*http.Response, error) {
if req.URL.String() != fURL+"/"+te.name+"/"+te.id {
t.Errorf("expected request URL to be '%s' but got '%s'", fURL+"/"+te.name+"/"+te.id, req.URL)
}
if req.Method != http.MethodPut {
t.Errorf("expected request Method to be '%s' but got '%s'", http.MethodPut, req.Method)
}
defer req.Body.Close()
var jsonMap map[string]interface{}
if err := json.NewDecoder(req.Body).Decode(&jsonMap); err != nil {
t.Errorf("expected json.NewDecoder.Decode to not return error: %s", err)
}
if !reflect.DeepEqual(jsonMap, te.expected) {
t.Errorf("expected body to be '%s' but got '%s'", te.expected, jsonMap)
}
return &http.Response{}, nil
})
c.Update(Resource{Name: te.name, ID: te.id, Parameters: te.parameters})
}
}
func TestDelete(t *testing.T) {
tt := []struct {
name string
id string
}{
{"architectures", "1"},
{"media", "Arch"},
}
c := New(Options{})
fURL := defaultAddress + "/api/" + defaultAPIVersion
for _, te := range tt {
c.httpClient = newTestingHTTPClient(func(req *http.Request) (*http.Response, error) {
if req.URL.String() != fURL+"/"+te.name+"/"+te.id {
t.Errorf("expected request URL to be '%s' but got '%s'", fURL+"/"+te.name+"/"+te.id, req.URL)
}
if req.Method != http.MethodDelete {
t.Errorf("expected request Method to be '%s' but got '%s'", http.MethodDelete, req.Method)
}
return &http.Response{}, nil
})
c.Delete(Resource{Name: te.name, ID: te.id})
}
}