7 . "github.com/onsi/ginkgo"
8 . "github.com/onsi/ginkgo/extensions/table"
9 . "github.com/onsi/gomega"
12var _ = Describe("Result suite", func() {
13 Context("with a result", func() {
14 type resultTableInput struct {
17 expectedStatusCode int
18 expectedHeaders http.Header
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))
27 Expect(in.result.Error()).To(BeNil())
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))
34 Entry("with an empty result", resultTableInput{
37 expectedStatusCode: 0,
41 Entry("with an error", resultTableInput{
43 err: errors.New("error"),
45 expectedError: errors.New("error"),
46 expectedStatusCode: 0,
50 Entry("with a response with no headers", resultTableInput{
52 response: &http.Response{
53 StatusCode: http.StatusTeapot,
57 expectedStatusCode: http.StatusTeapot,
61 Entry("with a response with no status code", resultTableInput{
63 response: &http.Response{
65 "foo": []string{"bar"},
70 expectedStatusCode: 0,
71 expectedHeaders: http.Header{
72 "foo": []string{"bar"},
76 Entry("with a response with a body", resultTableInput{
78 body: []byte("some body"),
81 expectedStatusCode: 0,
83 expectedBody: []byte("some body"),
85 Entry("with all fields", resultTableInput{
87 err: errors.New("some error"),
88 response: &http.Response{
89 StatusCode: http.StatusFound,
91 "header": []string{"value"},
94 body: []byte("a body"),
96 expectedError: errors.New("some error"),
97 expectedStatusCode: http.StatusFound,
98 expectedHeaders: http.Header{
99 "header": []string{"value"},
101 expectedBody: []byte("a body"),
106 Context("UnmarshalInto", func() {
107 type testStruct struct {
112 type unmarshalIntoTableInput struct {
115 expectedOutput *testStruct
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))
125 Expect(err).ToNot(HaveOccurred())
127 Expect(input).To(Equal(in.expectedOutput))
129 Entry("with an error", unmarshalIntoTableInput{
131 err: errors.New("got an error"),
132 response: &http.Response{
133 StatusCode: http.StatusOK,
135 body: []byte("{\"a\": \"foo\"}"),
137 expectedErr: errors.New("got an error"),
138 expectedOutput: &testStruct{},
140 Entry("with a 409 status code", unmarshalIntoTableInput{
143 response: &http.Response{
144 StatusCode: http.StatusConflict,
146 body: []byte("{\"a\": \"foo\"}"),
148 expectedErr: errors.New("unexpected status \"409\": {\"a\": \"foo\"}"),
149 expectedOutput: &testStruct{},
151 Entry("when the response has a valid json response", unmarshalIntoTableInput{
154 response: &http.Response{
155 StatusCode: http.StatusOK,
157 body: []byte("{\"a\": \"foo\", \"b\": 1}"),
160 expectedOutput: &testStruct{A: "foo", B: 1},
162 Entry("when the response body is empty", unmarshalIntoTableInput{
165 response: &http.Response{
166 StatusCode: http.StatusOK,
170 expectedErr: errors.New("error unmarshalling body: unexpected end of JSON input"),
171 expectedOutput: &testStruct{},
173 Entry("when the response body is not json", unmarshalIntoTableInput{
176 response: &http.Response{
177 StatusCode: http.StatusOK,
179 body: []byte("not json"),
181 expectedErr: errors.New("error unmarshalling body: invalid character 'o' in literal null (expecting 'u')"),
182 expectedOutput: &testStruct{},
187 Context("UnmarshalJSON", func() {
188 type testStruct struct {
193 type unmarshalJSONTableInput struct {
196 expectedOutput *testStruct
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())
208 // No error so j should not be nil
209 Expect(err).ToNot(HaveOccurred())
211 input := &testStruct{
212 A: j.Get("a").MustString(),
213 B: j.Get("b").MustInt(),
215 Expect(input).To(Equal(in.expectedOutput))
217 Entry("with an error", unmarshalJSONTableInput{
219 err: errors.New("got an error"),
220 response: &http.Response{
221 StatusCode: http.StatusOK,
223 body: []byte("{\"a\": \"foo\"}"),
225 expectedErr: errors.New("got an error"),
226 expectedOutput: &testStruct{},
228 Entry("with a 409 status code", unmarshalJSONTableInput{
231 response: &http.Response{
232 StatusCode: http.StatusConflict,
234 body: []byte("{\"a\": \"foo\"}"),
236 expectedErr: errors.New("unexpected status \"409\": {\"a\": \"foo\"}"),
237 expectedOutput: &testStruct{},
239 Entry("when the response has a valid json response", unmarshalJSONTableInput{
242 response: &http.Response{
243 StatusCode: http.StatusOK,
245 body: []byte("{\"a\": \"foo\", \"b\": 1}"),
248 expectedOutput: &testStruct{A: "foo", B: 1},
250 Entry("when the response body is empty", unmarshalJSONTableInput{
253 response: &http.Response{
254 StatusCode: http.StatusOK,
258 expectedErr: errors.New("error reading json: EOF"),
259 expectedOutput: &testStruct{},
261 Entry("when the response body is not json", unmarshalJSONTableInput{
264 response: &http.Response{
265 StatusCode: http.StatusOK,
267 body: []byte("not json"),
269 expectedErr: errors.New("error reading json: invalid character 'o' in literal null (expecting 'u')"),
270 expectedOutput: &testStruct{},
275 Context("getBodyForUnmarshal", func() {
276 type getBodyForUnmarshalTableInput struct {
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))
287 Expect(err).ToNot(HaveOccurred())
289 Expect(body).To(Equal(in.expectedBody))
291 Entry("when the result has an error", getBodyForUnmarshalTableInput{
293 err: errors.New("got an error"),
294 response: &http.Response{
295 StatusCode: http.StatusOK,
297 body: []byte("body"),
299 expectedErr: errors.New("got an error"),
302 Entry("when the response has a 409 status code", getBodyForUnmarshalTableInput{
305 response: &http.Response{
306 StatusCode: http.StatusConflict,
308 body: []byte("body"),
310 expectedErr: errors.New("unexpected status \"409\": body"),
313 Entry("when the response has a 200 status code", getBodyForUnmarshalTableInput{
316 response: &http.Response{
317 StatusCode: http.StatusOK,
319 body: []byte("body"),
322 expectedBody: []byte("body"),
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list