Return correct Content-Type for unrecognized requests (#3355)

Fixes #3354
This commit is contained in:
Till 2024-04-08 07:51:04 +02:00 committed by GitHub
parent b732eede27
commit 8aa088f713
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 4 deletions

View File

@ -66,15 +66,15 @@ func NewRouters() Routers {
} }
var NotAllowedHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var NotAllowedHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusMethodNotAllowed)
unrecognizedErr, _ := json.Marshal(spec.Unrecognized("Unrecognized request")) // nolint:misspell unrecognizedErr, _ := json.Marshal(spec.Unrecognized("Unrecognized request")) // nolint:misspell
_, _ = w.Write(unrecognizedErr) // nolint:misspell _, _ = w.Write(unrecognizedErr) // nolint:misspell
})) }))
var NotFoundCORSHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var NotFoundCORSHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
unrecognizedErr, _ := json.Marshal(spec.Unrecognized("Unrecognized request")) // nolint:misspell unrecognizedErr, _ := json.Marshal(spec.Unrecognized("Unrecognized request")) // nolint:misspell
_, _ = w.Write(unrecognizedErr) // nolint:misspell _, _ = w.Write(unrecognizedErr) // nolint:misspell
})) }))

View File

@ -17,7 +17,7 @@ func TestRoutersError(t *testing.T) {
if rec.Code != http.StatusNotFound { if rec.Code != http.StatusNotFound {
t.Fatalf("unexpected status code: %d - %s", rec.Code, rec.Body.String()) t.Fatalf("unexpected status code: %d - %s", rec.Code, rec.Body.String())
} }
if ct := rec.Header().Get("Content-Type"); ct != "application/json" { if ct := rec.Result().Header.Get("Content-Type"); ct != "application/json" {
t.Fatalf("unexpected content-type: %s", ct) t.Fatalf("unexpected content-type: %s", ct)
} }
@ -32,7 +32,7 @@ func TestRoutersError(t *testing.T) {
if rec.Code != http.StatusMethodNotAllowed { if rec.Code != http.StatusMethodNotAllowed {
t.Fatalf("unexpected status code: %d - %s", rec.Code, rec.Body.String()) t.Fatalf("unexpected status code: %d - %s", rec.Code, rec.Body.String())
} }
if ct := rec.Header().Get("Content-Type"); ct != "application/json" { if ct := rec.Result().Header.Get("Content-Type"); ct != "application/json" {
t.Fatalf("unexpected content-type: %s", ct) t.Fatalf("unexpected content-type: %s", ct)
} }
} }