Exemple #1
0
func TestHTTPResponse(t *testing.T) {
	p := testresponse.NewResponseWriter()
	w := NewResponseWriter(p)

	status := http.StatusInternalServerError
	errmsg := "FooBar"
	message := "Hello!"

	HTTPResponse(w, status, errmsg)
	w.Write([]byte("Hello!"))

	if w.(*ResponseWriter).HTTPStatus != status {
		t.Errorf("Unexpected HTTP status '%d', expected '%d'", w.(*ResponseWriter).HTTPStatus, status)
	}

	if w.(*ResponseWriter).HTTPError != errmsg {
		t.Errorf("Unexpected HTTP message '%s', expected '%s'", w.(*ResponseWriter).HTTPError, errmsg)
	}

	expect := fmt.Sprintf("Status: %d\n\n%s\n", status, message)
	result := p.(*testresponse.ResponseWriter).String()

	if expect != result {
		t.Errorf("Unexpected result")
	}
}
Exemple #2
0
func TestNotAllowedHandler(t *testing.T) {
	p := testresponse.NewResponseWriter()
	w := ahttp.NewResponseWriter(p)
	r := &http.Request{}

	ctx := context.Background()

	NotAllowedHandler(ctx, w, r)

	expect := "Status: 405\n\n{\"data\":{\"status\":405,\"title\":\"Method Not Allowed\",\"detail\":\"Method Not Allowed\"},\"status\":\"error\"}\n"
	result := p.(*testresponse.ResponseWriter).String()

	if expect != result {
		t.Errorf("Unexpected result: %q", result)
	}
}
Exemple #3
0
func TestInternalServerErrorHandler(t *testing.T) {
	w := testresponse.NewResponseWriter()
	r := &http.Request{}

	ctx := context.Background()

	ahttp.HTTPResponse(w, http.StatusInternalServerError, "Error!")
	InternalServerErrorHandler(ctx, w, r)

	expect := "Status: 500\n\nInternal server error\n"
	result := w.(*testresponse.ResponseWriter).String()

	if expect != result {
		t.Errorf("Unexpected result: %q", result)
	}
}
Exemple #4
0
func TestInternalServerErrorHandlerJSON(t *testing.T) {
	p := testresponse.NewResponseWriter()
	w := ahttp.NewResponseWriter(p)
	r := &http.Request{}

	ctx := context.Background()

	ahttp.HTTPResponse(w, http.StatusInternalServerError, "Error!")
	InternalServerErrorHandler(ctx, w, r)

	expect := "Status: 500\n\n{\"data\":{\"status\":500,\"title\":\"Internal Server Error\",\"detail\":\"Error!\"},\"status\":\"error\"}\n"
	result := p.(*testresponse.ResponseWriter).String()

	if expect != result {
		t.Errorf("Unexpected result: %q", result)
	}
}