2015-01-21 08:27:25 +01:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2014-12-24 13:55:25 +01:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
module Servant.JQuerySpec where
|
|
|
|
|
|
|
|
import Data.Either (isRight)
|
|
|
|
import Data.Proxy
|
|
|
|
import Language.ECMAScript3.Parser (parseFromString)
|
|
|
|
import Test.Hspec
|
|
|
|
|
|
|
|
import Servant.API
|
|
|
|
import Servant.JQuery
|
2015-07-17 23:36:38 +02:00
|
|
|
import qualified Servant.JQuery.Vanilla as JS
|
|
|
|
import qualified Servant.JQuery.JQuery as JQ
|
|
|
|
import qualified Servant.JQuery.Angular as NG
|
2015-01-21 08:47:23 +01:00
|
|
|
import Servant.JQuerySpec.CustomHeaders
|
2014-12-24 13:55:25 +01:00
|
|
|
|
2015-03-03 22:59:36 +01:00
|
|
|
type TestAPI = "simple" :> ReqBody '[JSON,FormUrlEncoded] String :> Post '[JSON] Bool
|
|
|
|
:<|> "has.extension" :> Get '[FormUrlEncoded,JSON] Bool
|
2014-12-24 13:55:25 +01:00
|
|
|
|
2015-02-19 01:36:31 +01:00
|
|
|
type TopLevelRawAPI = "something" :> Get '[JSON] Int
|
2015-01-02 10:46:21 +01:00
|
|
|
:<|> Raw
|
|
|
|
|
2015-01-21 08:27:25 +01:00
|
|
|
type HeaderHandlingAPI = "something" :> Header "Foo" String
|
2015-02-19 01:36:31 +01:00
|
|
|
:> Get '[JSON] Int
|
2015-01-21 08:27:25 +01:00
|
|
|
|
2015-01-21 08:47:23 +01:00
|
|
|
type CustomAuthAPI = "something" :> Authorization "Basic" String
|
2015-02-19 01:36:31 +01:00
|
|
|
:> Get '[JSON] Int
|
2015-01-21 08:47:23 +01:00
|
|
|
|
2015-01-21 09:32:06 +01:00
|
|
|
type CustomHeaderAPI = "something" :> MyLovelyHorse String
|
2015-02-19 01:36:31 +01:00
|
|
|
:> Get '[JSON] Int
|
2015-01-21 09:32:06 +01:00
|
|
|
|
2015-01-22 01:41:06 +01:00
|
|
|
type CustomHeaderAPI2 = "something" :> WhatsForDinner String
|
2015-02-19 01:36:31 +01:00
|
|
|
:> Get '[JSON] Int
|
2015-01-22 01:41:06 +01:00
|
|
|
|
2015-01-21 08:27:25 +01:00
|
|
|
headerHandlingProxy :: Proxy HeaderHandlingAPI
|
|
|
|
headerHandlingProxy = Proxy
|
|
|
|
|
2015-01-21 08:47:23 +01:00
|
|
|
customAuthProxy :: Proxy CustomAuthAPI
|
|
|
|
customAuthProxy = Proxy
|
|
|
|
|
2015-01-21 09:32:06 +01:00
|
|
|
customHeaderProxy :: Proxy CustomHeaderAPI
|
|
|
|
customHeaderProxy = Proxy
|
|
|
|
|
2015-01-22 01:41:06 +01:00
|
|
|
customHeaderProxy2 :: Proxy CustomHeaderAPI2
|
|
|
|
customHeaderProxy2 = Proxy
|
|
|
|
|
2015-07-17 23:36:38 +02:00
|
|
|
data TestNames = Vanilla
|
|
|
|
| VanillaCustom
|
|
|
|
| JQuery
|
|
|
|
| JQueryCustom
|
|
|
|
| Angular
|
|
|
|
| AngularCustom
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
customOptions :: CommonGeneratorOptions
|
|
|
|
customOptions = defCommonGeneratorOptions {
|
|
|
|
successCallback = "okCallback",
|
|
|
|
errorCallback = "errorCallback"
|
|
|
|
}
|
|
|
|
|
2014-12-24 13:55:25 +01:00
|
|
|
spec :: Spec
|
2015-07-17 23:36:38 +02:00
|
|
|
spec = describe "Servant.JQuery" $ do
|
|
|
|
(generateJSSpec Vanilla JS.generateVanillaJS)
|
|
|
|
(generateJSSpec VanillaCustom $ JS.generateVanillaJSWith customOptions)
|
|
|
|
(generateJSSpec JQuery JQ.generateJQueryJS)
|
|
|
|
(generateJSSpec JQueryCustom $ JQ.generateJQueryJSWith customOptions)
|
|
|
|
(generateJSSpec Angular $ NG.generateAngularJS NG.defAngularOptions)
|
|
|
|
(generateJSSpec AngularCustom $ (NG.generateAngularJSWith NG.defAngularOptions) customOptions)
|
|
|
|
|
|
|
|
(angularSpec Angular)
|
|
|
|
(angularSpec AngularCustom)
|
|
|
|
|
|
|
|
angularSpec :: TestNames -> Spec
|
|
|
|
angularSpec test = describe specLabel $ do
|
|
|
|
it "should implement a service globally" $ do
|
|
|
|
let jsText = genJS $ listFromAPI (Proxy :: Proxy TestAPI)
|
|
|
|
output jsText
|
|
|
|
jsText `shouldContain` (".service('" ++ testName ++ "'")
|
|
|
|
|
|
|
|
it "should depend on $http service globally" $ do
|
|
|
|
let jsText = genJS $ listFromAPI (Proxy :: Proxy TestAPI)
|
|
|
|
output jsText
|
|
|
|
jsText `shouldContain` ("('" ++ testName ++ "', function($http) {")
|
|
|
|
|
|
|
|
it "should not depend on $http service in handlers" $ do
|
|
|
|
let jsText = genJS $ listFromAPI (Proxy :: Proxy TestAPI)
|
|
|
|
output jsText
|
|
|
|
jsText `shouldNotContain` "getsomething($http, "
|
|
|
|
where
|
|
|
|
specLabel = "generateJS(" ++ (show test) ++ ")"
|
|
|
|
--output = putStrLn
|
|
|
|
output _ = return ()
|
|
|
|
testName = "MyService"
|
|
|
|
ngOpts = NG.defAngularOptions { NG.serviceName = testName }
|
|
|
|
genJS req = NG.wrapInService ngOpts req
|
|
|
|
|
|
|
|
generateJSSpec :: TestNames -> (AjaxReq -> String) -> Spec
|
|
|
|
generateJSSpec n gen = describe specLabel $ do
|
2015-01-02 10:46:21 +01:00
|
|
|
it "should generate valid javascript" $ do
|
2015-07-17 23:36:38 +02:00
|
|
|
let (postSimple :<|> getHasExtension ) = javascript (Proxy :: Proxy TestAPI)
|
|
|
|
parseFromString (genJS postSimple) `shouldSatisfy` isRight
|
|
|
|
parseFromString (genJS getHasExtension) `shouldSatisfy` isRight
|
|
|
|
output $ genJS getHasExtension
|
2015-01-02 10:46:21 +01:00
|
|
|
|
|
|
|
it "should use non-empty function names" $ do
|
2015-07-17 23:36:38 +02:00
|
|
|
let (_ :<|> topLevel) = javascript (Proxy :: Proxy TopLevelRawAPI)
|
|
|
|
output $ genJS (topLevel "GET")
|
|
|
|
parseFromString (genJS $ topLevel "GET") `shouldSatisfy` isRight
|
2014-12-24 13:55:25 +01:00
|
|
|
|
2015-01-21 08:27:25 +01:00
|
|
|
it "should handle simple HTTP headers" $ do
|
2015-07-17 23:36:38 +02:00
|
|
|
let jsText = genJS $ javascript headerHandlingProxy
|
|
|
|
output jsText
|
2015-01-21 08:27:25 +01:00
|
|
|
parseFromString jsText `shouldSatisfy` isRight
|
|
|
|
jsText `shouldContain` "headerFoo"
|
2015-07-17 23:36:38 +02:00
|
|
|
jsText `shouldContain` (header n "Foo" $ "headerFoo")
|
2015-01-21 08:27:25 +01:00
|
|
|
|
2015-01-21 08:47:23 +01:00
|
|
|
it "should handle complex HTTP headers" $ do
|
2015-07-17 23:36:38 +02:00
|
|
|
let jsText = genJS $ javascript customAuthProxy
|
|
|
|
output jsText
|
2015-01-21 08:47:23 +01:00
|
|
|
parseFromString jsText `shouldSatisfy` isRight
|
|
|
|
jsText `shouldContain` "headerAuthorization"
|
2015-07-17 23:36:38 +02:00
|
|
|
jsText `shouldContain` (header n "Authorization" $ "\"Basic \" + headerAuthorization")
|
2015-01-21 08:47:23 +01:00
|
|
|
|
2015-01-21 09:32:06 +01:00
|
|
|
it "should handle complex, custom HTTP headers" $ do
|
2015-07-17 23:36:38 +02:00
|
|
|
let jsText = genJS $ javascript customHeaderProxy
|
|
|
|
output jsText
|
2015-01-21 09:32:06 +01:00
|
|
|
parseFromString jsText `shouldSatisfy` isRight
|
|
|
|
jsText `shouldContain` "headerXMyLovelyHorse"
|
2015-07-17 23:36:38 +02:00
|
|
|
jsText `shouldContain` (header n "X-MyLovelyHorse" $ "\"I am good friends with \" + headerXMyLovelyHorse")
|
2015-01-22 01:41:06 +01:00
|
|
|
|
|
|
|
it "should handle complex, custom HTTP headers (template replacement)" $ do
|
2015-07-17 23:36:38 +02:00
|
|
|
let jsText = genJS $ javascript customHeaderProxy2
|
|
|
|
output jsText
|
2015-01-22 01:41:06 +01:00
|
|
|
parseFromString jsText `shouldSatisfy` isRight
|
|
|
|
jsText `shouldContain` "headerXWhatsForDinner"
|
2015-07-17 23:36:38 +02:00
|
|
|
jsText `shouldContain` (header n "X-WhatsForDinner" $ "\"I would like \" + headerXWhatsForDinner + \" with a cherry on top.\"")
|
2015-04-19 11:56:29 +02:00
|
|
|
|
|
|
|
it "can generate the whole javascript code string at once with jsForAPI" $ do
|
2015-07-17 23:36:38 +02:00
|
|
|
let jsStr = jsForAPI (Proxy :: Proxy TestAPI) gen
|
2015-04-19 11:56:29 +02:00
|
|
|
parseFromString jsStr `shouldSatisfy` isRight
|
2015-07-17 23:36:38 +02:00
|
|
|
where
|
|
|
|
specLabel = "generateJS(" ++ (show n) ++ ")"
|
|
|
|
--output = print
|
|
|
|
output _ = return ()
|
|
|
|
genJS req = generateJS req gen
|
|
|
|
header :: TestNames -> String -> String -> String
|
|
|
|
header v headerName headerValue
|
|
|
|
| v `elem` [Vanilla, VanillaCustom] = "xhr.setRequestHeader(\"" ++ headerName ++ "\", " ++ headerValue ++ ");\n"
|
|
|
|
| otherwise = "headers: { \"" ++ headerName ++ "\": " ++ headerValue ++ " }\n"
|
|
|
|
--header _ _ _ = "Not Implemented"
|