git.iko.soy/typeable/oauth2-proxy
on master help sign in
Add tests for request result
commitd0b6c0496084d10bf7a667937598f1db8f2405e4 authorJoel Speed <joel.speed@hotmail.co.uk> dateMon 6 Jul 2020, 18:26 (6 years ago) parentfbf4063
1 file changed+326
pkg/requests/result_test.go+326
pkg/requests/result_test.gonew file+326
@@ -0,0 +1,326 @@
1+package requests
2+
3+import (
4+ "errors"
5+ "net/http"
6+
7+ . "github.com/onsi/ginkgo"
8+ . "github.com/onsi/ginkgo/extensions/table"
9+ . "github.com/onsi/gomega"
10+)
11+
12+var _ = Describe("Result suite", func() {
13+ Context("with a result", func() {
14+ type resultTableInput struct {
15+ result Result
16+ expectedError error
17+ expectedStatusCode int
18+ expectedHeaders http.Header
19+ expectedBody []byte
20+ }
21+
22+ DescribeTable("accessors should return expected results",
23+ func(in resultTableInput) {
24+ if in.expectedError != nil {
25+ Expect(in.result.Error()).To(MatchError(in.expectedError))
26+ } else {
27+ Expect(in.result.Error()).To(BeNil())
28+ }
29+
30+ Expect(in.result.StatusCode()).To(Equal(in.expectedStatusCode))
31+ Expect(in.result.Headers()).To(Equal(in.expectedHeaders))
32+ Expect(in.result.Body()).To(Equal(in.expectedBody))
33+ },
34+ Entry("with an empty result", resultTableInput{
35+ result: &result{},
36+ expectedError: nil,
37+ expectedStatusCode: 0,
38+ expectedHeaders: nil,
39+ expectedBody: nil,
40+ }),
41+ Entry("with an error", resultTableInput{
42+ result: &result{
43+ err: errors.New("error"),
44+ },
45+ expectedError: errors.New("error"),
46+ expectedStatusCode: 0,
47+ expectedHeaders: nil,
48+ expectedBody: nil,
49+ }),
50+ Entry("with a response with no headers", resultTableInput{
51+ result: &result{
52+ response: &http.Response{
53+ StatusCode: http.StatusTeapot,
54+ },
55+ },
56+ expectedError: nil,
57+ expectedStatusCode: http.StatusTeapot,
58+ expectedHeaders: nil,
59+ expectedBody: nil,
60+ }),
61+ Entry("with a response with no status code", resultTableInput{
62+ result: &result{
63+ response: &http.Response{
64+ Header: http.Header{
65+ "foo": []string{"bar"},
66+ },
67+ },
68+ },
69+ expectedError: nil,
70+ expectedStatusCode: 0,
71+ expectedHeaders: http.Header{
72+ "foo": []string{"bar"},
73+ },
74+ expectedBody: nil,
75+ }),
76+ Entry("with a response with a body", resultTableInput{
77+ result: &result{
78+ body: []byte("some body"),
79+ },
80+ expectedError: nil,
81+ expectedStatusCode: 0,
82+ expectedHeaders: nil,
83+ expectedBody: []byte("some body"),
84+ }),
85+ Entry("with all fields", resultTableInput{
86+ result: &result{
87+ err: errors.New("some error"),
88+ response: &http.Response{
89+ StatusCode: http.StatusFound,
90+ Header: http.Header{
91+ "header": []string{"value"},
92+ },
93+ },
94+ body: []byte("a body"),
95+ },
96+ expectedError: errors.New("some error"),
97+ expectedStatusCode: http.StatusFound,
98+ expectedHeaders: http.Header{
99+ "header": []string{"value"},
100+ },
101+ expectedBody: []byte("a body"),
102+ }),
103+ )
104+ })
105+
106+ Context("UnmarshalInto", func() {
107+ type testStruct struct {
108+ A string `json:"a"`
109+ B int `json:"b"`
110+ }
111+
112+ type unmarshalIntoTableInput struct {
113+ result Result
114+ expectedErr error
115+ expectedOutput *testStruct
116+ }
117+
118+ DescribeTable("with a result",
119+ func(in unmarshalIntoTableInput) {
120+ input := &testStruct{}
121+ err := in.result.UnmarshalInto(input)
122+ if in.expectedErr != nil {
123+ Expect(err).To(MatchError(in.expectedErr))
124+ } else {
125+ Expect(err).ToNot(HaveOccurred())
126+ }
127+ Expect(input).To(Equal(in.expectedOutput))
128+ },
129+ Entry("with an error", unmarshalIntoTableInput{
130+ result: &result{
131+ err: errors.New("got an error"),
132+ response: &http.Response{
133+ StatusCode: http.StatusOK,
134+ },
135+ body: []byte("{\"a\": \"foo\"}"),
136+ },
137+ expectedErr: errors.New("got an error"),
138+ expectedOutput: &testStruct{},
139+ }),
140+ Entry("with a 409 status code", unmarshalIntoTableInput{
141+ result: &result{
142+ err: nil,
143+ response: &http.Response{
144+ StatusCode: http.StatusConflict,
145+ },
146+ body: []byte("{\"a\": \"foo\"}"),
147+ },
148+ expectedErr: errors.New("unexpected status \"409\": {\"a\": \"foo\"}"),
149+ expectedOutput: &testStruct{},
150+ }),
151+ Entry("when the response has a valid json response", unmarshalIntoTableInput{
152+ result: &result{
153+ err: nil,
154+ response: &http.Response{
155+ StatusCode: http.StatusOK,
156+ },
157+ body: []byte("{\"a\": \"foo\", \"b\": 1}"),
158+ },
159+ expectedErr: nil,
160+ expectedOutput: &testStruct{A: "foo", B: 1},
161+ }),
162+ Entry("when the response body is empty", unmarshalIntoTableInput{
163+ result: &result{
164+ err: nil,
165+ response: &http.Response{
166+ StatusCode: http.StatusOK,
167+ },
168+ body: []byte(""),
169+ },
170+ expectedErr: errors.New("error unmarshalling body: unexpected end of JSON input"),
171+ expectedOutput: &testStruct{},
172+ }),
173+ Entry("when the response body is not json", unmarshalIntoTableInput{
174+ result: &result{
175+ err: nil,
176+ response: &http.Response{
177+ StatusCode: http.StatusOK,
178+ },
179+ body: []byte("not json"),
180+ },
181+ expectedErr: errors.New("error unmarshalling body: invalid character 'o' in literal null (expecting 'u')"),
182+ expectedOutput: &testStruct{},
183+ }),
184+ )
185+ })
186+
187+ Context("UnmarshalJSON", func() {
188+ type testStruct struct {
189+ A string `json:"a"`
190+ B int `json:"b"`
191+ }
192+
193+ type unmarshalJSONTableInput struct {
194+ result Result
195+ expectedErr error
196+ expectedOutput *testStruct
197+ }
198+
199+ DescribeTable("with a result",
200+ func(in unmarshalJSONTableInput) {
201+ j, err := in.result.UnmarshalJSON()
202+ if in.expectedErr != nil {
203+ Expect(err).To(MatchError(in.expectedErr))
204+ Expect(j).To(BeNil())
205+ return
206+ }
207+
208+ // No error so j should not be nil
209+ Expect(err).ToNot(HaveOccurred())
210+
211+ input := &testStruct{
212+ A: j.Get("a").MustString(),
213+ B: j.Get("b").MustInt(),
214+ }
215+ Expect(input).To(Equal(in.expectedOutput))
216+ },
217+ Entry("with an error", unmarshalJSONTableInput{
218+ result: &result{
219+ err: errors.New("got an error"),
220+ response: &http.Response{
221+ StatusCode: http.StatusOK,
222+ },
223+ body: []byte("{\"a\": \"foo\"}"),
224+ },
225+ expectedErr: errors.New("got an error"),
226+ expectedOutput: &testStruct{},
227+ }),
228+ Entry("with a 409 status code", unmarshalJSONTableInput{
229+ result: &result{
230+ err: nil,
231+ response: &http.Response{
232+ StatusCode: http.StatusConflict,
233+ },
234+ body: []byte("{\"a\": \"foo\"}"),
235+ },
236+ expectedErr: errors.New("unexpected status \"409\": {\"a\": \"foo\"}"),
237+ expectedOutput: &testStruct{},
238+ }),
239+ Entry("when the response has a valid json response", unmarshalJSONTableInput{
240+ result: &result{
241+ err: nil,
242+ response: &http.Response{
243+ StatusCode: http.StatusOK,
244+ },
245+ body: []byte("{\"a\": \"foo\", \"b\": 1}"),
246+ },
247+ expectedErr: nil,
248+ expectedOutput: &testStruct{A: "foo", B: 1},
249+ }),
250+ Entry("when the response body is empty", unmarshalJSONTableInput{
251+ result: &result{
252+ err: nil,
253+ response: &http.Response{
254+ StatusCode: http.StatusOK,
255+ },
256+ body: []byte(""),
257+ },
258+ expectedErr: errors.New("error reading json: EOF"),
259+ expectedOutput: &testStruct{},
260+ }),
261+ Entry("when the response body is not json", unmarshalJSONTableInput{
262+ result: &result{
263+ err: nil,
264+ response: &http.Response{
265+ StatusCode: http.StatusOK,
266+ },
267+ body: []byte("not json"),
268+ },
269+ expectedErr: errors.New("error reading json: invalid character 'o' in literal null (expecting 'u')"),
270+ expectedOutput: &testStruct{},
271+ }),
272+ )
273+ })
274+
275+ Context("getBodyForUnmarshal", func() {
276+ type getBodyForUnmarshalTableInput struct {
277+ result *result
278+ expectedErr error
279+ expectedBody []byte
280+ }
281+
282+ DescribeTable("when getting the body", func(in getBodyForUnmarshalTableInput) {
283+ body, err := in.result.getBodyForUnmarshal()
284+ if in.expectedErr != nil {
285+ Expect(err).To(MatchError(in.expectedErr))
286+ } else {
287+ Expect(err).ToNot(HaveOccurred())
288+ }
289+ Expect(body).To(Equal(in.expectedBody))
290+ },
291+ Entry("when the result has an error", getBodyForUnmarshalTableInput{
292+ result: &result{
293+ err: errors.New("got an error"),
294+ response: &http.Response{
295+ StatusCode: http.StatusOK,
296+ },
297+ body: []byte("body"),
298+ },
299+ expectedErr: errors.New("got an error"),
300+ expectedBody: nil,
301+ }),
302+ Entry("when the response has a 409 status code", getBodyForUnmarshalTableInput{
303+ result: &result{
304+ err: nil,
305+ response: &http.Response{
306+ StatusCode: http.StatusConflict,
307+ },
308+ body: []byte("body"),
309+ },
310+ expectedErr: errors.New("unexpected status \"409\": body"),
311+ expectedBody: nil,
312+ }),
313+ Entry("when the response has a 200 status code", getBodyForUnmarshalTableInput{
314+ result: &result{
315+ err: nil,
316+ response: &http.Response{
317+ StatusCode: http.StatusOK,
318+ },
319+ body: []byte("body"),
320+ },
321+ expectedErr: nil,
322+ expectedBody: []byte("body"),
323+ }),
324+ )
325+ })
326+})