From 158eab51571dcab54e770de5d046ec072a6765a1 Mon Sep 17 00:00:00 2001 From: Geoffrey Roberts Date: Wed, 21 Jan 2015 18:47:23 +1100 Subject: [PATCH] Changed template for ReplaceHeaderArg. Got tests working. --- servant-jquery.cabal | 1 + src/Servant/JQuery/Internal.hs | 2 +- test/Servant/JQuerySpec.hs | 14 +++++++++++ test/Servant/JQuerySpec/CustomHeaders.hs | 31 ++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test/Servant/JQuerySpec/CustomHeaders.hs diff --git a/servant-jquery.cabal b/servant-jquery.cabal index ee6d19ee..6767f6be 100644 --- a/servant-jquery.cabal +++ b/servant-jquery.cabal @@ -65,6 +65,7 @@ test-suite spec main-is: Spec.hs build-depends: base == 4.* + , lens , servant-jquery , servant , hspec >= 2.0 diff --git a/src/Servant/JQuery/Internal.hs b/src/Servant/JQuery/Internal.hs index 31c059ca..3ff0ae5d 100644 --- a/src/Servant/JQuery/Internal.hs +++ b/src/Servant/JQuery/Internal.hs @@ -72,7 +72,7 @@ instance Show HeaderArg where | otherwise = p where pv = "header" <> n - pn = "{header" <> n <> "}" + pn = "{" <> n <> "}" rp = replace pn "" p data Url = Url diff --git a/test/Servant/JQuerySpec.hs b/test/Servant/JQuerySpec.hs index 83135701..db3f1146 100644 --- a/test/Servant/JQuerySpec.hs +++ b/test/Servant/JQuerySpec.hs @@ -14,6 +14,7 @@ import Test.Hspec import Servant.API import Servant.JQuery +import Servant.JQuerySpec.CustomHeaders type TestAPI = [sitemap| POST /simple String -> Bool @@ -26,9 +27,15 @@ type TopLevelRawAPI = "something" :> Get Int type HeaderHandlingAPI = "something" :> Header "Foo" String :> Get Int +type CustomAuthAPI = "something" :> Authorization "Basic" String + :> Get Int + headerHandlingProxy :: Proxy HeaderHandlingAPI headerHandlingProxy = Proxy +customAuthProxy :: Proxy CustomAuthAPI +customAuthProxy = Proxy + spec :: Spec spec = describe "Servant.JQuery" generateJSSpec @@ -53,3 +60,10 @@ generateJSSpec = describe "generateJS" $ do jsText `shouldContain` "headerFoo" jsText `shouldContain` "headers: { \"Foo\": headerFoo }\n" + it "should handle complex HTTP headers" $ do + let jsText = generateJS $ jquery customAuthProxy + print jsText + parseFromString jsText `shouldSatisfy` isRight + jsText `shouldContain` "headerAuthorization" + jsText `shouldContain` "headers: { \"Authorization\": \"Basic \" + headerAuthorization }\n" + diff --git a/test/Servant/JQuerySpec/CustomHeaders.hs b/test/Servant/JQuerySpec/CustomHeaders.hs new file mode 100644 index 00000000..d70bb076 --- /dev/null +++ b/test/Servant/JQuerySpec/CustomHeaders.hs @@ -0,0 +1,31 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE KindSignatures #-} +{-# LANGUAGE PolyKinds #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE TypeOperators #-} + +module Servant.JQuerySpec.CustomHeaders where + +import Control.Lens +import Data.Monoid +import Data.Proxy +import GHC.TypeLits +import Servant.API +import Servant.JQuery + +-- | This is a hypothetical combinator that fetches an Authorization header. +-- The symbol in the header denotes what kind of authentication we are +-- using -- Basic, Digest, whatever. +data Authorization (sym :: Symbol) a + +instance (KnownSymbol sym, HasJQ sublayout) + => HasJQ (Authorization sym a :> sublayout) where + type JQ (Authorization sym a :> sublayout) = JQ sublayout + + jqueryFor Proxy req = jqueryFor (Proxy :: Proxy sublayout) $ + req & reqHeaders <>~ [ ReplaceHeaderArg "Authorization" $ + tokenType (symbolVal (Proxy :: Proxy sym)) ] + where + tokenType t = t <> " {Authorization}"