NilRoutes: false,
			})
			Expect(testResource.Context()).To(Equal(ctx))
		})
	})
	Describe("MapFromJSON", func() {

		It("should map JSON string bytes to map[] if data is a valid JSON", func() {
			data := []byte(`
				{
					"a": "isString",
					"b": 100,
					"c": true
				}
			`)
			body := test_helpers.MapFromJSON(data)
			Expect(body["a"]).To(Equal("isString"))
			Expect(body["b"]).To(Equal(float64(100)))
			Expect(body["c"]).To(Equal(true))

		})

		It("should panic if data is an invalid json ", func() {
			data := []byte("{this is an invalid json}")
			Expect(func() {
				_ = test_helpers.MapFromJSON(data)
			}).Should(Panic())
		})
	})

	Describe("DecodeResponseToType", func() {
Beispiel #2
0
			router := server.NewRouter(ctx, nil)
			router.AddRoutes(routes)
			s.UseContextMiddleware(r)
			s.UseRouter(router)

			// record HTTP responses
			recorder = httptest.NewRecorder()
		})

		Context("when user does not specify API version", func() {

			It("should use default API version", func() {

				request, _ = http.NewRequest("GET", "/api/test", nil)
				s.ServeHTTP(recorder, request)
				bodyJSON = test_helpers.MapFromJSON(recorder.Body.Bytes())

				Expect(bodyJSON["version"]).To(Equal(string(route.DefaultVersion)))
			})

		})

		Context("when user specify a valid Accept header (application/json) with valid API version", func() {

			It("should use specified API version", func() {

				request, _ = http.NewRequest("GET", "/api/test", nil)
				request.Header.Set("Accept", "application/json;version=0.1")
				s.ServeHTTP(recorder, request)
				bodyJSON = test_helpers.MapFromJSON(recorder.Body.Bytes())