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)
16import Servant.API.ContentTypes
18import Servant.JQuerySpec.CustomHeaders
20type TestAPI = "simple" :> ReqBody '[JSON,FormUrlEncoded] String :> Post '[JSON] Bool
21 :<|> "has.extension" :> Get '[FormUrlEncoded,JSON] Bool
23type TopLevelRawAPI = "something" :> Get '[JSON] Int
26type HeaderHandlingAPI = "something" :> Header "Foo" String
29type CustomAuthAPI = "something" :> Authorization "Basic" String
32type CustomHeaderAPI = "something" :> MyLovelyHorse String
35type CustomHeaderAPI2 = "something" :> WhatsForDinner String
38headerHandlingProxy :: Proxy HeaderHandlingAPI
39headerHandlingProxy = Proxy
41customAuthProxy :: Proxy CustomAuthAPI
42customAuthProxy = Proxy
44customHeaderProxy :: Proxy CustomHeaderAPI
45customHeaderProxy = Proxy
47customHeaderProxy2 :: Proxy CustomHeaderAPI2
48customHeaderProxy2 = Proxy
51spec = describe "Servant.JQuery"
55generateJSSpec = describe "generateJS" $ do
56 it "should generate valid javascript" $ do
57 let (postSimple :<|> getHasExtension ) = jquery (Proxy :: Proxy TestAPI)
58 parseFromString (generateJS postSimple) `shouldSatisfy` isRight
59 parseFromString (generateJS getHasExtension) `shouldSatisfy` isRight
60 print $ generateJS getHasExtension
62 it "should use non-empty function names" $ do
63 let (_ :<|> topLevel) = jquery (Proxy :: Proxy TopLevelRawAPI)
64 print $ generateJS $ topLevel "GET"
65 parseFromString (generateJS $ topLevel "GET") `shouldSatisfy` isRight
67 it "should handle simple HTTP headers" $ do
68 let jsText = generateJS $ jquery headerHandlingProxy
70 parseFromString jsText `shouldSatisfy` isRight
71 jsText `shouldContain` "headerFoo"
72 jsText `shouldContain` "headers: { \"Foo\": headerFoo }\n"
74 it "should handle complex HTTP headers" $ do
75 let jsText = generateJS $ jquery customAuthProxy
77 parseFromString jsText `shouldSatisfy` isRight
78 jsText `shouldContain` "headerAuthorization"
79 jsText `shouldContain` "headers: { \"Authorization\": \"Basic \" + headerAuthorization }\n"
81 it "should handle complex, custom HTTP headers" $ do
82 let jsText = generateJS $ jquery customHeaderProxy
84 parseFromString jsText `shouldSatisfy` isRight
85 jsText `shouldContain` "headerXMyLovelyHorse"
86 jsText `shouldContain` "headers: { \"X-MyLovelyHorse\": \"I am good friends with \" + headerXMyLovelyHorse }\n"
88 it "should handle complex, custom HTTP headers (template replacement)" $ do
89 let jsText = generateJS $ jquery customHeaderProxy2
91 parseFromString jsText `shouldSatisfy` isRight
92 jsText `shouldContain` "headerXWhatsForDinner"
93 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