@@ -3,7 +3,7 @@ module Feature.Query.CustomMediaSpec where
33import Network.Wai (Application )
44
55import Network.HTTP.Types
6- import Network.Wai.Test (SResponse (simpleBody , simpleHeaders , simpleStatus ))
6+ import Network.Wai.Test (SResponse (simpleBody , simpleHeaders ))
77import Test.Hspec
88import Test.Hspec.Wai
99import Test.Hspec.Wai.JSON
@@ -28,14 +28,11 @@ spec = describe "custom media types" $ do
2828 simpleBody r `shouldBe` readFixtureFile " 1.twkb"
2929 simpleHeaders r `shouldContain` [(" Content-Type" , " application/vnd.twkb" )]
3030
31- it " will fail if there's no aggregate defined for the table" $ do
32- request methodGet " /lines" (acceptHdrs " text/plain" ) " "
33- `shouldRespondWith`
34- [json | {"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/plain"} |]
35- { matchStatus = 406
36- , matchHeaders = [ matchContentTypeJson
37- , " Content-Length" <:> " 110" ]
38- }
31+ it " will succeed if an aggregate exist with the correct media type" $ do
32+ r <- request methodGet " /lines" (acceptHdrs " text/plain" ) " "
33+ liftIO $ do
34+ simpleBody r `shouldBe` readFixtureFile " lines.twkb"
35+ simpleHeaders r `shouldContain` [(" Content-Type" , " application/vnd.twkb" )]
3936
4037 it " can get raw xml output with Accept: text/xml if there's an aggregate defined" $ do
4138 request methodGet " /xmltest" (acceptHdrs " text/xml" ) " "
@@ -116,14 +113,22 @@ spec = describe "custom media types" $ do
116113 , matchHeaders = [" Content-Type" <:> " text/xml; charset=utf-8" ]
117114 }
118115
119- it " should fail with function returning text and Accept: text/xml" $ do
116+ it " should get the return type of function when accept is */*" $ do
117+ request methodGet " /rpc/javascript"
118+ [(" Accept" , " */*" )]
119+ " "
120+ `shouldRespondWith`
121+ " This is Javascript."
122+ { matchStatus = 200
123+ , matchHeaders = [" Content-Type" <:> " text/javascript" ]
124+ }
125+
126+ it " should return the content-type as given in function return type" $ do
120127 request methodGet " /rpc/welcome" (acceptHdrs " text/xml" ) " "
121128 `shouldRespondWith`
122- [json |
123- {"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/xml"}
124- |]
125- { matchStatus = 406
126- , matchHeaders = [" Content-Type" <:> " application/json; charset=utf-8" ]
129+ " Welcome to PostgREST"
130+ { matchStatus = 200
131+ , matchHeaders = [" Content-Type" <:> " text/plain; charset=utf-8" ]
127132 }
128133
129134 it " should not fail when the function doesn't return a row" $ do
@@ -160,12 +165,11 @@ spec = describe "custom media types" $ do
160165 simpleBody r `shouldBe` readFixtureFile " lines.twkb"
161166 simpleHeaders r `shouldContain` [(" Content-Type" , " application/vnd.twkb" )]
162167
163- it " fails if doesn't have an aggregate defined" $ do
164- request methodGet " /rpc/get_lines"
165- (acceptHdrs " application/octet-stream" ) " "
166- `shouldRespondWith`
167- [json | {"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: application/octet-stream"} |]
168- { matchStatus = 406 }
168+ it " return the content with the available aggregate and media type" $ do
169+ r <- request methodGet " /rpc/get_lines" (acceptHdrs " application/octet-stream" ) " "
170+ liftIO $ do
171+ simpleBody r `shouldBe` readFixtureFile " lines.twkb"
172+ simpleHeaders r `shouldContain` [(" Content-Type" , " application/vnd.twkb" )]
169173
170174 -- TODO SOH (start of heading) is being added to results
171175 it " works if there's an anyelement aggregate defined" $ do
@@ -221,11 +225,15 @@ spec = describe "custom media types" $ do
221225 simpleHeaders r2 `shouldContain` [(" Content-Length" , " 138" )]
222226
223227 -- https://github.com/PostgREST/postgrest/issues/2170
224- it " will match json in presence of text/plain" $ do
225- r <- request methodGet " /projects?id=eq.1" (acceptHdrs " text/plain, application/json" ) " "
226- liftIO $ do
227- simpleStatus r `shouldBe` status200
228- simpleHeaders r `shouldContain` [(" Content-Type" , " application/json; charset=utf-8" )]
228+ it " TODO" $ do
229+ request methodGet " /projects?id=eq.1"
230+ (acceptHdrs " text/plain, application/json" )
231+ " "
232+ `shouldRespondWith`
233+ " id\t name\t client_id\n 1\t Windows 7\t 1\n "
234+ { matchStatus = 200
235+ , matchHeaders = [ " Content-Type" <:> " text/tab-separated-values" ]
236+ }
229237
230238 -- https://github.com/PostgREST/postgrest/issues/1102
231239 it " will match a custom text/tab-separated-values" $ do
0 commit comments