2014-10-27 08:52:18 +01:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2015-01-30 01:03:48 +01:00
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
2015-01-29 00:07:01 +01:00
|
|
|
|
2014-11-07 09:58:53 +01:00
|
|
|
module Servant.Utils.LinksSpec where
|
2014-10-27 08:52:18 +01:00
|
|
|
|
2015-01-29 00:07:01 +01:00
|
|
|
import Test.Hspec ( Spec, it, describe, shouldBe, Expectation )
|
|
|
|
import Data.Proxy ( Proxy(..) )
|
2014-10-27 08:52:18 +01:00
|
|
|
|
2015-02-18 10:40:55 +01:00
|
|
|
import Servant.API
|
2014-10-27 08:52:18 +01:00
|
|
|
|
|
|
|
type TestApi =
|
2015-01-29 00:07:01 +01:00
|
|
|
-- Capture and query/matrix params
|
|
|
|
"hello" :> Capture "name" String :> QueryParam "capital" Bool :> Delete
|
|
|
|
|
|
|
|
:<|> "parent" :> MatrixParams "name" String :> "child"
|
2015-01-08 16:24:19 +01:00
|
|
|
:> MatrixParam "gender" String :> Get '[JSON] String
|
2015-01-29 00:07:01 +01:00
|
|
|
|
|
|
|
-- Flags
|
|
|
|
:<|> "ducks" :> MatrixFlag "yellow" :> MatrixFlag "loud" :> Delete
|
|
|
|
:<|> "balls" :> QueryFlag "bouncy" :> QueryFlag "fast" :> Delete
|
|
|
|
|
|
|
|
-- All of the verbs
|
2015-01-08 16:24:19 +01:00
|
|
|
:<|> "get" :> Get '[JSON] ()
|
|
|
|
:<|> "put" :> Put '[JSON] ()
|
2015-01-13 20:38:34 +01:00
|
|
|
:<|> "post" :> ReqBody '[JSON] 'True :> Post '[JSON] ()
|
2015-01-29 00:07:01 +01:00
|
|
|
:<|> "delete" :> Header "ponies" :> Delete
|
|
|
|
:<|> "raw" :> Raw
|
2014-10-27 08:52:18 +01:00
|
|
|
|
2015-01-08 16:24:19 +01:00
|
|
|
type TestLink = "hello" :> "hi" :> Get '[JSON] Bool
|
|
|
|
type TestLink2 = "greet" :> Post '[XML] Bool
|
|
|
|
type TestLink3 = "parent" :> "child" :> Get '[JSON] String
|
|
|
|
|
|
|
|
type BadTestLink = "hallo" :> "hi" :> Get '[JSON] Bool
|
|
|
|
type BadTestLink2 = "greet" :> Get '[XML] Bool
|
|
|
|
type BadTestLink3 = "parent" :> "child" :> MatrixFlag "male" :> Get '[JSON] String
|
|
|
|
|
|
|
|
type BadTestLink' = "hello" :> "hi" :> Get '[HTML] Bool
|
|
|
|
type BadTestLink'2 = "greet" :> Get '[HTML] Bool
|
|
|
|
|
|
|
|
type NotALink = "hello" :> Capture "x" Bool :> Get '[JSON] Bool
|
2015-01-13 20:38:34 +01:00
|
|
|
type NotALink2 = "hello" :> ReqBody '[JSON] 'True :> Get '[JSON] Bool
|
2014-10-27 08:52:18 +01:00
|
|
|
|
2015-01-30 01:03:48 +01:00
|
|
|
apiLink :: (IsElem endpoint TestApi, HasLink endpoint)
|
|
|
|
=> Proxy endpoint -> MkLink endpoint
|
|
|
|
apiLink = safeLink (Proxy :: Proxy TestApi)
|
2014-10-27 08:52:18 +01:00
|
|
|
|
2015-01-29 00:07:01 +01:00
|
|
|
-- | Convert a link to a URI and ensure that this maps to the given string
|
|
|
|
-- given string
|
|
|
|
shouldBeURI :: URI -> String -> Expectation
|
|
|
|
shouldBeURI link expected =
|
|
|
|
show link `shouldBe` expected
|
2014-10-27 08:52:18 +01:00
|
|
|
|
|
|
|
spec :: Spec
|
2015-01-29 00:07:01 +01:00
|
|
|
spec = describe "Servant.Utils.Links" $ do
|
|
|
|
it "Generates correct links for capture query and matrix params" $ do
|
|
|
|
let l1 = Proxy :: Proxy ("hello" :> Capture "name" String :> Delete)
|
2015-01-30 01:03:48 +01:00
|
|
|
apiLink l1 "hi" `shouldBeURI` "hello/hi"
|
2015-01-29 00:07:01 +01:00
|
|
|
|
|
|
|
let l2 = Proxy :: Proxy ("hello" :> Capture "name" String
|
|
|
|
:> QueryParam "capital" Bool
|
|
|
|
:> Delete)
|
2015-01-30 01:03:48 +01:00
|
|
|
apiLink l2 "bye" True `shouldBeURI` "hello/bye?capital=true"
|
2015-01-29 00:07:01 +01:00
|
|
|
|
|
|
|
let l3 = Proxy :: Proxy ("parent" :> MatrixParams "name" String
|
|
|
|
:> "child"
|
|
|
|
:> MatrixParam "gender" String
|
2015-02-18 10:40:55 +01:00
|
|
|
:> Get '[JSON] String)
|
2015-01-30 01:03:48 +01:00
|
|
|
apiLink l3 ["Hubert?x=;&", "Cumberdale"] "Edward?"
|
2015-01-29 00:07:01 +01:00
|
|
|
`shouldBeURI` "parent;name[]=Hubert%3Fx%3D%3B%26;\
|
|
|
|
\name[]=Cumberdale/child;gender=Edward%3F"
|
|
|
|
|
|
|
|
it "Generates correct links for query and matrix flags" $ do
|
|
|
|
let l1 = Proxy :: Proxy ("balls" :> QueryFlag "bouncy"
|
|
|
|
:> QueryFlag "fast" :> Delete)
|
2015-01-30 01:03:48 +01:00
|
|
|
apiLink l1 True True `shouldBeURI` "balls?bouncy&fast"
|
|
|
|
apiLink l1 False True `shouldBeURI` "balls?fast"
|
2015-01-29 00:07:01 +01:00
|
|
|
|
|
|
|
let l2 = Proxy :: Proxy ("ducks" :> MatrixFlag "yellow"
|
|
|
|
:> MatrixFlag "loud" :> Delete)
|
2015-01-30 01:03:48 +01:00
|
|
|
apiLink l2 True True `shouldBeURI` "ducks;yellow;loud"
|
|
|
|
apiLink l2 False True `shouldBeURI` "ducks;loud"
|
2015-01-29 00:07:01 +01:00
|
|
|
|
|
|
|
it "Generates correct links for all of the verbs" $ do
|
2015-02-18 10:40:55 +01:00
|
|
|
apiLink (Proxy :: Proxy ("get" :> Get '[JSON] ())) `shouldBeURI` "get"
|
|
|
|
apiLink (Proxy :: Proxy ("put" :> Put '[JSON] ())) `shouldBeURI` "put"
|
|
|
|
apiLink (Proxy :: Proxy ("post" :> Post '[JSON] ())) `shouldBeURI` "post"
|
2015-01-30 01:03:48 +01:00
|
|
|
apiLink (Proxy :: Proxy ("delete" :> Delete)) `shouldBeURI` "delete"
|
|
|
|
apiLink (Proxy :: Proxy ("raw" :> Raw)) `shouldBeURI` "raw"
|