on master help sign in
Changed template for ReplaceHeaderArg. Got tests working.
commit158eab51571dcab54e770de5d046ec072a6765a1 authorGeoffrey Roberts <groberts@anchor.net.au> dateWed 21 Jan 2015, 18:47 (12 years ago) parente16f90f
4 files changed+47−1
servant-jquery.cabal+1 src/Servant/JQuery/Internal.hs+1−1 test/Servant/JQuerySpec.hs+14 test/Servant/JQuerySpec/CustomHeaders.hs+31
servant-jquery.cabal+1
@@ -65,6 +65,7 @@ test-suite spec
6565 main-is: Spec.hs
6666 build-depends:
6767 base == 4.*
68+ , lens
6869 , servant-jquery
6970 , servant
7071 , hspec >= 2.0
src/Servant/JQuery/Internal.hs+1−1
@@ -72,7 +72,7 @@ instance Show HeaderArg where
7272 | otherwise = p
7373 where
7474 pv = "header" <> n
75− pn = "{header" <> n <> "}"
75+ pn = "{" <> n <> "}"
7676 rp = replace pn "" p
7777
7878data Url = Url
test/Servant/JQuerySpec.hs+14
@@ -14,6 +14,7 @@ import Test.Hspec
1414
1515import Servant.API
1616import Servant.JQuery
17+import Servant.JQuerySpec.CustomHeaders
1718
1819type TestAPI = [sitemap|
1920POST /simple String -> Bool
@@ -26,9 +27,15 @@ type TopLevelRawAPI = "something" :> Get Int
2627type HeaderHandlingAPI = "something" :> Header "Foo" String
2728 :> Get Int
2829
30+type CustomAuthAPI = "something" :> Authorization "Basic" String
31+ :> Get Int
32+
2933headerHandlingProxy :: Proxy HeaderHandlingAPI
3034headerHandlingProxy = Proxy
3135
36+customAuthProxy :: Proxy CustomAuthAPI
37+customAuthProxy = Proxy
38+
3239spec :: Spec
3340spec = describe "Servant.JQuery"
3441 generateJSSpec
@@ -53,3 +60,10 @@ generateJSSpec = describe "generateJS" $ do
5360 jsText `shouldContain` "headerFoo"
5461 jsText `shouldContain` "headers: { \"Foo\": headerFoo }\n"
5562
63+ it "should handle complex HTTP headers" $ do
64+ let jsText = generateJS $ jquery customAuthProxy
65+ print jsText
66+ parseFromString jsText `shouldSatisfy` isRight
67+ jsText `shouldContain` "headerAuthorization"
68+ jsText `shouldContain` "headers: { \"Authorization\": \"Basic \" + headerAuthorization }\n"
69+
test/Servant/JQuerySpec/CustomHeaders.hsnew file+31
@@ -0,0 +1,31 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE FlexibleInstances #-}
3+{-# LANGUAGE KindSignatures #-}
4+{-# LANGUAGE PolyKinds #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TypeFamilies #-}
7+{-# LANGUAGE TypeOperators #-}
8+
9+module Servant.JQuerySpec.CustomHeaders where
10+
11+import Control.Lens
12+import Data.Monoid
13+import Data.Proxy
14+import GHC.TypeLits
15+import Servant.API
16+import Servant.JQuery
17+
18+-- | This is a hypothetical combinator that fetches an Authorization header.
19+-- The symbol in the header denotes what kind of authentication we are
20+-- using -- Basic, Digest, whatever.
21+data Authorization (sym :: Symbol) a
22+
23+instance (KnownSymbol sym, HasJQ sublayout)
24+ => HasJQ (Authorization sym a :> sublayout) where
25+ type JQ (Authorization sym a :> sublayout) = JQ sublayout
26+
27+ jqueryFor Proxy req = jqueryFor (Proxy :: Proxy sublayout) $
28+ req & reqHeaders <>~ [ ReplaceHeaderArg "Authorization" $
29+ tokenType (symbolVal (Proxy :: Proxy sym)) ]
30+ where
31+ tokenType t = t <> " {Authorization}"