コード例 #1
0
			It("should log and respond with status 400", func() {
				resp := httptest.NewRecorder()
				handler.ServeHTTP(resp, request)

				Expect(resp.Code).To(Equal(http.StatusBadRequest))
				Expect(logger).To(gbytes.Say("cni-add.*body-read-failed"))
			})
		})

		Context("when unmarshalling payload fails", func() {
			BeforeEach(func() {
				request.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{}`)))
			})

			It("logs an error and responds with code 400", func() {
				unmarshaler.UnmarshalReturns(errors.New("some-unmarshal-error"))
				resp := httptest.NewRecorder()
				handler.ServeHTTP(resp, request)

				Expect(resp.Code).To(Equal(http.StatusBadRequest))
				Expect(logger).To(gbytes.Say("cni-add.unmarshal-failed.*some-unmarshal-error"))

				Expect(controller.AddCallCount()).To(BeZero())
			})
		})

		DescribeTable("missing payload fields",
			func(paramToRemove, jsonName string) {
				field := reflect.ValueOf(&payload).Elem().FieldByName(paramToRemove)
				if !field.IsValid() {
					Fail("invalid test: payload does not have a field named " + paramToRemove)
コード例 #2
0
			Context("reading the response body read fails", func() {
				BeforeEach(func() {
					mockHttpClient := &fakes.HTTPClient{}
					mockHttpClient.DoReturns(&http.Response{
						StatusCode: 201,
						Body:       &testsupport.BadReader{},
					}, nil)

					jsonClient.HttpClient = mockHttpClient
				})

				It("should return an error", func() {
					err := jsonClient.BuildAndDo(config)
					Expect(err).To(MatchError("failed to read response body: banana"))
				})
			})

			Context("the response JSON cannot be unmarshaled", func() {
				BeforeEach(func() {
					unmarshaler.UnmarshalReturns(errors.New("something went wrong"))
				})

				It("should return an error", func() {
					err := jsonClient.BuildAndDo(config)
					Expect(err).To(MatchError("failed to unmarshal result: something went wrong"))
				})
			})
		})
	})
})