@@ -4,7 +4,7 @@
4{-# LANGUAGE DeriveGeneric #-}
4{-# LANGUAGE DeriveGeneric #-}
5{-# LANGUAGE TypeOperators #-}
5{-# LANGUAGE TypeOperators #-}
6{-# LANGUAGE OverloadedStrings #-}
6{-# LANGUAGE OverloadedStrings #-}
7−
7+{-# OPTIONS_GHC -fcontext-stack=25 #-}
8module Servant.ClientSpec where
8module Servant.ClientSpec where
10import Control.Concurrent
10import Control.Concurrent
@@ -50,6 +50,9 @@ type Api =
50 :<|> "param" :> QueryParam "name" String :> Get Person
50 :<|> "param" :> QueryParam "name" String :> Get Person
51 :<|> "params" :> QueryParams "names" String :> Get [Person]
51 :<|> "params" :> QueryParams "names" String :> Get [Person]
52 :<|> "flag" :> QueryFlag "flag" :> Get Bool
52 :<|> "flag" :> QueryFlag "flag" :> Get Bool
53+ :<|> "matrixparam" :> MatrixParam "name" String :> Get Person
54+ :<|> "matrixparams" :> MatrixParams "name" String :> Get [Person]
55+ :<|> "matrixflag" :> MatrixFlag "flag" :> Get Bool
53 :<|> "rawSuccess" :> Raw
56 :<|> "rawSuccess" :> Raw
54 :<|> "rawFailure" :> Raw
57 :<|> "rawFailure" :> Raw
55 :<|> "multiple" :>
58 :<|> "multiple" :>
@@ -72,6 +75,12 @@ server = serve api (
72 Nothing -> left (400, "missing parameter"))
75 Nothing -> left (400, "missing parameter"))
73 :<|> (\ names -> return (zipWith Person names [0..]))
76 :<|> (\ names -> return (zipWith Person names [0..]))
74 :<|> return
77 :<|> return
78+ :<|> (\ name -> case name of
79+ Just "alice" -> return alice
80+ Just name -> left (400, name ++ " not found")
81+ Nothing -> left (400, "missing parameter"))
82+ :<|> (\ names -> return (zipWith Person names [0..]))
75 :<|> (\ _request respond -> respond $ responseLBS ok200 [] "rawSuccess")
84 :<|> (\ _request respond -> respond $ responseLBS ok200 [] "rawSuccess")
76 :<|> (\ _request respond -> respond $ responseLBS badRequest400 [] "rawFailure")
85 :<|> (\ _request respond -> respond $ responseLBS badRequest400 [] "rawFailure")
77 :<|> \ a b c d -> return (a, b, c, d)
86 :<|> \ a b c d -> return (a, b, c, d)
@@ -86,6 +95,9 @@ getBody :: Person -> BaseUrl -> EitherT String IO Person
86getQueryParam :: Maybe String -> BaseUrl -> EitherT String IO Person
95getQueryParam :: Maybe String -> BaseUrl -> EitherT String IO Person
87getQueryParams :: [String] -> BaseUrl -> EitherT String IO [Person]
96getQueryParams :: [String] -> BaseUrl -> EitherT String IO [Person]
88getQueryFlag :: Bool -> BaseUrl -> EitherT String IO Bool
97getQueryFlag :: Bool -> BaseUrl -> EitherT String IO Bool
98+getMatrixParam :: Maybe String -> BaseUrl -> EitherT String IO Person
99+getMatrixParams :: [String] -> BaseUrl -> EitherT String IO [Person]
100+getMatrixFlag :: Bool -> BaseUrl -> EitherT String IO Bool
89getRawSuccess :: Method -> BaseUrl -> EitherT String IO (Int, ByteString)
101getRawSuccess :: Method -> BaseUrl -> EitherT String IO (Int, ByteString)
90getRawFailure :: Method -> BaseUrl -> EitherT String IO (Int, ByteString)
102getRawFailure :: Method -> BaseUrl -> EitherT String IO (Int, ByteString)
91getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
103getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
@@ -97,6 +109,9 @@ getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
97 :<|> getQueryParam
109 :<|> getQueryParam
98 :<|> getQueryParams
110 :<|> getQueryParams
99 :<|> getQueryFlag
111 :<|> getQueryFlag
113+ :<|> getMatrixParams
100 :<|> getRawSuccess
115 :<|> getRawSuccess
101 :<|> getRawFailure
116 :<|> getRawFailure
102 :<|> getMultiple)
117 :<|> getMultiple)
@@ -129,6 +144,21 @@ spec = do
129 it (show flag) $ withServer $ \ host -> do
144 it (show flag) $ withServer $ \ host -> do
130 runEitherT (getQueryFlag flag host) `shouldReturn` Right flag
145 runEitherT (getQueryFlag flag host) `shouldReturn` Right flag
147+ it "Servant.API.MatrixParam" $ withServer $ \ host -> do
148+ runEitherT (getMatrixParam (Just "alice") host) `shouldReturn` Right alice
149+ Left result <- runEitherT (getMatrixParam (Just "bob") host)
150+ result `shouldContain` "bob not found"
152+ it "Servant.API.MatrixParam.MatrixParams" $ withServer $ \ host -> do
153+ runEitherT (getMatrixParams [] host) `shouldReturn` Right []
154+ runEitherT (getMatrixParams ["alice", "bob"] host)
155+ `shouldReturn` Right [Person "alice" 0, Person "bob" 1]
157+ context "Servant.API.MatrixParam.MatrixFlag" $
158+ forM_ [False, True] $ \ flag ->
159+ it (show flag) $ withServer $ \ host -> do
160+ runEitherT (getMatrixFlag flag host) `shouldReturn` Right flag
132 it "Servant.API.Raw on success" $ withServer $ \ host -> do
162 it "Servant.API.Raw on success" $ withServer $ \ host -> do
133 runEitherT (getRawSuccess methodGet host) `shouldReturn` Right (200, "rawSuccess")
163 runEitherT (getRawSuccess methodGet host) `shouldReturn` Right (200, "rawSuccess")