on master help sign in
Add a test to explicitly cover header variable template replacement
commitd7f9e30fc8ee081d8e1059011fe685c157b341ec authorGeoffrey Roberts <groberts@anchor.net.au> dateThu 22 Jan 2015, 11:41 (12 years ago) parent8aa5ce5
2 files changed+25
test/Servant/JQuerySpec.hs+13 test/Servant/JQuerySpec/CustomHeaders.hs+12
test/Servant/JQuerySpec.hs+13
@@ -33,6 +33,9 @@ type CustomAuthAPI = "something" :> Authorization "Basic" String
3333type CustomHeaderAPI = "something" :> MyLovelyHorse String
3434 :> Get Int
3535
36+type CustomHeaderAPI2 = "something" :> WhatsForDinner String
37+ :> Get Int
38+
3639headerHandlingProxy :: Proxy HeaderHandlingAPI
3740headerHandlingProxy = Proxy
3841
@@ -42,6 +45,9 @@ customAuthProxy = Proxy
4245customHeaderProxy :: Proxy CustomHeaderAPI
4346customHeaderProxy = Proxy
4447
48+customHeaderProxy2 :: Proxy CustomHeaderAPI2
49+customHeaderProxy2 = Proxy
50+
4551spec :: Spec
4652spec = describe "Servant.JQuery"
4753 generateJSSpec
@@ -79,3 +85,10 @@ generateJSSpec = describe "generateJS" $ do
7985 parseFromString jsText `shouldSatisfy` isRight
8086 jsText `shouldContain` "headerXMyLovelyHorse"
8187 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"
test/Servant/JQuerySpec/CustomHeaders.hs+12
@@ -41,3 +41,15 @@ instance (HasJQ sublayout)
4141 req & reqHeaders <>~ [ ReplaceHeaderArg "X-MyLovelyHorse" tpl ]
4242 where
4343 tpl = "I am good friends with {X-MyLovelyHorse}"
44+
45+-- | This is a combinator that fetches an X-WhatsForDinner header.
46+data WhatsForDinner a
47+
48+instance (HasJQ sublayout)
49+ => HasJQ (WhatsForDinner a :> sublayout) where
50+ type JQ (WhatsForDinner a :> sublayout) = JQ sublayout
51+
52+ jqueryFor Proxy req = jqueryFor (Proxy :: Proxy sublayout) $
53+ req & reqHeaders <>~ [ ReplaceHeaderArg "X-WhatsForDinner" tpl ]
54+ where
55+ tpl = "I would like {X-WhatsForDinner} with a cherry on top."