2015-01-30 01:03:48 +01:00
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
2015-12-16 13:38:42 +01:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2014-11-07 09:58:53 +01:00
|
|
|
module Servant.Utils.LinksSpec where
|
2014-10-27 08:52:18 +01:00
|
|
|
|
2015-12-16 13:38:42 +01:00
|
|
|
import Data.Proxy (Proxy (..))
|
|
|
|
import Test.Hspec (Expectation, Spec, describe, it,
|
|
|
|
shouldBe)
|
2014-10-27 08:52:18 +01:00
|
|
|
|
2015-12-16 13:38:42 +01:00
|
|
|
import Servant.API
|
2014-10-27 08:52:18 +01:00
|
|
|
|
|
|
|
type TestApi =
|
2015-10-08 22:40:46 +02:00
|
|
|
-- Capture and query params
|
2016-07-08 09:11:34 +02:00
|
|
|
"hello" :> Capture "name" String :> QueryParam "capital" Bool :> Delete '[JSON] NoContent
|
2016-05-26 17:49:04 +02:00
|
|
|
:<|> "all" :> CaptureAll "names" String :> Get '[JSON] NoContent
|
2015-01-29 00:07:01 +01:00
|
|
|
|
|
|
|
-- Flags
|
2016-07-08 09:11:34 +02:00
|
|
|
:<|> "balls" :> QueryFlag "bouncy" :> QueryFlag "fast" :> Delete '[JSON] NoContent
|
2015-01-29 00:07:01 +01:00
|
|
|
|
|
|
|
-- All of the verbs
|
2016-07-08 09:11:34 +02:00
|
|
|
:<|> "get" :> Get '[JSON] NoContent
|
|
|
|
:<|> "put" :> Put '[JSON] NoContent
|
|
|
|
:<|> "post" :> ReqBody '[JSON] 'True :> Post '[JSON] NoContent
|
|
|
|
:<|> "delete" :> Header "ponies" String :> Delete '[JSON] NoContent
|
2015-01-29 00:07:01 +01:00
|
|
|
:<|> "raw" :> Raw
|
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
|
2015-12-16 13:38:42 +01:00
|
|
|
it "generates correct links for capture query params" $ do
|
2016-07-08 09:11:34 +02:00
|
|
|
let l1 = Proxy :: Proxy ("hello" :> Capture "name" String :> Delete '[JSON] NoContent)
|
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
|
2016-07-08 09:11:34 +02:00
|
|
|
:> Delete '[JSON] NoContent)
|
2015-06-19 10:03:24 +02:00
|
|
|
apiLink l2 "bye" (Just True) `shouldBeURI` "hello/bye?capital=true"
|
2015-01-29 00:07:01 +01:00
|
|
|
|
2016-05-26 17:49:04 +02:00
|
|
|
it "generates correct links for CaptureAll" $ do
|
|
|
|
apiLink (Proxy :: Proxy ("all" :> CaptureAll "names" String :> Get '[JSON] NoContent))
|
|
|
|
["roads", "lead", "to", "rome"]
|
|
|
|
`shouldBeURI` "all/roads/lead/to/rome"
|
2015-01-29 00:07:01 +01:00
|
|
|
|
2015-12-16 13:38:42 +01:00
|
|
|
it "generates correct links for query flags" $ do
|
2015-01-29 00:07:01 +01:00
|
|
|
let l1 = Proxy :: Proxy ("balls" :> QueryFlag "bouncy"
|
2016-07-08 09:11:34 +02:00
|
|
|
:> QueryFlag "fast" :> Delete '[JSON] NoContent)
|
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
|
|
|
|
2015-12-16 13:38:42 +01:00
|
|
|
it "generates correct links for all of the verbs" $ do
|
2016-07-08 09:11:34 +02:00
|
|
|
apiLink (Proxy :: Proxy ("get" :> Get '[JSON] NoContent)) `shouldBeURI` "get"
|
|
|
|
apiLink (Proxy :: Proxy ("put" :> Put '[JSON] NoContent)) `shouldBeURI` "put"
|
|
|
|
apiLink (Proxy :: Proxy ("post" :> Post '[JSON] NoContent)) `shouldBeURI` "post"
|
|
|
|
apiLink (Proxy :: Proxy ("delete" :> Delete '[JSON] NoContent)) `shouldBeURI` "delete"
|
2015-01-30 01:03:48 +01:00
|
|
|
apiLink (Proxy :: Proxy ("raw" :> Raw)) `shouldBeURI` "raw"
|
2015-12-16 13:38:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
-- |
|
|
|
|
-- Before https://github.com/CRogers/should-not-typecheck/issues/5 is fixed,
|
|
|
|
-- we'll just use doctest
|
|
|
|
--
|
|
|
|
-- >>> apiLink (Proxy :: Proxy WrongPath)
|
|
|
|
-- ...
|
2016-04-17 20:50:45 +02:00
|
|
|
-- ...Could not deduce...
|
2015-12-16 13:38:42 +01:00
|
|
|
-- ...
|
|
|
|
--
|
|
|
|
-- >>> apiLink (Proxy :: Proxy WrongReturnType)
|
|
|
|
-- ...
|
2016-04-17 20:50:45 +02:00
|
|
|
-- ...Could not deduce...
|
2015-12-16 13:38:42 +01:00
|
|
|
-- ...
|
|
|
|
--
|
|
|
|
-- >>> apiLink (Proxy :: Proxy WrongContentType)
|
|
|
|
-- ...
|
2016-04-17 20:50:45 +02:00
|
|
|
-- ...Could not deduce...
|
2015-12-16 13:38:42 +01:00
|
|
|
-- ...
|
|
|
|
--
|
|
|
|
-- >>> apiLink (Proxy :: Proxy WrongMethod)
|
|
|
|
-- ...
|
2016-04-17 20:50:45 +02:00
|
|
|
-- ...Could not deduce...
|
2015-12-16 13:38:42 +01:00
|
|
|
-- ...
|
|
|
|
--
|
|
|
|
-- >>> apiLink (Proxy :: Proxy NotALink)
|
|
|
|
-- ...
|
2016-04-17 20:50:45 +02:00
|
|
|
-- ...Could not deduce...
|
2015-12-16 13:38:42 +01:00
|
|
|
-- ...
|
|
|
|
--
|
|
|
|
-- sanity check
|
|
|
|
-- >>> apiLink (Proxy :: Proxy AllGood)
|
|
|
|
-- get
|
2016-07-08 09:11:34 +02:00
|
|
|
type WrongPath = "getTypo" :> Get '[JSON] NoContent
|
2015-12-16 13:38:42 +01:00
|
|
|
type WrongReturnType = "get" :> Get '[JSON] Bool
|
2016-07-08 09:11:34 +02:00
|
|
|
type WrongContentType = "get" :> Get '[OctetStream] NoContent
|
|
|
|
type WrongMethod = "get" :> Post '[JSON] NoContent
|
2015-12-16 13:38:42 +01:00
|
|
|
type NotALink = "hello" :> ReqBody '[JSON] 'True :> Get '[JSON] Bool
|
2016-07-08 09:11:34 +02:00
|
|
|
type AllGood = "get" :> Get '[JSON] NoContent
|