func (s *Server) Handler(w http.ResponseWriter, req *http.Request) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ctx = context.WithValue(ctx, "app.database", s.DB) ctx = context.WithValue(ctx, "app.config", s.Cfg) ctx = context.WithValue(ctx, "http.endpoints", api.Endpoints) mlog.Handler(api.Handler)(ctx, ahttp.NewResponseWriter(w), req) }
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) } }
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) } }
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) } }