-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_client_test.go
242 lines (223 loc) · 7.84 KB
/
http_client_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
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
240
241
242
package httpclient
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"gopkg.in/h2non/gentleman.v2"
"github.com/cloudtrust/httpclient/mock"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"gopkg.in/h2non/gentleman.v2/plugins/body"
"gopkg.in/h2non/gentleman.v2/plugins/url"
)
func TestNewHttpClient(t *testing.T) {
t.Run("InvalidURI", func(t *testing.T) {
var _, err = New("://localhost", time.Minute)
assert.NotNil(t, err)
})
}
func TestNoResponse(t *testing.T) {
var client, err = New("http://localhost:19766", time.Minute)
assert.Nil(t, err)
assert.NotNil(t, client)
// No server is running: sending any request should return an error
t.Run("Get", func(t *testing.T) {
err = client.Get(nil, url.Path("/any/path/to/target"))
assert.True(t, strings.Contains(err.Error(), "cannotObtain.response"))
})
t.Run("Post", func(t *testing.T) {
_, err = client.Post(nil, url.Path("/any/path/to/target"))
assert.True(t, strings.Contains(err.Error(), "cannotObtain.response"))
})
t.Run("Put", func(t *testing.T) {
err = client.Put(url.Path("/any/path/to/target"), body.String("content"))
assert.True(t, strings.Contains(err.Error(), "cannotObtain.response"))
})
t.Run("Delete", func(t *testing.T) {
err = client.Delete(url.Path("/any/path/to/target"))
assert.True(t, strings.Contains(err.Error(), "cannotObtain.response"))
})
}
func TestRequestUpdaterFails(t *testing.T) {
var expectedError = errors.New("request updater failure")
var client, err = New("http://localhost", time.Minute, func(r *gentleman.Request) (*gentleman.Request, error) {
return nil, expectedError
})
assert.Nil(t, err)
assert.NotNil(t, client)
// No server is running: sending any request should return an error
t.Run("Get", func(t *testing.T) {
err = client.Get(nil, url.Path("/any/path/to/target"))
assert.Equal(t, expectedError, err)
})
t.Run("Post", func(t *testing.T) {
_, err = client.Post(nil, url.Path("/any/path/to/target"))
assert.Equal(t, expectedError, err)
})
t.Run("Put", func(t *testing.T) {
err = client.Put(url.Path("/any/path/to/target"), body.String("content"))
assert.Equal(t, expectedError, err)
})
t.Run("Delete", func(t *testing.T) {
err = client.Delete(url.Path("/any/path/to/target"))
assert.Equal(t, expectedError, err)
})
}
func TestProcessResponse(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var mockHandler = mock.NewHandler(mockCtrl)
var path = "/sample"
r := mux.NewRouter()
r.Handle(path, mockHandler)
ts := httptest.NewServer(r)
defer ts.Close()
var client, _ = New(ts.URL, time.Minute, func(r *gentleman.Request) (*gentleman.Request, error) {
return r, nil
})
var expectedError = HTTPError{
StatusCode: http.StatusUnauthorized,
Message: "error message",
}
mockHandler.EXPECT().ServeHTTP(gomock.Any(), gomock.Any()).DoAndReturn(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("error message"))
}).Times(4)
t.Run("GET-Unauthorized", func(t *testing.T) {
var resp string
var err = client.Get(&resp, url.Path(path))
assert.Equal(t, expectedError, err)
assert.Equal(t, "", resp)
})
t.Run("POST-Unauthorized", func(t *testing.T) {
var resp string
var _, err = client.Post(&resp, url.Path(path))
assert.Equal(t, expectedError, err)
})
t.Run("PUT-Unauthorized", func(t *testing.T) {
var err = client.Put(url.Path(path))
assert.Equal(t, expectedError, err)
})
t.Run("DELETE-Unauthorized", func(t *testing.T) {
var err = client.Delete(url.Path(path))
assert.Equal(t, expectedError, err)
})
expectedError = HTTPError{
StatusCode: http.StatusBadRequest,
Message: "error message",
}
mockHandler.EXPECT().ServeHTTP(gomock.Any(), gomock.Any()).DoAndReturn(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"errorMessage":"error message"}`))
}).Times(4)
t.Run("GET-Internal server error", func(t *testing.T) {
var resp string
var err = client.Get(&resp, url.Path(path))
assert.Equal(t, expectedError, err)
assert.Equal(t, "", resp)
})
t.Run("POST-Internal server error", func(t *testing.T) {
var resp string
var _, err = client.Post(&resp, url.Path(path))
assert.Equal(t, expectedError, err)
})
t.Run("PUT-Internal server error", func(t *testing.T) {
var err = client.Put(url.Path(path))
assert.Equal(t, expectedError, err)
})
t.Run("DELETE-Internal server error (json error message)", func(t *testing.T) {
var err = client.Delete(url.Path(path))
assert.Equal(t, expectedError, err)
})
mockHandler.EXPECT().ServeHTTP(gomock.Any(), gomock.Any()).DoAndReturn(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`error message`))
})
t.Run("DELETE-Internal server error (plain text error message)", func(t *testing.T) {
var err = client.Delete(url.Path(path))
assert.Equal(t, expectedError, err)
})
mockHandler.EXPECT().ServeHTTP(gomock.Any(), gomock.Any()).DoAndReturn(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Location", "the location")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`response`))
}).Times(4)
t.Run("GET-Success", func(t *testing.T) {
var resp string
var err = client.Get(&resp, url.Path(path))
assert.Nil(t, err)
assert.Equal(t, "response", resp)
})
t.Run("POST-Success", func(t *testing.T) {
var resp string
var location, err = client.Post(&resp, url.Path(path))
assert.Nil(t, err)
assert.Equal(t, "the location", location)
})
t.Run("PUT-Success", func(t *testing.T) {
var err = client.Put(url.Path(path))
assert.Nil(t, err)
})
t.Run("DELETE-Success", func(t *testing.T) {
var err = client.Delete(url.Path(path))
assert.Nil(t, err)
})
var response = `{"key1": "value1", "key2": 234}`
mockHandler.EXPECT().ServeHTTP(gomock.Any(), gomock.Any()).DoAndReturn(func(w http.ResponseWriter, r *http.Request) {
var content = r.URL.Query().Get("content")
w.Header().Set("Content-Type", content)
w.WriteHeader(http.StatusOK)
w.Write([]byte(response))
}).Times(4)
t.Run("Content-Type text/plain", func(t *testing.T) {
var resp string
var plugins = CreateQueryPlugins("content", "text/plain")
plugins = append(plugins, url.Path(path))
var err = client.Get(&resp, plugins...)
assert.Nil(t, err)
assert.Equal(t, response, resp)
})
t.Run("Content-Type application/octet-stream", func(t *testing.T) {
var resp []byte
var plugins = CreateQueryPlugins("content", "application/octet-stream")
plugins = append(plugins, url.Path(path))
var err = client.Get(&resp, plugins...)
assert.Nil(t, err)
assert.Equal(t, []byte(response), resp)
})
t.Run("Content-Type application/json", func(t *testing.T) {
var resp map[string]interface{}
var plugins = CreateQueryPlugins("content", "application/json")
plugins = append(plugins, url.Path(path))
var err = client.Get(&resp, plugins...)
assert.Nil(t, err)
assert.Len(t, resp, 2)
assert.Equal(t, "value1", resp["key1"])
})
t.Run("Content-Type not supported", func(t *testing.T) {
var plugins = CreateQueryPlugins("content", "application/unknown")
plugins = append(plugins, url.Path(path))
var err = client.Get(nil, plugins...)
assert.NotNil(t, err)
})
mockHandler.EXPECT().ServeHTTP(gomock.Any(), gomock.Any()).DoAndReturn(func(w http.ResponseWriter, r *http.Request) {
var content = r.URL.Query().Get("content")
w.Header().Set("Content-Type", content)
w.WriteHeader(http.StatusOK)
w.Write([]byte(""))
})
t.Run("Empty content", func(t *testing.T) {
var plugins = CreateQueryPlugins("content", "application/unknown")
plugins = append(plugins, url.Path(path))
var err = client.Get(nil, plugins...)
assert.Nil(t, err)
})
}