fix: Return 406 instead of 415 for non-acceptable media type
415 is for Content-Type and 406 for Accept headers.
This commit is contained in:
committed by
Wolfgang Walther
parent
e4984dc751
commit
491c7a0891
@@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
- #3149, Misleading "Starting PostgREST.." logs on schema cache reloading - @steve-chavez
|
- #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
|
- #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
|
## [12.0.2] - 2023-12-20
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ instance PgrstError ApiRequestError where
|
|||||||
status AggregatesNotAllowed{} = HTTP.status400
|
status AggregatesNotAllowed{} = HTTP.status400
|
||||||
status AmbiguousRelBetween{} = HTTP.status300
|
status AmbiguousRelBetween{} = HTTP.status300
|
||||||
status AmbiguousRpc{} = HTTP.status300
|
status AmbiguousRpc{} = HTTP.status300
|
||||||
status MediaTypeError{} = HTTP.status415
|
status MediaTypeError{} = HTTP.status406
|
||||||
status InvalidBody{} = HTTP.status400
|
status InvalidBody{} = HTTP.status400
|
||||||
status InvalidFilters = HTTP.status405
|
status InvalidFilters = HTTP.status405
|
||||||
status InvalidPreferences{} = HTTP.status400
|
status InvalidPreferences{} = HTTP.status400
|
||||||
|
|||||||
@@ -30,15 +30,15 @@ spec actualPgVersion = describe "OpenAPI" $ do
|
|||||||
, matchHeaders = ["Content-Type" <:> "application/openapi+json; charset=utf-8"]
|
, 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"
|
request methodGet "/items"
|
||||||
(acceptHdrs "application/openapi+json") ""
|
(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 "/"
|
request methodGet "/"
|
||||||
(acceptHdrs "text/csv") ""
|
(acceptHdrs "text/csv") ""
|
||||||
`shouldRespondWith` 415
|
`shouldRespondWith` 406
|
||||||
|
|
||||||
it "includes postgrest.org current version api docs" $ do
|
it "includes postgrest.org current version api docs" $ do
|
||||||
r <- simpleBody <$> get "/"
|
r <- simpleBody <$> get "/"
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ spec = describe "custom media types" $ do
|
|||||||
request methodGet "/lines" (acceptHdrs "text/plain") ""
|
request methodGet "/lines" (acceptHdrs "text/plain") ""
|
||||||
`shouldRespondWith`
|
`shouldRespondWith`
|
||||||
[json| {"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/plain"} |]
|
[json| {"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/plain"} |]
|
||||||
{ matchStatus = 415
|
{ matchStatus = 406
|
||||||
, matchHeaders = [matchContentTypeJson]
|
, matchHeaders = [matchContentTypeJson]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +115,7 @@ spec = describe "custom media types" $ do
|
|||||||
[json|
|
[json|
|
||||||
{"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/xml"}
|
{"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"]
|
, matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,7 +140,7 @@ spec = describe "custom media types" $ do
|
|||||||
""
|
""
|
||||||
`shouldRespondWith`
|
`shouldRespondWith`
|
||||||
[json|{"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/plain"}|]
|
[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"]
|
, matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ spec = describe "custom media types" $ do
|
|||||||
(acceptHdrs "application/octet-stream") ""
|
(acceptHdrs "application/octet-stream") ""
|
||||||
`shouldRespondWith`
|
`shouldRespondWith`
|
||||||
[json| {"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: application/octet-stream"} |]
|
[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
|
-- TODO SOH (start of heading) is being added to results
|
||||||
it "works if there's an anyelement aggregate defined" $ do
|
it "works if there's an anyelement aggregate defined" $ do
|
||||||
@@ -279,7 +279,7 @@ spec = describe "custom media types" $ do
|
|||||||
}
|
}
|
||||||
|
|
||||||
request methodGet "/rpc/ret_some_mt" (acceptHdrs "text/csv") ""
|
request methodGet "/rpc/ret_some_mt" (acceptHdrs "text/csv") ""
|
||||||
`shouldRespondWith` 415
|
`shouldRespondWith` 406
|
||||||
|
|
||||||
context "on tables" $ do
|
context "on tables" $ do
|
||||||
it "returns application/json for */* if not explicitly set" $ do
|
it "returns application/json for */* if not explicitly set" $ do
|
||||||
|
|||||||
@@ -463,12 +463,12 @@ disabledSpec =
|
|||||||
it "doesn't work if db-plan-enabled=false(the default)" $ do
|
it "doesn't work if db-plan-enabled=false(the default)" $ do
|
||||||
request methodGet "/projects?id=in.(1,2,3)"
|
request methodGet "/projects?id=in.(1,2,3)"
|
||||||
(acceptHdrs "application/vnd.pgrst.plan") ""
|
(acceptHdrs "application/vnd.pgrst.plan") ""
|
||||||
`shouldRespondWith` 415
|
`shouldRespondWith` 406
|
||||||
|
|
||||||
request methodGet "/rpc/getallprojects?id=in.(1,2,3)"
|
request methodGet "/rpc/getallprojects?id=in.(1,2,3)"
|
||||||
(acceptHdrs "application/vnd.pgrst.plan") ""
|
(acceptHdrs "application/vnd.pgrst.plan") ""
|
||||||
`shouldRespondWith` 415
|
`shouldRespondWith` 406
|
||||||
|
|
||||||
request methodDelete "/projects?id=in.(1,2,3)"
|
request methodDelete "/projects?id=in.(1,2,3)"
|
||||||
(acceptHdrs "application/vnd.pgrst.plan") ""
|
(acceptHdrs "application/vnd.pgrst.plan") ""
|
||||||
`shouldRespondWith` 415
|
`shouldRespondWith` 406
|
||||||
|
|||||||
@@ -939,12 +939,12 @@ spec actualPgVersion = do
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe "Accept headers" $ 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"
|
request methodGet "/simple_pk"
|
||||||
(acceptHdrs "text/unknowntype") ""
|
(acceptHdrs "text/unknowntype") ""
|
||||||
`shouldRespondWith`
|
`shouldRespondWith`
|
||||||
[json|{"message":"None of these media types are available: text/unknowntype","code":"PGRST107","details":null,"hint":null}|]
|
[json|{"message":"None of these media types are available: text/unknowntype","code":"PGRST107","details":null,"hint":null}|]
|
||||||
{ matchStatus = 415
|
{ matchStatus = 406
|
||||||
, matchHeaders = [matchContentTypeJson]
|
, matchHeaders = [matchContentTypeJson]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -656,10 +656,10 @@ spec actualPgVersion =
|
|||||||
it "rejects unknown content type even if payload is good" $ do
|
it "rejects unknown content type even if payload is good" $ do
|
||||||
request methodPost "/rpc/sayhello"
|
request methodPost "/rpc/sayhello"
|
||||||
(acceptHdrs "audio/mpeg3") [json| { "name": "world" } |]
|
(acceptHdrs "audio/mpeg3") [json| { "name": "world" } |]
|
||||||
`shouldRespondWith` 415
|
`shouldRespondWith` 406
|
||||||
request methodGet "/rpc/sayhello?name=world"
|
request methodGet "/rpc/sayhello?name=world"
|
||||||
(acceptHdrs "audio/mpeg3") ""
|
(acceptHdrs "audio/mpeg3") ""
|
||||||
`shouldRespondWith` 415
|
`shouldRespondWith` 406
|
||||||
it "rejects malformed json payload" $ do
|
it "rejects malformed json payload" $ do
|
||||||
p <- request methodPost "/rpc/sayhello"
|
p <- request methodPost "/rpc/sayhello"
|
||||||
(acceptHdrs "application/json") "sdfsdf"
|
(acceptHdrs "application/json") "sdfsdf"
|
||||||
|
|||||||
Vendored
+1
-1
@@ -3714,7 +3714,7 @@ begin
|
|||||||
perform set_config('response.headers', json_build_array(json_build_object('Content-Type', 'app/groucho'))::text, true);
|
perform set_config('response.headers', json_build_array(json_build_object('Content-Type', 'app/groucho'))::text, true);
|
||||||
resp := 'groucho';
|
resp := 'groucho';
|
||||||
else
|
else
|
||||||
raise sqlstate 'PT415' using message = 'Unsupported Media Type';
|
raise sqlstate 'PT406' using message = 'Not Acceptable';
|
||||||
end case;
|
end case;
|
||||||
return resp;
|
return resp;
|
||||||
end; $$ language plpgsql;
|
end; $$ language plpgsql;
|
||||||
|
|||||||
Reference in New Issue
Block a user