feat: sql handlers for custom media types
* test text/html and drop HtmlRawOutputSpec.hs * all tests passing, removed all pendingWith * make functions compatible with pg <= 12 * move custom media types tests to own spec * anyelement aggregate * apply aggregates without a final function * overriding works * overriding anyelement with particular agg * cannot override vendored media types * plan spec works with custom aggregate * renamed media types to make clear which ones are overridable * correct content negotiation with same weight * text/tab-separated-values media type * text/csv with BOM plus content-disposition header
This commit is contained in:
committed by
Steve Chavez
parent
4a90e9fbd9
commit
82b38341bf
@@ -102,7 +102,7 @@ postJsonArrayTest(){
|
||||
|
||||
echo "Running memory usage tests.."
|
||||
|
||||
jsonKeyTest "1M" "POST" "/rpc/leak?columns=blob" "23M"
|
||||
jsonKeyTest "1M" "POST" "/rpc/leak?columns=blob" "24M"
|
||||
jsonKeyTest "1M" "POST" "/leak?columns=blob" "16M"
|
||||
jsonKeyTest "1M" "PATCH" "/leak?id=eq.1&columns=blob" "16M"
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ spec :: SpecWith ((), Application)
|
||||
spec =
|
||||
describe "root spec function" $ do
|
||||
it "accepts application/openapi+json" $ do
|
||||
pendingWith "TBD"
|
||||
request methodGet "/"
|
||||
[("Accept","application/openapi+json")] "" `shouldRespondWith`
|
||||
[json|{
|
||||
@@ -21,3 +20,12 @@ spec =
|
||||
"info": {"title": "PostgREST API", "description": "This is a dynamic API generated by PostgREST"}
|
||||
}|]
|
||||
{ matchHeaders = ["Content-Type" <:> "application/openapi+json; charset=utf-8"] }
|
||||
|
||||
it "accepts application/json" $ do
|
||||
request methodGet "/"
|
||||
[("Accept","application/json")] "" `shouldRespondWith`
|
||||
[json|{
|
||||
"swagger": "2.0",
|
||||
"info": {"title": "PostgREST API", "description": "This is a dynamic API generated by PostgREST"}
|
||||
}|]
|
||||
{ matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"] }
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
module Feature.Query.CustomMediaSpec where
|
||||
|
||||
import Network.Wai (Application)
|
||||
|
||||
import Network.HTTP.Types
|
||||
import Network.Wai.Test (SResponse (simpleBody, simpleHeaders, simpleStatus))
|
||||
import Test.Hspec
|
||||
import Test.Hspec.Wai
|
||||
import Test.Hspec.Wai.JSON
|
||||
import Text.Heredoc (str)
|
||||
|
||||
import Protolude hiding (get)
|
||||
import SpecHelper
|
||||
|
||||
spec :: SpecWith ((), Application)
|
||||
spec = describe "custom media types" $ do
|
||||
context "for tables with aggregate" $ do
|
||||
it "can query if there's an aggregate defined for the table" $ do
|
||||
r <- request methodGet "/lines" (acceptHdrs "application/vnd.twkb") ""
|
||||
liftIO $ do
|
||||
simpleBody r `shouldBe` readFixtureFile "lines.twkb"
|
||||
simpleHeaders r `shouldContain` [("Content-Type", "application/vnd.twkb")]
|
||||
|
||||
it "can query by id if there's an aggregate defined for the table" $ do
|
||||
r <- request methodGet "/lines?id=eq.1" (acceptHdrs "application/vnd.twkb") ""
|
||||
liftIO $ do
|
||||
simpleBody r `shouldBe` readFixtureFile "1.twkb"
|
||||
simpleHeaders r `shouldContain` [("Content-Type", "application/vnd.twkb")]
|
||||
|
||||
it "will fail if there's no aggregate defined for the table" $ 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
|
||||
, matchHeaders = [matchContentTypeJson]
|
||||
}
|
||||
|
||||
it "can get raw xml output with Accept: text/xml if there's an aggregate defined" $ do
|
||||
request methodGet "/xmltest" (acceptHdrs "text/xml") ""
|
||||
`shouldRespondWith`
|
||||
"<myxml>foo</myxml>bar<foobar><baz/></foobar>"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/xml; charset=utf-8"]
|
||||
}
|
||||
|
||||
-- TODO SOH (start of heading) is being added to results
|
||||
context "for tables with anyelement aggregate" $ do
|
||||
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/vnd.geo2+json"]
|
||||
}
|
||||
|
||||
it "will use the more specific application/vnd.geo2 handler for this table" $ do
|
||||
request methodGet "/shop_bles" (acceptHdrs "application/vnd.geo2+json") ""
|
||||
`shouldRespondWith`
|
||||
"\SOH\"anyelement overridden\""
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "application/vnd.geo2+json"]
|
||||
}
|
||||
|
||||
request methodGet "/rpc/get_shop_bles" (acceptHdrs "application/vnd.geo2+json") ""
|
||||
`shouldRespondWith`
|
||||
"\SOH\"anyelement overridden\""
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "application/vnd.geo2+json"]
|
||||
}
|
||||
|
||||
context "Proc that returns scalar" $ do
|
||||
it "can get raw output with Accept: text/html" $ do
|
||||
request methodGet "/rpc/welcome.html" (acceptHdrs "text/html") ""
|
||||
`shouldRespondWith`
|
||||
[str|
|
||||
|<html>
|
||||
| <head>
|
||||
| <title>PostgREST</title>
|
||||
| </head>
|
||||
| <body>
|
||||
| <h1>Welcome to PostgREST</h1>
|
||||
| </body>
|
||||
|</html>
|
||||
|]
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/html"]
|
||||
}
|
||||
|
||||
it "can get raw output with Accept: text/plain" $ do
|
||||
request methodGet "/rpc/welcome" (acceptHdrs "text/plain") ""
|
||||
`shouldRespondWith` "Welcome to PostgREST"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/plain; charset=utf-8"]
|
||||
}
|
||||
|
||||
it "can get raw xml output with Accept: text/xml" $ do
|
||||
request methodGet "/rpc/return_scalar_xml" (acceptHdrs "text/xml") ""
|
||||
`shouldRespondWith`
|
||||
"<my-xml-tag/>"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/xml; charset=utf-8"]
|
||||
}
|
||||
|
||||
it "can get raw xml output with Accept: text/xml" $ do
|
||||
request methodGet "/rpc/welcome.xml" (acceptHdrs "text/xml") ""
|
||||
`shouldRespondWith`
|
||||
"<html>\n <head>\n <title>PostgREST</title>\n </head>\n <body>\n <h1>Welcome to PostgREST</h1>\n </body>\n</html>"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/xml; charset=utf-8"]
|
||||
}
|
||||
|
||||
it "should fail with function returning text and Accept: text/xml" $ do
|
||||
request methodGet "/rpc/welcome" (acceptHdrs "text/xml") ""
|
||||
`shouldRespondWith`
|
||||
[json|
|
||||
{"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/xml"}
|
||||
|]
|
||||
{ matchStatus = 415
|
||||
, matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"]
|
||||
}
|
||||
|
||||
context "Proc that returns scalar based on a table" $ do
|
||||
it "can get an image with Accept: image/png" $ do
|
||||
r <- request methodGet "/rpc/ret_image" (acceptHdrs "image/png") ""
|
||||
liftIO $ do
|
||||
simpleBody r `shouldBe` readFixtureFile "A.png"
|
||||
simpleHeaders r `shouldContain` [("Content-Type", "image/png")]
|
||||
|
||||
context "Proc that returns set of scalars and Accept: text/plain" $
|
||||
it "will err because only scalars work with media type domains" $ do
|
||||
request methodGet "/rpc/welcome_twice"
|
||||
(acceptHdrs "text/plain")
|
||||
""
|
||||
`shouldRespondWith`
|
||||
[json|{"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/plain"}|]
|
||||
{ matchStatus = 415
|
||||
, matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"]
|
||||
}
|
||||
|
||||
context "Proc that returns rows and accepts custom media type" $ do
|
||||
it "works if it has an aggregate defined" $ do
|
||||
r <- request methodGet "/rpc/get_lines" [("Accept", "application/vnd.twkb")] ""
|
||||
liftIO $ do
|
||||
simpleBody r `shouldBe` readFixtureFile "lines.twkb"
|
||||
simpleHeaders r `shouldContain` [("Content-Type", "application/vnd.twkb")]
|
||||
|
||||
it "fails if doesn't have an aggregate defined" $ do
|
||||
request methodGet "/rpc/get_lines"
|
||||
(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 }
|
||||
|
||||
-- TODO SOH (start of heading) is being added to results
|
||||
it "works if there's an anyelement aggregate defined" $ do
|
||||
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"]
|
||||
}
|
||||
|
||||
-- TODO SOH (start of heading) is being added to results
|
||||
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"]
|
||||
}
|
||||
|
||||
it "will not override vendored media types like application/vnd.pgrst.object" $
|
||||
request methodGet "/projects?id=eq.1" (acceptHdrs "application/vnd.pgrst.object") ""
|
||||
`shouldRespondWith`
|
||||
[json|{"id":1,"name":"Windows 7","client_id":1}|]
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "application/vnd.pgrst.object+json; charset=utf-8"]
|
||||
}
|
||||
|
||||
context "matches requested media type correctly" $ do
|
||||
-- https://github.com/PostgREST/postgrest/issues/1462
|
||||
it "will match image/png according to q values" $ do
|
||||
r1 <- request methodGet "/rpc/ret_image" (acceptHdrs "image/png, */*") ""
|
||||
liftIO $ do
|
||||
simpleBody r1 `shouldBe` readFixtureFile "A.png"
|
||||
simpleHeaders r1 `shouldContain` [("Content-Type", "image/png")]
|
||||
|
||||
r2 <- request methodGet "/rpc/ret_image" (acceptHdrs "text/html,application/xhtml+xml,application/xml;q=0.9,image/png,*/*;q=0.8") ""
|
||||
liftIO $ do
|
||||
simpleBody r2 `shouldBe` readFixtureFile "A.png"
|
||||
simpleHeaders r2 `shouldContain` [("Content-Type", "image/png")]
|
||||
|
||||
-- https://github.com/PostgREST/postgrest/issues/2170
|
||||
it "will match json in presence of text/plain" $ do
|
||||
r <- request methodGet "/projects?id=eq.1" (acceptHdrs "text/plain, application/json") ""
|
||||
liftIO $ do
|
||||
simpleStatus r `shouldBe` status200
|
||||
simpleHeaders r `shouldContain` [("Content-Type", "application/json; charset=utf-8")]
|
||||
|
||||
-- https://github.com/PostgREST/postgrest/issues/1102
|
||||
it "will match a custom text/tab-separated-values" $ do
|
||||
request methodGet "/projects?id=in.(1,2)" (acceptHdrs "text/tab-separated-values") ""
|
||||
`shouldRespondWith`
|
||||
"id\tname\tclient_id\n1\tWindows 7\t1\n2\tWindows 10\t1\n"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/tab-separated-values"]
|
||||
}
|
||||
|
||||
-- https://github.com/PostgREST/postgrest/issues/1371#issuecomment-519248984
|
||||
it "will match a custom text/csv with BOM" $ do
|
||||
r <- request methodGet "/lines" (acceptHdrs "text/csv") ""
|
||||
liftIO $ do
|
||||
simpleBody r `shouldBe` readFixtureFile "lines.csv"
|
||||
simpleHeaders r `shouldContain` [("Content-Type", "text/csv; charset=utf-8")]
|
||||
simpleHeaders r `shouldContain` [("Content-Disposition", "attachment; filename=\"lines.csv\"")]
|
||||
@@ -1,31 +0,0 @@
|
||||
module Feature.Query.HtmlRawOutputSpec where
|
||||
|
||||
import Network.Wai (Application)
|
||||
|
||||
import Network.HTTP.Types
|
||||
import Test.Hspec hiding (pendingWith)
|
||||
import Test.Hspec.Wai
|
||||
import Text.Heredoc
|
||||
|
||||
import Protolude hiding (get)
|
||||
import SpecHelper (acceptHdrs)
|
||||
|
||||
spec :: SpecWith ((), Application)
|
||||
spec = describe "When raw-media-types is set to \"text/html\"" $
|
||||
it "can get raw output with Accept: text/html" $ do
|
||||
pendingWith "TBD"
|
||||
request methodGet "/rpc/welcome.html" (acceptHdrs "text/html") ""
|
||||
`shouldRespondWith`
|
||||
[str|
|
||||
|<html>
|
||||
| <head>
|
||||
| <title>PostgREST</title>
|
||||
| </head>
|
||||
| <body>
|
||||
| <h1>Welcome to PostgREST</h1>
|
||||
| </body>
|
||||
|</html>
|
||||
|]
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/html"]
|
||||
}
|
||||
@@ -275,18 +275,6 @@ spec actualPgVersion = do
|
||||
resStatus `shouldBe` Status { statusCode = 200, statusMessage="OK" }
|
||||
totalCost `shouldBe` 68.56
|
||||
|
||||
it "outputs the plan for text/xml" $ do
|
||||
pendingWith "TBD"
|
||||
r <- request methodGet "/rpc/return_scalar_xml"
|
||||
(acceptHdrs "application/vnd.pgrst.plan+json; for=\"text/xml\"; options=verbose") ""
|
||||
|
||||
let aggCol = simpleBody r ^? nth 0 . key "Plan" . key "Output" . nth 2
|
||||
resHeaders = simpleHeaders r
|
||||
|
||||
liftIO $ do
|
||||
resHeaders `shouldSatisfy` elem ("Content-Type", "application/vnd.pgrst.plan+json; for=\"text/xml\"; options=verbose; charset=utf-8")
|
||||
aggCol `shouldBe` Just [aesonQQ| "COALESCE(xmlagg(return_scalar_xml.pgrst_scalar), ''::xml)" |]
|
||||
|
||||
describe "text format" $ do
|
||||
it "outputs the total cost for a function call" $ do
|
||||
r <- request methodGet "/projects?id=in.(1,2,3)"
|
||||
@@ -442,6 +430,34 @@ spec actualPgVersion = do
|
||||
liftIO $ do
|
||||
resBody `shouldSatisfy` (\t -> T.isInfixOf "Index" (decodeUtf8 $ LBS.toStrict t))
|
||||
|
||||
describe "custom media types" $ do
|
||||
it "outputs the plan for a scalar function text/xml" $ do
|
||||
r <- request methodGet "/rpc/return_scalar_xml"
|
||||
(acceptHdrs "application/vnd.pgrst.plan+json; for=\"text/xml\"; options=verbose") ""
|
||||
|
||||
let aggCol = simpleBody r ^? nth 0 . key "Plan" . key "Output" . nth 2
|
||||
resHeaders = simpleHeaders r
|
||||
|
||||
liftIO $ do
|
||||
resHeaders `shouldSatisfy` elem ("Content-Type", "application/vnd.pgrst.plan+json; for=\"text/xml\"; options=verbose; charset=utf-8")
|
||||
aggCol `shouldBe` Just [aesonQQ| "return_scalar_xml.pgrst_scalar" |]
|
||||
|
||||
it "outputs the plan for an aggregate application/vnd.twkb" $ do
|
||||
r <- request methodGet "/lines"
|
||||
(acceptHdrs "application/vnd.pgrst.plan+json; for=\"application/vnd.twkb\"; options=verbose") ""
|
||||
|
||||
let aggCol = simpleBody r ^? nth 0 . key "Plan" . key "Output" . nth 2
|
||||
resHeaders = simpleHeaders r
|
||||
|
||||
liftIO $ do
|
||||
resHeaders `shouldSatisfy` elem ("Content-Type", "application/vnd.pgrst.plan+json; for=\"application/vnd.twkb\"; options=verbose; charset=utf-8")
|
||||
aggCol `shouldBe`
|
||||
(
|
||||
if actualPgVersion >= pgVersion120
|
||||
then Just [aesonQQ| "twkb_agg(ROW(lines.id, lines.name, lines.geom)::lines)" |]
|
||||
else Just [aesonQQ| "twkb_agg(ROW(pgrst_source.id, pgrst_source.name, pgrst_source.geom)::lines)" |]
|
||||
)
|
||||
|
||||
disabledSpec :: SpecWith ((), Application)
|
||||
disabledSpec =
|
||||
it "doesn't work if db-plan-enabled=false(the default)" $ do
|
||||
|
||||
@@ -1042,61 +1042,6 @@ spec actualPgVersion = do
|
||||
[json|[{"a$num$":100}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
context "binary output" $ do
|
||||
it "can query if a single column is selected" $ do
|
||||
pendingWith "TBD"
|
||||
request methodGet "/images_base64?select=img&name=eq.A.png" (acceptHdrs "application/octet-stream") ""
|
||||
`shouldRespondWith` "iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeAQMAAAAB/jzhAAAABlBMVEUAAAD/AAAb/40iAAAAP0lEQVQI12NgwAbYG2AE/wEYwQMiZB4ACQkQYZEAIgqAhAGIKLCAEQ8kgMT/P1CCEUwc4IMSzA3sUIIdCHECAGSQEkeOTUyCAAAAAElFTkSuQmCC"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "application/octet-stream"]
|
||||
}
|
||||
|
||||
it "can get raw output with Accept: text/plain" $ do
|
||||
pendingWith "TBD"
|
||||
request methodGet "/projects?select=name&id=eq.1" (acceptHdrs "text/plain") ""
|
||||
`shouldRespondWith` "Windows 7"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/plain; charset=utf-8"]
|
||||
}
|
||||
|
||||
it "can get raw xml output with Accept: text/xml" $ do
|
||||
pendingWith "TBD"
|
||||
request methodGet "/xmltest?select=xml" (acceptHdrs "text/xml") ""
|
||||
`shouldRespondWith`
|
||||
"<myxml>foo</myxml>bar<foobar><baz/></foobar>"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/xml; charset=utf-8"]
|
||||
}
|
||||
|
||||
it "fails if a single column is not selected" $ do
|
||||
pendingWith "TBD"
|
||||
request methodGet "/images?select=img,name&name=eq.A.png" (acceptHdrs "application/octet-stream") ""
|
||||
`shouldRespondWith`
|
||||
[json| {"message":"application/octet-stream requested but more than one column was selected","code":"PGRST113","details":null,"hint":null} |]
|
||||
{ matchStatus = 406 }
|
||||
|
||||
request methodGet "/images?select=*&name=eq.A.png"
|
||||
(acceptHdrs "application/octet-stream")
|
||||
""
|
||||
`shouldRespondWith`
|
||||
[json| {"message":"application/octet-stream requested but more than one column was selected","code":"PGRST113","details":null,"hint":null} |]
|
||||
{ matchStatus = 406 }
|
||||
|
||||
request methodGet "/images?name=eq.A.png"
|
||||
(acceptHdrs "application/octet-stream")
|
||||
""
|
||||
`shouldRespondWith`
|
||||
[json| {"message":"application/octet-stream requested but more than one column was selected","code":"PGRST113","details":null,"hint":null} |]
|
||||
{ matchStatus = 406 }
|
||||
|
||||
it "concatenates results if more than one row is returned" $ do
|
||||
pendingWith "TBD"
|
||||
request methodGet "/images_base64?select=img&name=in.(A.png,B.png)" (acceptHdrs "application/octet-stream") ""
|
||||
`shouldRespondWith` "iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeAQMAAAAB/jzhAAAABlBMVEUAAAD/AAAb/40iAAAAP0lEQVQI12NgwAbYG2AE/wEYwQMiZB4ACQkQYZEAIgqAhAGIKLCAEQ8kgMT/P1CCEUwc4IMSzA3sUIIdCHECAGSQEkeOTUyCAAAAAElFTkSuQmCCiVBORw0KGgoAAAANSUhEUgAAAB4AAAAeAQMAAAAB/jzhAAAABlBMVEX///8AAP94wDzzAAAAL0lEQVQIW2NgwAb+HwARH0DEDyDxwAZEyGAhLODqHmBRzAcn5GAS///A1IF14AAA5/Adbiiz/0gAAAAASUVORK5CYII="
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "application/octet-stream"]
|
||||
}
|
||||
|
||||
describe "values with quotes in IN and NOT IN" $ do
|
||||
it "succeeds when only quoted values are present" $ do
|
||||
get "/w_or_wo_comma_names?name=in.(\"Hebdon, John\")" `shouldRespondWith`
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
module Feature.Query.RpcSpec where
|
||||
|
||||
import qualified Data.ByteString.Lazy as BL (empty, readFile)
|
||||
import qualified Data.ByteString.Lazy as BL (empty)
|
||||
|
||||
import Network.Wai (Application)
|
||||
import Network.Wai.Test (SResponse (simpleBody, simpleHeaders, simpleStatus))
|
||||
|
||||
import Network.HTTP.Types
|
||||
import System.IO.Unsafe (unsafePerformIO)
|
||||
import Test.Hspec hiding (pendingWith)
|
||||
import Test.Hspec.Wai
|
||||
import Test.Hspec.Wai.JSON
|
||||
@@ -1077,87 +1076,6 @@ spec actualPgVersion =
|
||||
, matchHeaders = []
|
||||
}
|
||||
|
||||
context "binary output" $ do
|
||||
context "Proc that returns scalar" $ do
|
||||
it "can query without selecting column" $ do
|
||||
pendingWith "TBD"
|
||||
request methodPost "/rpc/ret_base64_bin" (acceptHdrs "application/octet-stream") ""
|
||||
`shouldRespondWith` "iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeAQMAAAAB/jzhAAAABlBMVEUAAAD/AAAb/40iAAAAP0lEQVQI12NgwAbYG2AE/wEYwQMiZB4ACQkQYZEAIgqAhAGIKLCAEQ8kgMT/P1CCEUwc4IMSzA3sUIIdCHECAGSQEkeOTUyCAAAAAElFTkSuQmCC"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "application/octet-stream"]
|
||||
}
|
||||
|
||||
it "can get raw output with Accept: text/plain" $ do
|
||||
pendingWith "TBD"
|
||||
request methodGet "/rpc/welcome" (acceptHdrs "text/plain") ""
|
||||
`shouldRespondWith` "Welcome to PostgREST"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/plain; charset=utf-8"]
|
||||
}
|
||||
|
||||
it "can get raw xml output with Accept: text/xml" $ do
|
||||
pendingWith "TBD"
|
||||
request methodGet "/rpc/return_scalar_xml" (acceptHdrs "text/xml") ""
|
||||
`shouldRespondWith`
|
||||
"<my-xml-tag/>"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/xml; charset=utf-8"]
|
||||
}
|
||||
|
||||
it "can get raw xml output with Accept: text/xml" $ do
|
||||
pendingWith "TBD"
|
||||
request methodGet "/rpc/welcome.xml" (acceptHdrs "text/xml") ""
|
||||
`shouldRespondWith`
|
||||
"<html>\n <head>\n <title>PostgREST</title>\n </head>\n <body>\n <h1>Welcome to PostgREST</h1>\n </body>\n</html>"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/xml; charset=utf-8"]
|
||||
}
|
||||
|
||||
it "should fail with function returning text and Accept: text/xml" $ do
|
||||
pendingWith "TBD"
|
||||
request methodGet "/rpc/welcome" (acceptHdrs "text/xml") ""
|
||||
`shouldRespondWith`
|
||||
[json|
|
||||
{
|
||||
"hint":"No function matches the given name and argument types. You might need to add explicit type casts.",
|
||||
"details":null,
|
||||
"code":"42883",
|
||||
"message":"function xmlagg(text) does not exist"
|
||||
}
|
||||
|]
|
||||
{ matchStatus = 406
|
||||
, matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"]
|
||||
}
|
||||
|
||||
context "Proc that returns set of scalars" $
|
||||
it "can query without selecting column" $ do
|
||||
pendingWith "TBD"
|
||||
request methodGet "/rpc/welcome_twice"
|
||||
(acceptHdrs "text/plain")
|
||||
""
|
||||
`shouldRespondWith`
|
||||
"Welcome to PostgRESTWelcome to PostgREST"
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "text/plain; charset=utf-8"]
|
||||
}
|
||||
|
||||
context "Proc that returns rows" $ do
|
||||
it "can query if a single column is selected" $ do
|
||||
pendingWith "TBD"
|
||||
request methodPost "/rpc/ret_rows_with_base64_bin?select=img" (acceptHdrs "application/octet-stream") ""
|
||||
`shouldRespondWith` "iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeAQMAAAAB/jzhAAAABlBMVEUAAAD/AAAb/40iAAAAP0lEQVQI12NgwAbYG2AE/wEYwQMiZB4ACQkQYZEAIgqAhAGIKLCAEQ8kgMT/P1CCEUwc4IMSzA3sUIIdCHECAGSQEkeOTUyCAAAAAElFTkSuQmCCiVBORw0KGgoAAAANSUhEUgAAAB4AAAAeAQMAAAAB/jzhAAAABlBMVEX///8AAP94wDzzAAAAL0lEQVQIW2NgwAb+HwARH0DEDyDxwAZEyGAhLODqHmBRzAcn5GAS///A1IF14AAA5/Adbiiz/0gAAAAASUVORK5CYII="
|
||||
{ matchStatus = 200
|
||||
, matchHeaders = ["Content-Type" <:> "application/octet-stream"]
|
||||
}
|
||||
|
||||
it "fails if a single column is not selected" $ do
|
||||
pendingWith "TBD"
|
||||
request methodPost "/rpc/ret_rows_with_base64_bin"
|
||||
(acceptHdrs "application/octet-stream") ""
|
||||
`shouldRespondWith`
|
||||
[json| {"message":"application/octet-stream requested but more than one column was selected","code":"PGRST113","details":null,"hint":null} |]
|
||||
{ matchStatus = 406 }
|
||||
|
||||
context "only for GET rpc" $ do
|
||||
it "should fail on mutating procs" $ do
|
||||
get "/rpc/callcounter" `shouldRespondWith` 405
|
||||
@@ -1327,7 +1245,6 @@ spec actualPgVersion =
|
||||
[json|{"A": 1, "B": 2, "C": 3}|]
|
||||
|
||||
it "can insert text directly" $ do
|
||||
pendingWith "TBD"
|
||||
request methodPost "/rpc/unnamed_text_param"
|
||||
[("Content-Type", "text/plain"), ("Accept", "text/plain")]
|
||||
[str|unnamed text arg|]
|
||||
@@ -1335,7 +1252,6 @@ spec actualPgVersion =
|
||||
[str|unnamed text arg|]
|
||||
|
||||
it "can insert xml directly" $ do
|
||||
pendingWith "TBD"
|
||||
request methodPost "/rpc/unnamed_xml_param"
|
||||
[("Content-Type", "text/xml"), ("Accept", "text/xml")]
|
||||
[str|<note><from>John</from><to>Jane</to><message>Remember me</message></note>|]
|
||||
@@ -1343,8 +1259,7 @@ spec actualPgVersion =
|
||||
[str|<note><from>John</from><to>Jane</to><message>Remember me</message></note>|]
|
||||
|
||||
it "can insert bytea directly" $ do
|
||||
pendingWith "TBD"
|
||||
let file = unsafePerformIO $ BL.readFile "test/spec/fixtures/image.png"
|
||||
let file = readFixtureFile "image.png"
|
||||
r <- request methodPost "/rpc/unnamed_bytea_param"
|
||||
[("Content-Type", "application/octet-stream"), ("Accept", "application/octet-stream")]
|
||||
file
|
||||
@@ -1397,10 +1312,9 @@ spec actualPgVersion =
|
||||
}
|
||||
|
||||
it "will err when no function with single unnamed bytea parameter exists and application/octet-stream is specified" $
|
||||
let file = unsafePerformIO $ BL.readFile "test/spec/fixtures/image.png" in
|
||||
request methodPost "/rpc/unnamed_int_param"
|
||||
[("Content-Type", "application/octet-stream")]
|
||||
file
|
||||
(readFixtureFile "image.png")
|
||||
`shouldRespondWith`
|
||||
[json|{
|
||||
"hint": null,
|
||||
@@ -1429,7 +1343,6 @@ spec actualPgVersion =
|
||||
}
|
||||
|
||||
it "should be able to fallback to the single unnamed parameter function when other overloaded functions are not found" $ do
|
||||
pendingWith "TBD"
|
||||
request methodPost "/rpc/overloaded_unnamed_param"
|
||||
[("Content-Type", "application/json")]
|
||||
[json|{"A": 1, "B": 2, "C": 3}|]
|
||||
@@ -1440,7 +1353,7 @@ spec actualPgVersion =
|
||||
[str|unnamed text arg|]
|
||||
`shouldRespondWith`
|
||||
[str|unnamed text arg|]
|
||||
let file = unsafePerformIO $ BL.readFile "test/spec/fixtures/image.png"
|
||||
let file = readFixtureFile "image.png"
|
||||
r <- request methodPost "/rpc/overloaded_unnamed_param"
|
||||
[("Content-Type", "application/octet-stream"), ("Accept", "application/octet-stream")]
|
||||
file
|
||||
@@ -1488,7 +1401,6 @@ spec actualPgVersion =
|
||||
}
|
||||
|
||||
it "should fail on /rpc/unnamed_xml_param when posting invalid xml" $ do
|
||||
pendingWith "TBD"
|
||||
request methodPost "/rpc/unnamed_xml_param"
|
||||
[("Content-Type", "text/xml"), ("Accept", "text/xml")]
|
||||
[str|<|]
|
||||
|
||||
+2
-2
@@ -37,11 +37,11 @@ import qualified Feature.OpenApi.SecurityOpenApiSpec
|
||||
import qualified Feature.OptionsSpec
|
||||
import qualified Feature.Query.AndOrParamsSpec
|
||||
import qualified Feature.Query.ComputedRelsSpec
|
||||
import qualified Feature.Query.CustomMediaSpec
|
||||
import qualified Feature.Query.DeleteSpec
|
||||
import qualified Feature.Query.EmbedDisambiguationSpec
|
||||
import qualified Feature.Query.EmbedInnerJoinSpec
|
||||
import qualified Feature.Query.ErrorSpec
|
||||
import qualified Feature.Query.HtmlRawOutputSpec
|
||||
import qualified Feature.Query.InsertSpec
|
||||
import qualified Feature.Query.JsonOperatorSpec
|
||||
import qualified Feature.Query.MultipleSchemaSpec
|
||||
@@ -129,6 +129,7 @@ main = do
|
||||
, ("Feature.Auth.AuthSpec" , Feature.Auth.AuthSpec.spec actualPgVersion)
|
||||
, ("Feature.ConcurrentSpec" , Feature.ConcurrentSpec.spec)
|
||||
, ("Feature.CorsSpec" , Feature.CorsSpec.spec)
|
||||
, ("Feature.CustomMediaSpec" , Feature.Query.CustomMediaSpec.spec)
|
||||
, ("Feature.Query.DeleteSpec" , Feature.Query.DeleteSpec.spec)
|
||||
, ("Feature.Query.EmbedDisambiguationSpec" , Feature.Query.EmbedDisambiguationSpec.spec)
|
||||
, ("Feature.Query.EmbedInnerJoinSpec" , Feature.Query.EmbedInnerJoinSpec.spec)
|
||||
@@ -149,7 +150,6 @@ main = do
|
||||
, ("Feature.Query.ComputedRelsSpec" , Feature.Query.ComputedRelsSpec.spec)
|
||||
, ("Feature.Query.RelatedQueriesSpec" , Feature.Query.RelatedQueriesSpec.spec)
|
||||
, ("Feature.Query.SpreadQueriesSpec" , Feature.Query.SpreadQueriesSpec.spec)
|
||||
, ("Feature.Query.HtmlRawOutputSpec" , Feature.Query.HtmlRawOutputSpec.spec)
|
||||
, ("Feature.NoSuperuserSpec" , Feature.NoSuperuserSpec.spec)
|
||||
]
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import Data.CaseInsensitive (CI (..), mk, original)
|
||||
import Data.List (lookup)
|
||||
import Data.List.NonEmpty (fromList)
|
||||
import Network.Wai.Test (SResponse (simpleBody, simpleHeaders, simpleStatus))
|
||||
import System.IO.Unsafe (unsafePerformIO)
|
||||
import System.Process (readProcess)
|
||||
import Text.Regex.TDFA ((=~))
|
||||
|
||||
@@ -340,3 +341,6 @@ getInsertDataForTiobePlsTable rows =
|
||||
JSON.encode $ fromList $ [TiobePlsRow {name' = nm, rank = rk} | (nm,rk) <- nameRankList]
|
||||
where
|
||||
nameRankList = [("Lang " <> show i, i) | i <- [20..(rows+20)] ] :: [(Text, Int)]
|
||||
|
||||
readFixtureFile :: FilePath -> BL.ByteString
|
||||
readFixtureFile file = unsafePerformIO $ BL.readFile $ "test/spec/fixtures/" <> file
|
||||
|
||||
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
|
After Width: | Height: | Size: 138 B |
Vendored
+3
@@ -858,3 +858,6 @@ TRUNCATE TABLE table_a CASCADE;
|
||||
INSERT INTO table_a(id, name) VALUES (1, 'Not null 1'), (2, null), (3, 'Not null 2');
|
||||
TRUNCATE TABLE table_b CASCADE;
|
||||
INSERT INTO table_b(table_a_id, name) VALUES (1, 'Test 1'), (2, 'Test 2'), (null, 'Test 3');
|
||||
|
||||
TRUNCATE TABLE lines CASCADE;
|
||||
insert into lines values (1, 'line-1', 'LINESTRING(1 1,5 5)'::extensions.geometry), (2, 'line-2', 'LINESTRING(2 2,6 6)'::extensions.geometry);
|
||||
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
id,name,geom
|
||||
1,line-1,0102000020E610000002000000000000000000F03F000000000000F03F00000000000014400000000000001440
|
||||
2,line-2,0102000020E6100000020000000000000000000040000000000000004000000000000018400000000000001840
|
||||
|
Vendored
BIN
Binary file not shown.
Vendored
+211
-26
@@ -62,6 +62,7 @@ CREATE TYPE enum_menagerie_type AS ENUM (
|
||||
'bar'
|
||||
);
|
||||
|
||||
create type bit as enum ('one', 'two');
|
||||
|
||||
SET search_path = postgrest, pg_catalog;
|
||||
|
||||
@@ -119,6 +120,20 @@ SET default_tablespace = '';
|
||||
|
||||
SET default_with_oids = false;
|
||||
|
||||
create domain "text/plain" as text;
|
||||
create domain "text/html" as text;
|
||||
create domain "text/xml" as pg_catalog.xml;
|
||||
create domain "application/octet-stream" as bytea;
|
||||
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 domain "application/vnd.pgrst.object" as json;
|
||||
create domain "text/tab-separated-values" as text;
|
||||
create domain "text/csv" as text;
|
||||
|
||||
CREATE TABLE items (
|
||||
id bigserial primary key
|
||||
);
|
||||
@@ -1160,12 +1175,8 @@ create or replace function test.ret_null() returns int as $$
|
||||
select null::int;
|
||||
$$ language sql;
|
||||
|
||||
create function test.ret_base64_bin() returns text as $$
|
||||
select i.img from test.images_base64 i where i.name = 'A.png';
|
||||
$$ language sql;
|
||||
|
||||
create function test.ret_rows_with_base64_bin() returns setof test.images_base64 as $$
|
||||
select i.name, i.img from test.images_base64 i;
|
||||
create function test.ret_image() returns "image/png" as $$
|
||||
select i.img::"image/png" from test.images i where i.name = 'A.png';
|
||||
$$ language sql;
|
||||
|
||||
create function test.single_article(id integer) returns test.articles as $$
|
||||
@@ -1896,7 +1907,7 @@ returns integer as $$
|
||||
select a + b;
|
||||
$$ language sql;
|
||||
|
||||
create or replace function root() returns json as $_$
|
||||
create or replace function root() returns "application/openapi+json" as $_$
|
||||
declare
|
||||
openapi json = $$
|
||||
{
|
||||
@@ -1912,17 +1923,17 @@ begin
|
||||
end
|
||||
$_$ language plpgsql;
|
||||
|
||||
create or replace function welcome() returns text as $$
|
||||
select 'Welcome to PostgREST'::text;
|
||||
create or replace function welcome() returns "text/plain" as $$
|
||||
select 'Welcome to PostgREST'::"text/plain";
|
||||
$$ language sql;
|
||||
|
||||
create or replace function welcome_twice() returns setof text as $$
|
||||
create or replace function welcome_twice() returns setof "text/plain" as $$
|
||||
select 'Welcome to PostgREST'
|
||||
union all
|
||||
select 'Welcome to PostgREST';
|
||||
$$ language sql;
|
||||
|
||||
create or replace function "welcome.html"() returns text as $_$
|
||||
create or replace function "welcome.html"() returns "text/html" as $_$
|
||||
select $$
|
||||
<html>
|
||||
<head>
|
||||
@@ -1932,7 +1943,7 @@ select $$
|
||||
<h1>Welcome to PostgREST</h1>
|
||||
</body>
|
||||
</html>
|
||||
$$::text;
|
||||
$$::"text/html";
|
||||
$_$ language sql;
|
||||
|
||||
create view getallprojects_view as
|
||||
@@ -2370,16 +2381,16 @@ create or replace function test.unnamed_json_param(json) returns json as $$
|
||||
select $1;
|
||||
$$ language sql;
|
||||
|
||||
create or replace function test.unnamed_text_param(text) returns text as $$
|
||||
select $1;
|
||||
create or replace function test.unnamed_text_param(text) returns "text/plain" as $$
|
||||
select $1::"text/plain";
|
||||
$$ language sql;
|
||||
|
||||
create or replace function test.unnamed_xml_param(pg_catalog.xml) returns pg_catalog.xml as $$
|
||||
select $1;
|
||||
create or replace function test.unnamed_xml_param(pg_catalog.xml) returns "text/xml" as $$
|
||||
select $1::"text/xml";
|
||||
$$ language sql;
|
||||
|
||||
create or replace function test.unnamed_bytea_param(bytea) returns bytea as $$
|
||||
select $1::bytea;
|
||||
create or replace function test.unnamed_bytea_param(bytea) returns "application/octet-stream" as $$
|
||||
select $1::"application/octet-stream";
|
||||
$$ language sql;
|
||||
|
||||
create or replace function test.unnamed_int_param(int) returns int as $$
|
||||
@@ -2390,12 +2401,12 @@ create or replace function test.overloaded_unnamed_param(json) returns json as $
|
||||
select $1;
|
||||
$$ language sql;
|
||||
|
||||
create or replace function test.overloaded_unnamed_param(bytea) returns bytea as $$
|
||||
select $1;
|
||||
create or replace function test.overloaded_unnamed_param(bytea) returns "application/octet-stream" as $$
|
||||
select $1::"application/octet-stream";
|
||||
$$ language sql;
|
||||
|
||||
create or replace function test.overloaded_unnamed_param(text) returns text as $$
|
||||
select $1;
|
||||
create or replace function test.overloaded_unnamed_param(text) returns "text/plain" as $$
|
||||
select $1::"text/plain";
|
||||
$$ language sql;
|
||||
|
||||
create or replace function test.overloaded_unnamed_param() returns int as $$
|
||||
@@ -2631,12 +2642,12 @@ create table plate_plan_step (
|
||||
REFERENCES well(well_id)
|
||||
);
|
||||
|
||||
CREATE FUNCTION test.return_scalar_xml() RETURNS pg_catalog.xml
|
||||
CREATE FUNCTION test.return_scalar_xml() RETURNS "text/xml"
|
||||
LANGUAGE sql AS $$
|
||||
SELECT '<my-xml-tag/>'::pg_catalog.xml
|
||||
SELECT '<my-xml-tag/>'::"text/xml"
|
||||
$$;
|
||||
|
||||
CREATE OR REPLACE FUNCTION "welcome.xml"() RETURNS pg_catalog.xml
|
||||
CREATE OR REPLACE FUNCTION "welcome.xml"() RETURNS "text/xml"
|
||||
LANGUAGE sql AS $_$
|
||||
select $$
|
||||
<html>
|
||||
@@ -2646,7 +2657,7 @@ select $$
|
||||
<body>
|
||||
<h1>Welcome to PostgREST</h1>
|
||||
</body>
|
||||
</html>$$::pg_catalog.xml;
|
||||
</html>$$::"text/xml";
|
||||
$_$;
|
||||
|
||||
CREATE TABLE test.xmltest (
|
||||
@@ -2654,6 +2665,23 @@ CREATE TABLE test.xmltest (
|
||||
xml pg_catalog.xml NOT NULL
|
||||
);
|
||||
|
||||
create or replace function test.xml_handler_transition (state "text/xml", next test.xmltest)
|
||||
returns "text/xml" as $$
|
||||
select xmlconcat2(state, next.xml)::"text/xml";
|
||||
$$ language sql;
|
||||
|
||||
create or replace function test.xml_handler_final (data "text/xml")
|
||||
returns "text/xml" as $$
|
||||
select data;
|
||||
$$ language sql;
|
||||
|
||||
drop aggregate if exists test.text_xml_agg(test.xmltest);
|
||||
create aggregate test.text_xml_agg (test.xmltest) (
|
||||
stype = "text/xml"
|
||||
, sfunc = test.xml_handler_transition
|
||||
, finalfunc = test.xml_handler_final
|
||||
);
|
||||
|
||||
CREATE TABLE oid_test(
|
||||
id int,
|
||||
oid_col oid,
|
||||
@@ -3471,3 +3499,160 @@ stable
|
||||
as $$ begin
|
||||
return query select items2.id from items2 where items2.id=search2.id;
|
||||
end$$;
|
||||
|
||||
create table test.lines (
|
||||
id int primary key
|
||||
, name text
|
||||
, geom extensions.geometry(LINESTRING, 4326)
|
||||
);
|
||||
|
||||
create or replace function test.get_lines ()
|
||||
returns setof test.lines as $$
|
||||
select * from lines;
|
||||
$$ language sql;
|
||||
|
||||
create or replace function test.get_shop_bles ()
|
||||
returns setof test.shop_bles as $$
|
||||
select * from shop_bles;
|
||||
$$ language sql;
|
||||
|
||||
-- it can work without a final function too if the stype is already the media type
|
||||
create or replace function test.twkb_handler_transition (state "application/vnd.twkb", next test.lines)
|
||||
returns "application/vnd.twkb" as $$
|
||||
select (state || extensions.st_astwkb(next.geom)) :: "application/vnd.twkb";
|
||||
$$ language sql;
|
||||
|
||||
drop aggregate if exists test.twkb_agg(test.lines);
|
||||
create aggregate test.twkb_agg (test.lines) (
|
||||
initcond = ''
|
||||
, stype = "application/vnd.twkb"
|
||||
, sfunc = test.twkb_handler_transition
|
||||
);
|
||||
|
||||
create or replace function test.geo2json_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.geo2json_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.geo2json_agg(anyelement);
|
||||
create aggregate test.geo2json_agg(anyelement) (
|
||||
initcond = '[]'
|
||||
, stype = "application/vnd.geo2+json"
|
||||
, sfunc = geo2json_trans
|
||||
, finalfunc = geo2json_final
|
||||
);
|
||||
|
||||
create or replace function test.geo2json_trans (state "application/vnd.geo2+json", next test.shop_bles)
|
||||
returns "application/vnd.geo2+json" as $$
|
||||
select '"anyelement overridden"'::"application/vnd.geo2+json";
|
||||
$$ language sql;
|
||||
|
||||
drop aggregate if exists test.geo2json_agg(test.shop_bles);
|
||||
create aggregate test.geo2json_agg(test.shop_bles) (
|
||||
initcond = '[]'
|
||||
, stype = "application/vnd.geo2+json"
|
||||
, sfunc = geo2json_trans
|
||||
);
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
-- override application/vnd.pgrst.object
|
||||
create or replace function test.pgrst_obj_json_trans (state "application/vnd.pgrst.object", next anyelement)
|
||||
returns "application/vnd.pgrst.object" as $$
|
||||
select null;
|
||||
$$ language sql;
|
||||
|
||||
drop aggregate if exists test.pgrst_obj_agg(anyelement);
|
||||
create aggregate test.pgrst_obj_agg(anyelement) (
|
||||
initcond = '{"overridden": "true"}'
|
||||
, stype = "application/vnd.pgrst.object"
|
||||
, sfunc = pgrst_obj_json_trans
|
||||
);
|
||||
|
||||
-- create a "text/tab-separated-values" media type
|
||||
create or replace function test.tsv_trans (state text, next test.projects)
|
||||
returns "text/tab-separated-values" as $$
|
||||
select (state || next.id::text || E'\t' || next.name || E'\t' || coalesce(next.client_id::text, '') || E'\n')::"text/tab-separated-values";
|
||||
$$ language sql;
|
||||
|
||||
|
||||
create or replace function test.tsv_final (data "text/tab-separated-values")
|
||||
returns "text/tab-separated-values" as $$
|
||||
select set_config('response.headers', '[{"Cache-Control": "public"}, {"Cache-Control": "max-age=259200"}]', true);
|
||||
select (E'id\tname\tclient_id\n' || data)::"text/tab-separated-values";
|
||||
$$ language sql;
|
||||
|
||||
drop aggregate if exists test.tsv_agg(test.projects);
|
||||
create aggregate test.tsv_agg (test.projects) (
|
||||
initcond = ''
|
||||
, stype = "text/tab-separated-values"
|
||||
, sfunc = tsv_trans
|
||||
, finalfunc = tsv_final
|
||||
);
|
||||
|
||||
-- override CSV with BOM plus attachment
|
||||
create or replace function test.bom_csv_trans (state text, next test.lines)
|
||||
returns "text/csv" as $$
|
||||
select (state || next.id::text || ',' || next.name || ',' || next.geom::text || E'\n')::"text/csv";
|
||||
$$ language sql;
|
||||
|
||||
create or replace function test.bom_csv_final (data "text/csv")
|
||||
returns "text/csv" as $$
|
||||
select set_config('response.headers', '[{"Content-Disposition": "attachment; filename=\"lines.csv\""}]', true);
|
||||
-- EFBBBF is the BOM in UTF8 https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
|
||||
select (convert_from (decode (E'EFBBBF', 'hex'),'UTF8') || (E'id,name,geom\n' || data))::"text/csv";
|
||||
$$ language sql;
|
||||
|
||||
drop aggregate if exists test.bom_csv_agg(test.lines);
|
||||
create aggregate test.bom_csv_agg (test.lines) (
|
||||
initcond = ''
|
||||
, stype = "text/csv"
|
||||
, sfunc = bom_csv_trans
|
||||
, finalfunc = bom_csv_final
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user