Exemplo n.º 1
0
func TestFactoryErrorHandler(t *testing.T) {
	routes := []routem.Route{
		&testRoute{path: "/test"},
	}
	factory := NewHandlerFactory(context.Background(), func(err routem.HTTPError, ctx context.Context) error {
		response := routem.ResponseWriterFromContext(ctx)
		http.Error(response, "Converted to 400", 400)
		return nil
	})
	response := assertServerFactory(t, factory, routes, routem.Put, "http://localhost/test/c")
	assert.Equal(t, 400, response.Code)
}
Exemplo n.º 2
0
func TestRouteErrorHandler(t *testing.T) {
	routes := []routem.Route{
		&testRoute{
			path: "/test",
			handler: func(ctx context.Context) routem.HTTPError {
				return routem.NewHTTPError(304, fmt.Errorf("Moved Permanently!"))
			},
			errorHandler: func(err routem.HTTPError, ctx context.Context) error {
				response := routem.ResponseWriterFromContext(ctx)
				http.Error(response, "Converted to 400", 400)
				return nil
			},
		},
	}

	response := assertServer(t, routes, routem.Get, "http://localhost/test")
	assert.Equal(t, 400, response.Code)
}