@@ -17,7 +17,8 @@ import Data.String (fromString)
17import Data.String.Conversions (cs)
17import Data.String.Conversions (cs)
18import qualified Data.Text as T
18import qualified Data.Text as T
19import GHC.Generics (Generic)
19import GHC.Generics (Generic)
20−import Network.HTTP.Types (hContentType, methodDelete,
20+import Network.HTTP.Types (hAccept, hContentType,
21+ methodDelete, methodGet,
21 methodPatch, methodPost, methodPut,
22 methodPatch, methodPost, methodPut,
22 ok200, parseQuery, status409)
23 ok200, parseQuery, status409)
23import Network.Wai (Application, Request, pathInfo,
24import Network.Wai (Application, Request, pathInfo,
@@ -43,7 +44,7 @@ import Servant.Server.Internal (RouteMismatch (..))
44data Person = Person {
45data Person = Person {
45 name :: String,
46 name :: String,
46− age :: Integer
47+ age :: Integer
48 deriving (Eq, Show, Generic)
49 deriving (Eq, Show, Generic)
@@ -54,7 +55,7 @@ alice :: Person
54alice = Person "Alice" 42
55alice = Person "Alice" 42
56data Animal = Animal {
57data Animal = Animal {
57− species :: String,
58+ species :: String,
58 numberOfLegs :: Integer
59 numberOfLegs :: Integer
60 deriving (Eq, Show, Generic)
61 deriving (Eq, Show, Generic)
@@ -99,10 +100,13 @@ captureSpec :: Spec
99captureSpec = do
100captureSpec = do
100 describe "Servant.API.Capture" $ do
101 describe "Servant.API.Capture" $ do
101 with (return (serve captureApi captureServer)) $ do
102 with (return (serve captureApi captureServer)) $ do
102 it "can capture parts of the 'pathInfo'" $ do
104 it "can capture parts of the 'pathInfo'" $ do
103 response <- get "/2"
105 response <- get "/2"
105− decode' (simpleBody response) `shouldBe` Just tweety
106+ liftIO $ decode' (simpleBody response) `shouldBe` Just tweety
108+ it "returns 404 if the decoding fails" $ do
109+ get "/notAnInt" `shouldRespondWith` 404
107 with (return (serve
111 with (return (serve
108 (Proxy :: Proxy (Capture "captured" String :> Raw))
112 (Proxy :: Proxy (Capture "captured" String :> Raw))
@@ -122,18 +126,23 @@ getSpec = do
122 describe "Servant.API.Get" $ do
126 describe "Servant.API.Get" $ do
123 let server = return alice :<|> return ()
127 let server = return alice :<|> return ()
124 with (return $ serve getApi server) $ do
128 with (return $ serve getApi server) $ do
125 it "allows to GET a Person" $ do
130 it "allows to GET a Person" $ do
126 response <- get "/"
131 response <- get "/"
127 return response `shouldRespondWith` 200
132 return response `shouldRespondWith` 200
129− decode' (simpleBody response) `shouldBe` Just alice
133+ liftIO $ decode' (simpleBody response) `shouldBe` Just alice
131 it "throws 405 (wrong method) on POSTs" $ do
135 it "throws 405 (wrong method) on POSTs" $ do
132 post "/" "" `shouldRespondWith` 405
136 post "/" "" `shouldRespondWith` 405
137+ post "/empty" "" `shouldRespondWith` 405
134 it "returns 204 if the type is '()'" $ do
139 it "returns 204 if the type is '()'" $ do
135 get "empty" `shouldRespondWith` ""{ matchStatus = 204 }
140 get "empty" `shouldRespondWith` ""{ matchStatus = 204 }
142+ it "returns 415 if the Accept header is not supported" $ do
143+ Test.Hspec.Wai.request methodGet "" [(hAccept, "crazy/mime")] ""
144+ `shouldRespondWith` 415
139type QueryParamApi = QueryParam "name" String :> Get '[JSON] Person
148type QueryParamApi = QueryParam "name" String :> Get '[JSON] Person
@@ -486,6 +495,9 @@ type AlternativeApi =
486 "foo" :> Get '[JSON] Person
495 "foo" :> Get '[JSON] Person
487 :<|> "bar" :> Get '[JSON] Animal
496 :<|> "bar" :> Get '[JSON] Animal
488 :<|> "foo" :> Get '[PlainText] T.Text
497 :<|> "foo" :> Get '[PlainText] T.Text
498+ :<|> "bar" :> Post '[JSON] Animal
499+ :<|> "bar" :> Put '[JSON] Animal
500+ :<|> "bar" :> Delete
489unionApi :: Proxy AlternativeApi
501unionApi :: Proxy AlternativeApi
490unionApi = Proxy
502unionApi = Proxy
@@ -494,11 +506,15 @@ unionServer =
494 return alice
506 return alice
495 :<|> return jerry
507 :<|> return jerry
496 :<|> return "a string"
508 :<|> return "a string"
498unionSpec :: Spec
513unionSpec :: Spec
499unionSpec = do
514unionSpec = do
500 describe "Servant.API.Alternative" $ do
515 describe "Servant.API.Alternative" $ do
501 with (return $ serve unionApi unionServer) $ do
516 with (return $ serve unionApi unionServer) $ do
502 it "unions endpoints" $ do
518 it "unions endpoints" $ do
503 response <- get "/foo"
519 response <- get "/foo"
504 liftIO $ do
520 liftIO $ do
@@ -508,9 +524,13 @@ unionSpec = do
508 liftIO $ do
524 liftIO $ do
509 decode' (simpleBody response_) `shouldBe`
525 decode' (simpleBody response_) `shouldBe`
510 Just jerry
526 Just jerry
511− it "checks all endpoints before returning 406" $ do
528+ it "checks all endpoints before returning 415" $ do
512 get "/foo" `shouldRespondWith` 200
529 get "/foo" `shouldRespondWith` 200
531+ it "returns 404 if the path does not exist" $ do
532+ get "/nonexistent" `shouldRespondWith` 404
514-- | Test server error functionality.
534-- | Test server error functionality.
515errorsSpec :: Spec
535errorsSpec :: Spec
516errorsSpec = do
536errorsSpec = do