on master help sign in
Changed template for ReplaceHeaderArg. Got tests working.
158eab5Geoffrey Roberts
servant-jquery.cabal+1
@@ -65,6 +65,7 @@ test-suite spec
65 main-is: Spec.hs
65 main-is: Spec.hs
66 build-depends:
66 build-depends:
67 base == 4.*
67 base == 4.*
68+ , lens
68 , servant-jquery
69 , servant-jquery
69 , servant
70 , servant
70 , hspec >= 2.0
71 , hspec >= 2.0
src/Servant/JQuery/Internal.hs+1−1
@@ -72,7 +72,7 @@ instance Show HeaderArg where
72 | otherwise = p
72 | otherwise = p
73 where
73 where
74 pv = "header" <> n
74 pv = "header" <> n
75− pn = "{header" <> n <> "}"
75+ pn = "{" <> n <> "}"
76 rp = replace pn "" p
76 rp = replace pn "" p
77
77
78data Url = Url
78data Url = Url
test/Servant/JQuerySpec.hs+14
@@ -14,6 +14,7 @@ import Test.Hspec
14
14
15import Servant.API
15import Servant.API
16import Servant.JQuery
16import Servant.JQuery
17+import Servant.JQuerySpec.CustomHeaders
17
18
18type TestAPI = [sitemap|
19type TestAPI = [sitemap|
19POST /simple String -> Bool
20POST /simple String -> Bool
@@ -26,9 +27,15 @@ type TopLevelRawAPI = "something" :> Get Int
26type HeaderHandlingAPI = "something" :> Header "Foo" String
27type HeaderHandlingAPI = "something" :> Header "Foo" String
27 :> Get Int
28 :> Get Int
28
29
30+type CustomAuthAPI = "something" :> Authorization "Basic" String
31+ :> Get Int
32+
29headerHandlingProxy :: Proxy HeaderHandlingAPI
33headerHandlingProxy :: Proxy HeaderHandlingAPI
30headerHandlingProxy = Proxy
34headerHandlingProxy = Proxy
31
35
36+customAuthProxy :: Proxy CustomAuthAPI
37+customAuthProxy = Proxy
38+
32spec :: Spec
39spec :: Spec
33spec = describe "Servant.JQuery"
40spec = describe "Servant.JQuery"
34 generateJSSpec
41 generateJSSpec
@@ -53,3 +60,10 @@ generateJSSpec = describe "generateJS" $ do
53 jsText `shouldContain` "headerFoo"
60 jsText `shouldContain` "headerFoo"
54 jsText `shouldContain` "headers: { \"Foo\": headerFoo }\n"
61 jsText `shouldContain` "headers: { \"Foo\": headerFoo }\n"
55
62
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}"