From d7f9e30fc8ee081d8e1059011fe685c157b341ec Mon Sep 17 00:00:00 2001 From: Geoffrey Roberts Date: Thu, 22 Jan 2015 11:41:06 +1100 Subject: [PATCH] Add a test to explicitly cover header variable template replacement --- test/Servant/JQuerySpec.hs | 13 +++++++++++++ test/Servant/JQuerySpec/CustomHeaders.hs | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/test/Servant/JQuerySpec.hs b/test/Servant/JQuerySpec.hs index ada56d27..fdc9331f 100644 --- a/test/Servant/JQuerySpec.hs +++ b/test/Servant/JQuerySpec.hs @@ -33,6 +33,9 @@ type CustomAuthAPI = "something" :> Authorization "Basic" String type CustomHeaderAPI = "something" :> MyLovelyHorse String :> Get Int +type CustomHeaderAPI2 = "something" :> WhatsForDinner String + :> Get Int + headerHandlingProxy :: Proxy HeaderHandlingAPI headerHandlingProxy = Proxy @@ -42,6 +45,9 @@ customAuthProxy = Proxy customHeaderProxy :: Proxy CustomHeaderAPI customHeaderProxy = Proxy +customHeaderProxy2 :: Proxy CustomHeaderAPI2 +customHeaderProxy2 = Proxy + spec :: Spec spec = describe "Servant.JQuery" generateJSSpec @@ -79,3 +85,10 @@ generateJSSpec = describe "generateJS" $ do parseFromString jsText `shouldSatisfy` isRight jsText `shouldContain` "headerXMyLovelyHorse" jsText `shouldContain` "headers: { \"X-MyLovelyHorse\": \"I am good friends with \" + headerXMyLovelyHorse }\n" + + it "should handle complex, custom HTTP headers (template replacement)" $ do + let jsText = generateJS $ jquery customHeaderProxy2 + print jsText + parseFromString jsText `shouldSatisfy` isRight + jsText `shouldContain` "headerXWhatsForDinner" + jsText `shouldContain` "headers: { \"X-WhatsForDinner\": \"I would like \" + headerXWhatsForDinner + \" with a cherry on top.\" }\n" diff --git a/test/Servant/JQuerySpec/CustomHeaders.hs b/test/Servant/JQuerySpec/CustomHeaders.hs index 4bb378d9..4480d44c 100644 --- a/test/Servant/JQuerySpec/CustomHeaders.hs +++ b/test/Servant/JQuerySpec/CustomHeaders.hs @@ -41,3 +41,15 @@ instance (HasJQ sublayout) req & reqHeaders <>~ [ ReplaceHeaderArg "X-MyLovelyHorse" tpl ] where tpl = "I am good friends with {X-MyLovelyHorse}" + +-- | This is a combinator that fetches an X-WhatsForDinner header. +data WhatsForDinner a + +instance (HasJQ sublayout) + => HasJQ (WhatsForDinner a :> sublayout) where + type JQ (WhatsForDinner a :> sublayout) = JQ sublayout + + jqueryFor Proxy req = jqueryFor (Proxy :: Proxy sublayout) $ + req & reqHeaders <>~ [ ReplaceHeaderArg "X-WhatsForDinner" tpl ] + where + tpl = "I would like {X-WhatsForDinner} with a cherry on top."