on 2da16c7 help sign in
servant/test/Servant/JQuerySpec.hs
94 lines 3.5 kB history raw
1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE QuasiQuotes #-}
4{-# LANGUAGE ScopedTypeVariables #-}
5{-# LANGUAGE TypeFamilies #-}
6{-# LANGUAGE TypeOperators #-}
7{-# OPTIONS_GHC -fno-warn-orphans #-}
8module Servant.JQuerySpec where
10import Data.Either (isRight)
11import Data.Proxy
12import Language.ECMAScript3.Parser (parseFromString)
13import Test.Hspec
15import Servant.API
16import Servant.JQuery
17import Servant.JQuerySpec.CustomHeaders
19type TestAPI = [sitemap|
20POST /simple String -> Bool
21GET /has.extension Bool
22|]
24type TopLevelRawAPI = "something" :> Get Int
25 :<|> Raw
27type HeaderHandlingAPI = "something" :> Header "Foo" String
28 :> Get Int
30type CustomAuthAPI = "something" :> Authorization "Basic" String
31 :> Get Int
33type CustomHeaderAPI = "something" :> MyLovelyHorse String
34 :> Get Int
36type CustomHeaderAPI2 = "something" :> WhatsForDinner String
37 :> Get Int
39headerHandlingProxy :: Proxy HeaderHandlingAPI
40headerHandlingProxy = Proxy
42customAuthProxy :: Proxy CustomAuthAPI
43customAuthProxy = Proxy
45customHeaderProxy :: Proxy CustomHeaderAPI
46customHeaderProxy = Proxy
48customHeaderProxy2 :: Proxy CustomHeaderAPI2
49customHeaderProxy2 = Proxy
51spec :: Spec
52spec = describe "Servant.JQuery"
53 generateJSSpec
55generateJSSpec :: Spec
56generateJSSpec = describe "generateJS" $ do
57 it "should generate valid javascript" $ do
58 let (postSimple :<|> getHasExtension ) = jquery (Proxy :: Proxy TestAPI)
59 parseFromString (generateJS postSimple) `shouldSatisfy` isRight
60 parseFromString (generateJS getHasExtension) `shouldSatisfy` isRight
61 print $ generateJS getHasExtension
63 it "should use non-empty function names" $ do
64 let (_ :<|> topLevel) = jquery (Proxy :: Proxy TopLevelRawAPI)
65 print $ generateJS $ topLevel "GET"
66 parseFromString (generateJS $ topLevel "GET") `shouldSatisfy` isRight
68 it "should handle simple HTTP headers" $ do
69 let jsText = generateJS $ jquery headerHandlingProxy
70 print jsText
71 parseFromString jsText `shouldSatisfy` isRight
72 jsText `shouldContain` "headerFoo"
73 jsText `shouldContain` "headers: { \"Foo\": headerFoo }\n"
75 it "should handle complex HTTP headers" $ do
76 let jsText = generateJS $ jquery customAuthProxy
77 print jsText
78 parseFromString jsText `shouldSatisfy` isRight
79 jsText `shouldContain` "headerAuthorization"
80 jsText `shouldContain` "headers: { \"Authorization\": \"Basic \" + headerAuthorization }\n"
82 it "should handle complex, custom HTTP headers" $ do
83 let jsText = generateJS $ jquery customHeaderProxy
84 print jsText
85 parseFromString jsText `shouldSatisfy` isRight
86 jsText `shouldContain` "headerXMyLovelyHorse"
87 jsText `shouldContain` "headers: { \"X-MyLovelyHorse\": \"I am good friends with \" + headerXMyLovelyHorse }\n"
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"