Example #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)

}
Example #2
0
func handleThingsRead(w http.ResponseWriter, r *http.Request) {
	db := context.Get(r, "db").(*mgo.Session) // HL

	var results []interface{}
	if err := db.DB("myapp").C("things").Find(nil).All(&results); err != nil {
		respond.With(w, r, http.StatusInternalServerError, err)
		return
	}

	respond.With(w, r, http.StatusOK, results)
}
Example #3
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")
}
Example #4
0
func handleIndex(w http.ResponseWriter, r *http.Request) {
	data := ""
	respond.With(w, r, http.StatusOK, data)
}
Example #5
0
func (t *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	respond.With(w, r, t.status, t.data)
}