@@ -1,6 +1,9 @@
1−{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE QuasiQuotes #-}
3−{-# LANGUAGE TypeOperators #-}
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE FlexibleInstances #-}
3+{-# LANGUAGE QuasiQuotes #-}
4+{-# LANGUAGE ScopedTypeVariables #-}
5+{-# LANGUAGE TypeFamilies #-}
6+{-# LANGUAGE TypeOperators #-}
47{-# OPTIONS_GHC -fno-warn-orphans #-}
58module Servant.JQuerySpec where
69
@@ -11,6 +14,7 @@ import Test.Hspec
1114
1215import Servant.API
1316import Servant.JQuery
17+import Servant.JQuerySpec.CustomHeaders
1418
1519type TestAPI = [sitemap|
1620POST /simple String -> Bool
@@ -20,6 +24,30 @@ GET /has.extension Bool
2024type TopLevelRawAPI = "something" :> Get Int
2125 :<|> Raw
2226
27+type HeaderHandlingAPI = "something" :> Header "Foo" String
28+ :> Get Int
29+
30+type CustomAuthAPI = "something" :> Authorization "Basic" String
31+ :> Get Int
32+
33+type CustomHeaderAPI = "something" :> MyLovelyHorse String
34+ :> Get Int
35+
36+type CustomHeaderAPI2 = "something" :> WhatsForDinner String
37+ :> Get Int
38+
39+headerHandlingProxy :: Proxy HeaderHandlingAPI
40+headerHandlingProxy = Proxy
41+
42+customAuthProxy :: Proxy CustomAuthAPI
43+customAuthProxy = Proxy
44+
45+customHeaderProxy :: Proxy CustomHeaderAPI
46+customHeaderProxy = Proxy
47+
48+customHeaderProxy2 :: Proxy CustomHeaderAPI2
49+customHeaderProxy2 = Proxy
50+
2351spec :: Spec
2452spec = describe "Servant.JQuery"
2553 generateJSSpec
@@ -37,3 +65,30 @@ generateJSSpec = describe "generateJS" $ do
3765 print $ generateJS $ topLevel "GET"
3866 parseFromString (generateJS $ topLevel "GET") `shouldSatisfy` isRight
3967
68+ it "should handle simple HTTP headers" $ do
69+ let jsText = generateJS $ jquery headerHandlingProxy
70+ print jsText
71+ parseFromString jsText `shouldSatisfy` isRight
72+ jsText `shouldContain` "headerFoo"
73+ jsText `shouldContain` "headers: { \"Foo\": headerFoo }\n"
74+
75+ it "should handle complex HTTP headers" $ do
76+ let jsText = generateJS $ jquery customAuthProxy
77+ print jsText
78+ parseFromString jsText `shouldSatisfy` isRight
79+ jsText `shouldContain` "headerAuthorization"
80+ jsText `shouldContain` "headers: { \"Authorization\": \"Basic \" + headerAuthorization }\n"
81+
82+ it "should handle complex, custom HTTP headers" $ do
83+ let jsText = generateJS $ jquery customHeaderProxy
84+ print jsText
85+ parseFromString jsText `shouldSatisfy` isRight
86+ jsText `shouldContain` "headerXMyLovelyHorse"
87+ jsText `shouldContain` "headers: { \"X-MyLovelyHorse\": \"I am good friends with \" + headerXMyLovelyHorse }\n"
88+
89+ it "should handle complex, custom HTTP headers (template replacement)" $ do
90+ let jsText = generateJS $ jquery customHeaderProxy2
91+ print jsText
92+ parseFromString jsText `shouldSatisfy` isRight
93+ jsText `shouldContain` "headerXWhatsForDinner"
94+ jsText `shouldContain` "headers: { \"X-WhatsForDinner\": \"I would like \" + headerXWhatsForDinner + \" with a cherry on top.\" }\n"