Add tests for CaptureTime.
This commit is contained in:
parent
4f2a3d4afd
commit
7174e1e1fd
1 changed files with 31 additions and 1 deletions
|
@ -34,6 +34,7 @@ import Network.Wai.Internal (Response(ResponseBuilder))
|
|||
import Network.Wai.Test (defaultRequest, request,
|
||||
runSession, simpleBody)
|
||||
import Servant.API ((:<|>) (..), (:>), Capture, Delete,
|
||||
CaptureTime,
|
||||
Get, Header (..), Headers,
|
||||
HttpVersion, IsSecure (..), JSON,
|
||||
Patch, PlainText, Post, Put,
|
||||
|
@ -50,7 +51,8 @@ import Servant.Server.Internal.Router
|
|||
(tweakResponse, runRouter,
|
||||
Router, Router'(LeafRouter))
|
||||
|
||||
|
||||
import Data.Time.Calendar
|
||||
import Data.ByteString.Lazy (ByteString)
|
||||
-- * test data types
|
||||
|
||||
data Person = Person {
|
||||
|
@ -86,6 +88,7 @@ tweety = Animal "Bird" 2
|
|||
spec :: Spec
|
||||
spec = do
|
||||
captureSpec
|
||||
captureDateSpec
|
||||
getSpec
|
||||
headSpec
|
||||
postSpec
|
||||
|
@ -128,6 +131,33 @@ captureSpec = do
|
|||
it "strips the captured path snippet from pathInfo" $ do
|
||||
get "/captured/foo" `shouldRespondWith` (fromString (show ["foo" :: String]))
|
||||
|
||||
type CaptureTimeApi = CaptureTime "date" "%Y-%m-%d" Day :> Get '[PlainText] String
|
||||
captureTimeApi :: Proxy CaptureTimeApi
|
||||
captureTimeApi = Proxy
|
||||
captureTimesServer :: Day -> ExceptT ServantErr IO String
|
||||
captureTimesServer = return . show
|
||||
|
||||
|
||||
captureDateSpec :: Spec
|
||||
captureDateSpec = do
|
||||
describe "Servant.API.Times(CaptureTime)" $ do
|
||||
with (return (serve captureTimeApi captureTimesServer)) $ do
|
||||
|
||||
it "can capture parts of the 'pathInfo'" $ do
|
||||
response <- get "/2015-12-02"
|
||||
liftIO $ simpleBody response `shouldBe` (fromString . show $ fromGregorian 2015 12 2 :: ByteString)
|
||||
|
||||
it "returns 404 if the decoding fails" $ do
|
||||
get "/notAnInt" `shouldRespondWith` 404
|
||||
|
||||
with (return (serve
|
||||
(Proxy :: Proxy (CaptureTime "date" "%Y-%m-%d" Day :> Raw))
|
||||
(\ day request_ respond ->
|
||||
respond $ responseLBS ok200 [] (cs $ show $ pathInfo request_)))) $ do
|
||||
it "strips the captured path snippet from pathInfo" $ do
|
||||
get "/2015-12-02/foo" `shouldRespondWith` (fromString (show ["foo" :: String]))
|
||||
|
||||
|
||||
|
||||
type GetApi = Get '[JSON] Person
|
||||
:<|> "empty" :> Get '[] ()
|
||||
|
|
Loading…
Reference in a new issue