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) }
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") }
func (t *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { respond.With(w, r, t.status, t.data) }