Ejemplo n.º 1
0
func TestMultipleWith(t *testing.T) {
	is := is.New(t)

	options := &respond.Options{}
	handler := options.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		respond.With(w, r, http.StatusInternalServerError, errors.New("borked"))
		respond.With(w, r, http.StatusOK, nil)
	}))

	w := httptest.NewRecorder()
	r := newTestRequest()

	is.PanicWith("respond: multiple responses", func() {
		handler.ServeHTTP(w, r)
	})

	options = &respond.Options{
		AllowMultiple: true,
	}
	handler = options.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		respond.With(w, r, http.StatusInternalServerError, errors.New("borked"))
		respond.With(w, r, http.StatusOK, nil)
	}))

	w = httptest.NewRecorder()
	r = newTestRequest()

	handler.ServeHTTP(w, r)

}
Ejemplo n.º 2
0
func TestWith(t *testing.T) {
	is := is.New(t)

	w := httptest.NewRecorder()
	r := newTestRequest()

	respond.With(w, r, http.StatusOK, testdata)

	is.Equal(http.StatusOK, w.Code)
	var data map[string]interface{}
	is.NoErr(json.Unmarshal(w.Body.Bytes(), &data))
	is.Equal(data, testdata)
	is.Equal(w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
}
Ejemplo n.º 3
0
func (t *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	respond.With(w, r, t.status, t.data)
}