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)
12import Language.ECMAScript3.Parser (parseFromString)
17import Servant.JQuerySpec.CustomHeaders
19type TestAPI = [sitemap|
20POST /simple String -> Bool
21GET /has.extension Bool
24type TopLevelRawAPI = "something" :> Get Int
27type HeaderHandlingAPI = "something" :> Header "Foo" String
30type CustomAuthAPI = "something" :> Authorization "Basic" String
33type CustomHeaderAPI = "something" :> MyLovelyHorse String
36type CustomHeaderAPI2 = "something" :> WhatsForDinner String
39headerHandlingProxy :: Proxy HeaderHandlingAPI
40headerHandlingProxy = Proxy
42customAuthProxy :: Proxy CustomAuthAPI
43customAuthProxy = Proxy
45customHeaderProxy :: Proxy CustomHeaderAPI
46customHeaderProxy = Proxy
48customHeaderProxy2 :: Proxy CustomHeaderAPI2
49customHeaderProxy2 = Proxy
52spec = describe "Servant.JQuery"
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
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
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
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
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"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list