Example #1
0
func (JsonApiBinding) Bind(req *http.Request, obj interface{}) error {
	length, err := strconv.Atoi(req.Header.Get("Content-Length"))
	if err != nil {
		return err
	}

	formData := make([]byte, length)
	_, err = req.Body.Read(formData)
	if err != nil {
		if err.Error() != "EOF" {
			return err
		}
	}

	return jsonapi.UnmarshalFromJSON(formData, obj)
}
	})

	Context("Create", func() {
		post := func(payload SomeData) {
			m, err := jsonapi.MarshalToJSON(payload)
			Expect(err).ToNot(HaveOccurred())
			req, err := http.NewRequest("POST", "/v1/someDatas", strings.NewReader(string(m)))
			Expect(err).ToNot(HaveOccurred())
			api.Handler().ServeHTTP(rec, req)
		}

		It("returns object with 201 created", func() {
			post(payload)
			Expect(rec.Code).To(Equal(http.StatusCreated))
			var actual SomeData
			err := jsonapi.UnmarshalFromJSON(rec.Body.Bytes(), &actual)
			Expect(err).ToNot(HaveOccurred())
			Expect(payloadID).To(Equal(actual))
		})

		It("return no content 204 with client side generated id", func() {
			post(payloadID)
			Expect(rec.Code).To(Equal(http.StatusNoContent))
			Expect(rec.Body.String()).To(BeEmpty())
		})

		It("return accepted and no content", func() {
			post(SomeData{ID: "accept", Data: "nothing"})
			Expect(rec.Code).To(Equal(http.StatusAccepted))
			Expect(rec.Body.String()).To(BeEmpty())
		})