@@ -1,6 +1,9 @@
1−{-# LANGUAGE DataKinds #-}
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE FlexibleInstances #-}
2−{-# LANGUAGE QuasiQuotes #-}
3+{-# LANGUAGE QuasiQuotes #-}
4+{-# LANGUAGE ScopedTypeVariables #-}
5+{-# LANGUAGE TypeFamilies #-}
3−{-# LANGUAGE TypeOperators #-}
6+{-# LANGUAGE TypeOperators #-}
4{-# OPTIONS_GHC -fno-warn-orphans #-}
7{-# OPTIONS_GHC -fno-warn-orphans #-}
5module Servant.JQuerySpec where
8module Servant.JQuerySpec where
@@ -11,6 +14,7 @@ import Test.Hspec
12import Servant.API
15import Servant.API
13import Servant.JQuery
16import Servant.JQuery
17+import Servant.JQuerySpec.CustomHeaders
15type TestAPI = [sitemap|
19type TestAPI = [sitemap|
16POST /simple String -> Bool
20POST /simple String -> Bool
@@ -20,6 +24,30 @@ GET /has.extension Bool
20type TopLevelRawAPI = "something" :> Get Int
24type TopLevelRawAPI = "something" :> Get Int
27+type HeaderHandlingAPI = "something" :> Header "Foo" String
30+type CustomAuthAPI = "something" :> Authorization "Basic" String
33+type CustomHeaderAPI = "something" :> MyLovelyHorse String
36+type CustomHeaderAPI2 = "something" :> WhatsForDinner String
39+headerHandlingProxy :: Proxy HeaderHandlingAPI
40+headerHandlingProxy = Proxy
42+customAuthProxy :: Proxy CustomAuthAPI
43+customAuthProxy = Proxy
45+customHeaderProxy :: Proxy CustomHeaderAPI
46+customHeaderProxy = Proxy
48+customHeaderProxy2 :: Proxy CustomHeaderAPI2
49+customHeaderProxy2 = Proxy
23spec :: Spec
51spec :: Spec
24spec = describe "Servant.JQuery"
52spec = describe "Servant.JQuery"
25 generateJSSpec
53 generateJSSpec
@@ -37,3 +65,30 @@ generateJSSpec = describe "generateJS" $ do
37 print $ generateJS $ topLevel "GET"
65 print $ generateJS $ topLevel "GET"
38 parseFromString (generateJS $ topLevel "GET") `shouldSatisfy` isRight
66 parseFromString (generateJS $ topLevel "GET") `shouldSatisfy` isRight
68+ it "should handle simple HTTP headers" $ do
69+ let jsText = generateJS $ jquery headerHandlingProxy
71+ parseFromString jsText `shouldSatisfy` isRight
72+ jsText `shouldContain` "headerFoo"
73+ jsText `shouldContain` "headers: { \"Foo\": headerFoo }\n"
75+ it "should handle complex HTTP headers" $ do
76+ let jsText = generateJS $ jquery customAuthProxy
78+ parseFromString jsText `shouldSatisfy` isRight
79+ jsText `shouldContain` "headerAuthorization"
80+ jsText `shouldContain` "headers: { \"Authorization\": \"Basic \" + headerAuthorization }\n"
82+ it "should handle complex, custom HTTP headers" $ do
83+ let jsText = generateJS $ jquery customHeaderProxy
85+ parseFromString jsText `shouldSatisfy` isRight
86+ jsText `shouldContain` "headerXMyLovelyHorse"
87+ jsText `shouldContain` "headers: { \"X-MyLovelyHorse\": \"I am good friends with \" + headerXMyLovelyHorse }\n"
89+ it "should handle complex, custom HTTP headers (template replacement)" $ do
90+ let jsText = generateJS $ jquery customHeaderProxy2
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"