Exemplo n.º 1
0
func TestErrorWrapInternalError(t *testing.T) {
	recorder := httptest.NewRecorder()
	raven := &wrapServer{}
	wrap := NewErrorWrap(raven)
	router := httprouter.New()
	handler := negroni.New(negroni.Wrap(router))

	router.GET("/hello-internal-error", wrap.Do(testHelloInternalError))

	req, _ := http.NewRequest("GET", "/hello-internal-error", nil)
	handler.ServeHTTP(recorder, req)

	util.Expect(t, recorder.Code, http.StatusInternalServerError)
	util.Expect(t, strings.TrimSpace(recorder.Body.String()), `{"error":"Database error","error_code":"error-select"}`)
	util.Refute(t, len(raven.Msgs), 0)
}
Exemplo n.º 2
0
// TestErrorWrap creates a server with a method that raises an error
// and checks that ErrorWrap works as expected
func TestErrorWrapOK(t *testing.T) {
	recorder := httptest.NewRecorder()
	raven := &wrapServer{}
	wrap := NewErrorWrap(raven)
	router := httprouter.New()
	handler := negroni.New(negroni.Wrap(router))

	router.GET("/hello", wrap.Do(testHelloOK))

	req, _ := http.NewRequest("GET", "/hello", nil)
	handler.ServeHTTP(recorder, req)

	util.Expect(t, recorder.Code, http.StatusOK)
	util.Expect(t, recorder.Body.Len(), len("Hello World!"))
	util.Expect(t, len(raven.Msgs), 0)
}
Exemplo n.º 3
0
func TestErrorWrapNotFound(t *testing.T) {
	recorder := httptest.NewRecorder()
	raven := &wrapServer{}
	wrap := NewErrorWrap(raven)
	router := httprouter.New()
	handler := negroni.New(negroni.Wrap(router))

	router.GET("/hello-not-found", wrap.Do(testHelloNotFound))

	req, _ := http.NewRequest("GET", "/hello-not-found", nil)
	handler.ServeHTTP(recorder, req)

	util.Expect(t, recorder.Code, http.StatusNotFound)
	util.Expect(t, strings.TrimSpace(recorder.Body.String()), `{"error":"Not found","error_code":"not-found"}`)
	util.Expect(t, len(raven.Msgs), 0)
}
func TestRecoveryJson(t *testing.T) {
	buff := bytes.NewBufferString("")
	recorder := httptest.NewRecorder()
	recServer := &recoveryServer{}
	rec := NewRecoveryJson(log.New(buff, "[recoveryjson] ", 0), recServer)

	n := negroni.New()
	// replace log for testing
	n.Use(rec)
	n.UseHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		panic(`some error`)
	}))
	n.ServeHTTP(recorder, (*http.Request)(nil))
	util.Expect(t, recorder.Code, http.StatusInternalServerError)
	util.Refute(t, recorder.Body.Len(), 0)
	util.Refute(t, len(buff.String()), 0)
	util.Refute(t, len(recServer.Msgs), 0)
}