Skip to content

Commit

Permalink
fix: Return 406 instead of 415 for non-acceptable media type
Browse files Browse the repository at this point in the history
415 is for Content-Type and 406 for Accept headers.
  • Loading branch information
wolfgangwalther committed May 9, 2024
1 parent c2971e8 commit 8d4468e
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

- #3149, Misleading "Starting PostgREST.." logs on schema cache reloading - @steve-chavez
- #3205, Fix wrong subquery error returning a status of 400 Bad Request - @steve-chavez
- #3224, Return status code 406 for non-accepted media type instead of code 415 - @wolfgangwalther

## [12.0.2] - 2023-12-20

Expand Down
2 changes: 1 addition & 1 deletion src/PostgREST/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ instance PgrstError ApiRequestError where
status AggregatesNotAllowed{} = HTTP.status400
status AmbiguousRelBetween{} = HTTP.status300
status AmbiguousRpc{} = HTTP.status300
status MediaTypeError{} = HTTP.status415
status MediaTypeError{} = HTTP.status406
status InvalidBody{} = HTTP.status400
status InvalidFilters = HTTP.status405
status InvalidPreferences{} = HTTP.status400
Expand Down
8 changes: 4 additions & 4 deletions test/spec/Feature/OpenApi/OpenApiSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ spec actualPgVersion = describe "OpenAPI" $ do
, matchHeaders = ["Content-Type" <:> "application/openapi+json; charset=utf-8"]
}

it "should respond to openapi request on none root path with 415" $
it "should respond to openapi request on none root path with 406" $
request methodGet "/items"
(acceptHdrs "application/openapi+json") ""
`shouldRespondWith` 415
`shouldRespondWith` 406

it "should respond to openapi request with unsupported media type with 415" $
it "should respond to openapi request with unsupported media type with 406" $
request methodGet "/"
(acceptHdrs "text/csv") ""
`shouldRespondWith` 415
`shouldRespondWith` 406

it "includes postgrest.org current version api docs" $ do
r <- simpleBody <$> get "/"
Expand Down
10 changes: 5 additions & 5 deletions test/spec/Feature/Query/CustomMediaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ spec = describe "custom media types" $ do
request methodGet "/lines" (acceptHdrs "text/plain") ""
`shouldRespondWith`
[json| {"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/plain"} |]
{ matchStatus = 415
{ matchStatus = 406
, matchHeaders = [matchContentTypeJson]
}

Expand Down Expand Up @@ -115,7 +115,7 @@ spec = describe "custom media types" $ do
[json|
{"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/xml"}
|]
{ matchStatus = 415
{ matchStatus = 406
, matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"]
}

Expand All @@ -140,7 +140,7 @@ spec = describe "custom media types" $ do
""
`shouldRespondWith`
[json|{"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/plain"}|]
{ matchStatus = 415
{ matchStatus = 406
, matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"]
}

Expand All @@ -156,7 +156,7 @@ spec = describe "custom media types" $ do
(acceptHdrs "application/octet-stream") ""
`shouldRespondWith`
[json| {"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: application/octet-stream"} |]
{ matchStatus = 415 }
{ matchStatus = 406 }

-- TODO SOH (start of heading) is being added to results
it "works if there's an anyelement aggregate defined" $ do
Expand Down Expand Up @@ -279,7 +279,7 @@ spec = describe "custom media types" $ do
}

request methodGet "/rpc/ret_some_mt" (acceptHdrs "text/csv") ""
`shouldRespondWith` 415
`shouldRespondWith` 406

context "on tables" $ do
it "returns application/json for */* if not explicitly set" $ do
Expand Down
6 changes: 3 additions & 3 deletions test/spec/Feature/Query/PlanSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,12 @@ disabledSpec =
it "doesn't work if db-plan-enabled=false(the default)" $ do
request methodGet "/projects?id=in.(1,2,3)"
(acceptHdrs "application/vnd.pgrst.plan") ""
`shouldRespondWith` 415
`shouldRespondWith` 406

request methodGet "/rpc/getallprojects?id=in.(1,2,3)"
(acceptHdrs "application/vnd.pgrst.plan") ""
`shouldRespondWith` 415
`shouldRespondWith` 406

request methodDelete "/projects?id=in.(1,2,3)"
(acceptHdrs "application/vnd.pgrst.plan") ""
`shouldRespondWith` 415
`shouldRespondWith` 406
4 changes: 2 additions & 2 deletions test/spec/Feature/Query/QuerySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -939,12 +939,12 @@ spec actualPgVersion = do
}

describe "Accept headers" $ do
it "should respond an unknown accept type with 415" $
it "should respond an unknown accept type with 406" $
request methodGet "/simple_pk"
(acceptHdrs "text/unknowntype") ""
`shouldRespondWith`
[json|{"message":"None of these media types are available: text/unknowntype","code":"PGRST107","details":null,"hint":null}|]
{ matchStatus = 415
{ matchStatus = 406
, matchHeaders = [matchContentTypeJson]
}

Expand Down
4 changes: 2 additions & 2 deletions test/spec/Feature/Query/RpcSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -656,10 +656,10 @@ spec actualPgVersion =
it "rejects unknown content type even if payload is good" $ do
request methodPost "/rpc/sayhello"
(acceptHdrs "audio/mpeg3") [json| { "name": "world" } |]
`shouldRespondWith` 415
`shouldRespondWith` 406
request methodGet "/rpc/sayhello?name=world"
(acceptHdrs "audio/mpeg3") ""
`shouldRespondWith` 415
`shouldRespondWith` 406
it "rejects malformed json payload" $ do
p <- request methodPost "/rpc/sayhello"
(acceptHdrs "application/json") "sdfsdf"
Expand Down
2 changes: 1 addition & 1 deletion test/spec/fixtures/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3714,7 +3714,7 @@ begin
perform set_config('response.headers', json_build_array(json_build_object('Content-Type', 'app/groucho'))::text, true);
resp := 'groucho';
else
raise sqlstate 'PT415' using message = 'Unsupported Media Type';
raise sqlstate 'PT406' using message = 'Not Acceptable';
end case;
return resp;
end; $$ language plpgsql;
Expand Down

0 comments on commit 8d4468e

Please sign in to comment.