@@ -15,8 +15,10 @@ import Data.Monoid ((<>))
15import Data.Proxy (Proxy (Proxy))
15import Data.Proxy (Proxy (Proxy))
16import Data.String (fromString)
16import Data.String (fromString)
17import Data.String.Conversions (cs)
17import Data.String.Conversions (cs)
18+import qualified Data.Text as T
18import GHC.Generics (Generic)
19import GHC.Generics (Generic)
19−import Network.HTTP.Types (hContentType, methodDelete,
20+import Network.HTTP.Types (hAccept, hContentType,
21+ methodDelete, methodGet,
20 methodPatch, methodPost, methodPut,
22 methodPatch, methodPost, methodPut,
21 ok200, parseQuery, status409)
23 ok200, parseQuery, status409)
22import Network.Wai (Application, Request, pathInfo,
24import Network.Wai (Application, Request, pathInfo,
@@ -31,8 +33,9 @@ import Test.Hspec.Wai (get, liftIO, matchStatus, post,
31import Servant.API ((:<|>) (..), (:>), Capture, Delete,
33import Servant.API ((:<|>) (..), (:>), Capture, Delete,
32 Get, Header, JSON, MatrixFlag,
34 Get, Header, JSON, MatrixFlag,
33 MatrixParam, MatrixParams, Patch,
35 MatrixParam, MatrixParams, Patch,
34− Post, Put, QueryFlag, QueryParam,
36+ PlainText, Post, Put, QueryFlag,
35− QueryParams, Raw, ReqBody)
37+ QueryParam, QueryParams, Raw,
36import Servant.Server (Server, serve)
39import Servant.Server (Server, serve)
37import Servant.Server.Internal (RouteMismatch (..))
40import Servant.Server.Internal (RouteMismatch (..))
@@ -41,7 +44,7 @@ import Servant.Server.Internal (RouteMismatch (..))
42data Person = Person {
45data Person = Person {
43 name :: String,
46 name :: String,
44− age :: Integer
47+ age :: Integer
46 deriving (Eq, Show, Generic)
49 deriving (Eq, Show, Generic)
@@ -52,7 +55,7 @@ alice :: Person
52alice = Person "Alice" 42
55alice = Person "Alice" 42
54data Animal = Animal {
57data Animal = Animal {
55− species :: String,
58+ species :: String,
56 numberOfLegs :: Integer
59 numberOfLegs :: Integer
58 deriving (Eq, Show, Generic)
61 deriving (Eq, Show, Generic)
@@ -97,10 +100,13 @@ captureSpec :: Spec
97captureSpec = do
100captureSpec = do
98 describe "Servant.API.Capture" $ do
101 describe "Servant.API.Capture" $ do
99 with (return (serve captureApi captureServer)) $ do
102 with (return (serve captureApi captureServer)) $ do
100 it "can capture parts of the 'pathInfo'" $ do
104 it "can capture parts of the 'pathInfo'" $ do
101 response <- get "/2"
105 response <- get "/2"
103− 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
105 with (return (serve
111 with (return (serve
106 (Proxy :: Proxy (Capture "captured" String :> Raw))
112 (Proxy :: Proxy (Capture "captured" String :> Raw))
@@ -120,18 +126,23 @@ getSpec = do
120 describe "Servant.API.Get" $ do
126 describe "Servant.API.Get" $ do
121 let server = return alice :<|> return ()
127 let server = return alice :<|> return ()
122 with (return $ serve getApi server) $ do
128 with (return $ serve getApi server) $ do
123 it "allows to GET a Person" $ do
130 it "allows to GET a Person" $ do
124 response <- get "/"
131 response <- get "/"
125 return response `shouldRespondWith` 200
132 return response `shouldRespondWith` 200
127− decode' (simpleBody response) `shouldBe` Just alice
133+ liftIO $ decode' (simpleBody response) `shouldBe` Just alice
129 it "throws 405 (wrong method) on POSTs" $ do
135 it "throws 405 (wrong method) on POSTs" $ do
130 post "/" "" `shouldRespondWith` 405
136 post "/" "" `shouldRespondWith` 405
137+ post "/empty" "" `shouldRespondWith` 405
132 it "returns 204 if the type is '()'" $ do
139 it "returns 204 if the type is '()'" $ do
133 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
137type QueryParamApi = QueryParam "name" String :> Get '[JSON] Person
148type QueryParamApi = QueryParam "name" String :> Get '[JSON] Person
@@ -483,6 +494,10 @@ rawSpec = do
483type AlternativeApi =
494type AlternativeApi =
484 "foo" :> Get '[JSON] Person
495 "foo" :> Get '[JSON] Person
485 :<|> "bar" :> Get '[JSON] Animal
496 :<|> "bar" :> Get '[JSON] Animal
497+ :<|> "foo" :> Get '[PlainText] T.Text
498+ :<|> "bar" :> Post '[JSON] Animal
499+ :<|> "bar" :> Put '[JSON] Animal
500+ :<|> "bar" :> Delete
486unionApi :: Proxy AlternativeApi
501unionApi :: Proxy AlternativeApi
487unionApi = Proxy
502unionApi = Proxy
@@ -490,11 +505,16 @@ unionServer :: Server AlternativeApi
490unionServer =
505unionServer =
491 return alice
506 return alice
492 :<|> return jerry
507 :<|> return jerry
508+ :<|> return "a string"
494unionSpec :: Spec
513unionSpec :: Spec
495unionSpec = do
514unionSpec = do
496 describe "Servant.API.Alternative" $ do
515 describe "Servant.API.Alternative" $ do
497 with (return $ serve unionApi unionServer) $ do
516 with (return $ serve unionApi unionServer) $ do
498 it "unions endpoints" $ do
518 it "unions endpoints" $ do
499 response <- get "/foo"
519 response <- get "/foo"
500 liftIO $ do
520 liftIO $ do
@@ -505,6 +525,12 @@ unionSpec = do
505 decode' (simpleBody response_) `shouldBe`
525 decode' (simpleBody response_) `shouldBe`
506 Just jerry
526 Just jerry
528+ it "checks all endpoints before returning 415" $ do
529+ get "/foo" `shouldRespondWith` 200
531+ it "returns 404 if the path does not exist" $ do
532+ get "/nonexistent" `shouldRespondWith` 404
508-- | Test server error functionality.
534-- | Test server error functionality.
509errorsSpec :: Spec
535errorsSpec :: Spec
510errorsSpec = do
536errorsSpec = do