diff --git a/test/spec/Feature/Query/CustomMediaSpec.hs b/test/spec/Feature/Query/CustomMediaSpec.hs index 9ee8ec26f8..e0d3d3f1c2 100644 --- a/test/spec/Feature/Query/CustomMediaSpec.hs +++ b/test/spec/Feature/Query/CustomMediaSpec.hs @@ -44,20 +44,20 @@ spec = describe "custom media types" $ do } context "for tables with anyelement aggregate" $ do - it "will override the application/geo+json media type for any table" $ - request methodGet "/lines" (acceptHdrs "application/geo+json") "" + it "will use the application/vnd.geo2+json media type for any table" $ + request methodGet "/lines" (acceptHdrs "application/vnd.geo2+json") "" `shouldRespondWith` "\SOH{\"type\": \"FeatureCollection\", \"hello\": \"world\"}" { matchStatus = 200 - , matchHeaders = ["Content-Type" <:> "application/geo+json; charset=utf-8"] + , matchHeaders = ["Content-Type" <:> "application/vnd.geo2+json"] } - it "will override the application/geo+json media type for any table" $ - request methodGet "/shop_bles" (acceptHdrs "application/geo+json") "" + it "will use the application/vnd.geo2+json media type for any table" $ + request methodGet "/shop_bles" (acceptHdrs "application/vnd.geo2+json") "" `shouldRespondWith` "\SOH{\"type\": \"FeatureCollection\", \"hello\": \"world\"}" { matchStatus = 200 - , matchHeaders = ["Content-Type" <:> "application/geo+json; charset=utf-8"] + , matchHeaders = ["Content-Type" <:> "application/vnd.geo2+json"] } context "Proc that returns scalar" $ do @@ -144,9 +144,26 @@ spec = describe "custom media types" $ do { matchStatus = 415 } it "works if there's an anyelement aggregate defined" $ do - request methodGet "/rpc/get_lines" (acceptHdrs "application/geo+json") "" + request methodGet "/rpc/get_lines" (acceptHdrs "application/vnd.geo2+json") "" `shouldRespondWith` "\SOH{\"type\": \"FeatureCollection\", \"hello\": \"world\"}" { matchStatus = 200 + , matchHeaders = ["Content-Type" <:> "application/vnd.geo2+json"] + } + + context "overriding" $ do + it "will override the application/json handler for a single table" $ + request methodGet "/ov_json" (acceptHdrs "application/json") "" + `shouldRespondWith` + [json| {"overridden": "true"} |] + { matchStatus = 200 + , matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"] + } + + it "will override the application/geo+json handler for a single table" $ + request methodGet "/lines?id=eq.1" (acceptHdrs "application/geo+json") "" + `shouldRespondWith` + "\SOH{\"crs\": {\"type\": \"name\", \"properties\": {\"name\": \"EPSG:4326\"}}, \"type\": \"FeatureCollection\", \"features\": [{\"type\": \"Feature\", \"geometry\": {\"type\": \"LineString\", \"coordinates\": [[1, 1], [5, 5]]}, \"properties\": {\"id\": 1, \"name\": \"line-1\"}}]}" + { matchStatus = 200 , matchHeaders = ["Content-Type" <:> "application/geo+json; charset=utf-8"] } diff --git a/test/spec/fixtures/schema.sql b/test/spec/fixtures/schema.sql index 0bf74a3f9f..69a182b795 100644 --- a/test/spec/fixtures/schema.sql +++ b/test/spec/fixtures/schema.sql @@ -127,6 +127,8 @@ create domain "image/png" as bytea; create domain "application/vnd.twkb" as bytea; create domain "application/openapi+json" as json; create domain "application/geo+json" as jsonb; +create domain "application/vnd.geo2+json" as jsonb; +create domain "application/json" as json; CREATE TABLE items ( id bigserial primary key @@ -3566,20 +3568,63 @@ BEGIN END $do$; -create or replace function test.geoson_trans (state "application/geo+json", next anyelement) -returns "application/geo+json" as $$ - select (state || extensions.ST_AsGeoJSON(next)::jsonb)::"application/geo+json"; +create or replace function test.geoson_trans (state "application/vnd.geo2+json", next anyelement) +returns "application/vnd.geo2+json" as $$ + select (state || extensions.ST_AsGeoJSON(next)::jsonb)::"application/vnd.geo2+json"; $$ language sql; -create or replace function test.geojson_final (data "application/geo+json") -returns "application/geo+json" as $$ - select (jsonb_build_object('type', 'FeatureCollection', 'hello', 'world'))::"application/geo+json"; +create or replace function test.geojson_final (data "application/vnd.geo2+json") +returns "application/vnd.geo2+json" as $$ + select (jsonb_build_object('type', 'FeatureCollection', 'hello', 'world'))::"application/vnd.geo2+json"; $$ language sql; drop aggregate if exists test.geojson_agg(anyelement); create aggregate test.geojson_agg(anyelement) ( initcond = '[]' -, stype = "application/geo+json" +, stype = "application/vnd.geo2+json" , sfunc = geoson_trans , finalfunc = geojson_final ); + +create table ov_json (); + +-- override application/json +create or replace function test.ov_json_trans (state "application/json", next ov_json) +returns "application/json" as $$ + select null; +$$ language sql; + +drop aggregate if exists test.ov_json_agg(ov_json); +create aggregate test.ov_json_agg(ov_json) ( + initcond = '{"overridden": "true"}' +, stype = "application/json" +, sfunc = ov_json_trans +); + +-- override application/geo+json +create or replace function test.lines_geojson_trans (state jsonb, next test.lines) +returns "application/geo+json" as $$ + select (state || extensions.ST_AsGeoJSON(next)::jsonb)::"application/geo+json"; +$$ language sql; + +create or replace function test.lines_geojson_final (data jsonb) +returns "application/geo+json" as $$ + select jsonb_build_object( + 'type', 'FeatureCollection', + 'crs', json_build_object( + 'type', 'name', + 'properties', json_build_object( + 'name', 'EPSG:4326' + ) + ), + 'features', data + )::"application/geo+json"; +$$ language sql; + +drop aggregate if exists test.lines_geojson_agg(test.lines); +create aggregate test.lines_geojson_agg (test.lines) ( + initcond = '[]' +, stype = "application/geo+json" +, sfunc = lines_geojson_trans +, finalfunc = lines_geojson_final +);