Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions middleware/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ func NewRouter(ctx *Context, next http.Handler) http.Handler {
return
}

// Always use the default producer Content-Type for Method not
// allowed and Not found responses
produces := []string{ctx.api.DefaultProduces()}

// Not found, check if it exists in the other methods first
if others := ctx.AllowedMethods(r); len(others) > 0 {
ctx.Respond(rw, r, ctx.analyzer.RequiredProduces(), nil, errors.MethodNotAllowed(r.Method, others))
ctx.Respond(rw, r, produces, nil, errors.MethodNotAllowed(r.Method, others))
return
}

ctx.Respond(rw, r, ctx.analyzer.RequiredProduces(), nil, errors.NotFound("path %s was not found", r.URL.EscapedPath()))
ctx.Respond(rw, r, produces, nil, errors.NotFound("path %s was not found", r.URL.EscapedPath()))
})
}

Expand Down
2 changes: 2 additions & 0 deletions middleware/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestRouterMiddleware(t *testing.T) {
require.NoError(t, err)

mw.ServeHTTP(recorder, request)
assert.Equal(t, "application/json", recorder.Header().Get("Content-Type"))
assert.Equal(t, http.StatusMethodNotAllowed, recorder.Code)

methods := strings.Split(recorder.Header().Get("Allow"), ",")
Expand All @@ -51,6 +52,7 @@ func TestRouterMiddleware(t *testing.T) {
require.NoError(t, err)

mw.ServeHTTP(recorder, request)
assert.Equal(t, "application/json", recorder.Header().Get("Content-Type"))
assert.Equal(t, http.StatusNotFound, recorder.Code)

recorder = httptest.NewRecorder()
Expand Down
Loading