@@ -7,6 +7,7 @@ module Servant.Utils.LinksSpec where
7import Data.Proxy (Proxy (..))
7import Data.Proxy (Proxy (..))
8import Test.Hspec (Expectation, Spec, describe, it,
8import Test.Hspec (Expectation, Spec, describe, it,
10+import Data.String (fromString)
11import Servant.API
12import Servant.API
@@ -32,38 +33,38 @@ apiLink = safeLink (Proxy :: Proxy TestApi)
33-- | Convert a link to a URI and ensure that this maps to the given string
34-- | Convert a link to a URI and ensure that this maps to the given string
34-- given string
35-- given string
35−shouldBeURI :: URI -> String -> Expectation
36+shouldBeLink :: Link -> String -> Expectation
36−shouldBeURI link expected =
37+shouldBeLink link expected =
37− show link `shouldBe` expected
38+ toUrlPiece link `shouldBe` fromString expected
39spec :: Spec
40spec :: Spec
40spec = describe "Servant.Utils.Links" $ do
41spec = describe "Servant.Utils.Links" $ do
41 it "generates correct links for capture query params" $ do
42 it "generates correct links for capture query params" $ do
42 let l1 = Proxy :: Proxy ("hello" :> Capture "name" String :> Delete '[JSON] NoContent)
43 let l1 = Proxy :: Proxy ("hello" :> Capture "name" String :> Delete '[JSON] NoContent)
43− apiLink l1 "hi" `shouldBeURI` "hello/hi"
44+ apiLink l1 "hi" `shouldBeLink` "hello/hi"
45 let l2 = Proxy :: Proxy ("hello" :> Capture "name" String
46 let l2 = Proxy :: Proxy ("hello" :> Capture "name" String
46 :> QueryParam "capital" Bool
47 :> QueryParam "capital" Bool
47 :> Delete '[JSON] NoContent)
48 :> Delete '[JSON] NoContent)
48− apiLink l2 "bye" (Just True) `shouldBeURI` "hello/bye?capital=true"
49+ apiLink l2 "bye" (Just True) `shouldBeLink` "hello/bye?capital=true"
50 it "generates correct links for CaptureAll" $ do
51 it "generates correct links for CaptureAll" $ do
51 apiLink (Proxy :: Proxy ("all" :> CaptureAll "names" String :> Get '[JSON] NoContent))
52 apiLink (Proxy :: Proxy ("all" :> CaptureAll "names" String :> Get '[JSON] NoContent))
52 ["roads", "lead", "to", "rome"]
53 ["roads", "lead", "to", "rome"]
53− `shouldBeURI` "all/roads/lead/to/rome"
54+ `shouldBeLink` "all/roads/lead/to/rome"
55 it "generates correct links for query flags" $ do
56 it "generates correct links for query flags" $ do
56 let l1 = Proxy :: Proxy ("balls" :> QueryFlag "bouncy"
57 let l1 = Proxy :: Proxy ("balls" :> QueryFlag "bouncy"
57 :> QueryFlag "fast" :> Delete '[JSON] NoContent)
58 :> QueryFlag "fast" :> Delete '[JSON] NoContent)
58− apiLink l1 True True `shouldBeURI` "balls?bouncy&fast"
59+ apiLink l1 True True `shouldBeLink` "balls?bouncy&fast"
59− apiLink l1 False True `shouldBeURI` "balls?fast"
60+ apiLink l1 False True `shouldBeLink` "balls?fast"
61 it "generates correct links for all of the verbs" $ do
62 it "generates correct links for all of the verbs" $ do
62− apiLink (Proxy :: Proxy ("get" :> Get '[JSON] NoContent)) `shouldBeURI` "get"
63+ apiLink (Proxy :: Proxy ("get" :> Get '[JSON] NoContent)) `shouldBeLink` "get"
63− apiLink (Proxy :: Proxy ("put" :> Put '[JSON] NoContent)) `shouldBeURI` "put"
64+ apiLink (Proxy :: Proxy ("put" :> Put '[JSON] NoContent)) `shouldBeLink` "put"
64− apiLink (Proxy :: Proxy ("post" :> Post '[JSON] NoContent)) `shouldBeURI` "post"
65+ apiLink (Proxy :: Proxy ("post" :> Post '[JSON] NoContent)) `shouldBeLink` "post"
65− apiLink (Proxy :: Proxy ("delete" :> Delete '[JSON] NoContent)) `shouldBeURI` "delete"
66+ apiLink (Proxy :: Proxy ("delete" :> Delete '[JSON] NoContent)) `shouldBeLink` "delete"
66− apiLink (Proxy :: Proxy ("raw" :> Raw)) `shouldBeURI` "raw"
67+ apiLink (Proxy :: Proxy ("raw" :> Raw)) `shouldBeLink` "raw"
@@ -96,8 +97,8 @@ spec = describe "Servant.Utils.Links" $ do
98-- sanity check
99-- sanity check
99−-- >>> apiLink (Proxy :: Proxy AllGood)
100+-- >>> toUrlPiece $ apiLink (Proxy :: Proxy AllGood)
101type WrongPath = "getTypo" :> Get '[JSON] NoContent
102type WrongPath = "getTypo" :> Get '[JSON] NoContent
102type WrongReturnType = "get" :> Get '[JSON] Bool
103type WrongReturnType = "get" :> Get '[JSON] Bool
103type WrongContentType = "get" :> Get '[OctetStream] NoContent
104type WrongContentType = "get" :> Get '[OctetStream] NoContent