git.iko.soy/typeable/servant
on master help sign in
Error order tests
commit9c62a3b150eca9185c029ef72eb875da789ae4d8 authorJulian K. Arni <jkarni@gmail.com> dateWed 9 Sep 2015, 13:29 (11 years ago) parentc970b2b
1 file changed+78
servant-server/test/Servant/Server/ErrorSpec.hs+78
servant-server/test/Servant/Server/ErrorSpec.hsnew file+78
@@ -0,0 +1,78 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE TypeOperators #-}
3+{-# LANGUAGE DataKinds #-}
4+module Servant.Server.ErrorSpec (spec) where
5+
6+import Test.Hspec
7+import Data.Proxy
8+import Test.Hspec.Wai (request, with, shouldRespondWith)
9+import Network.HTTP.Types (methodGet, methodPost)
10+import Data.Aeson (encode)
11+
12+import Servant
13+
14+
15+-- 1) Check whether one or more endpoints have the right path. Otherwise return 404.
16+-- 2) Check whether the one of those have the right method. Otherwise return
17+-- 405. If so, pick the first. We've now commited to calling at most one handler. *
18+-- 3) Check whether the Content-Type is known. Otherwise return 415.
19+-- 4) Check whether that one deserializes the body. Otherwise return 400. If there
20+-- was no Content-Type, try the first one of the API content-type list.
21+-- 5) Check whether the request is authorized. Otherwise return a 401.
22+-- 6) Check whether the request is forbidden. If so return 403.
23+-- 7) Check whether the request has a known Accept. Otherwise return 406.
24+-- 8) Check whether Accept-Language, Accept-Charset and Accept-Encoding exist and
25+-- match. We can follow the webmachine order here.
26+-- 9) Call the handler. Whatever it returns, we return.
27+
28+spec :: Spec
29+spec = do
30+ errorOrder
31+
32+
33+type ErrorOrderApi = "home"
34+ :> ReqBody '[JSON] Int
35+ :> Capture "t" Int
36+ :> Post '[JSON] Int
37+
38+errorOrderApi :: Proxy ErrorOrderApi
39+errorOrderApi = Proxy
40+
41+errorOrderServer :: Server ErrorOrderApi
42+errorOrderServer = \_ _ -> return 10
43+
44+errorOrder :: Spec
45+errorOrder = describe "HTTP error order"
46+ $ with (return $ serve errorOrderApi errorOrderServer) $ do
47+ let badContentType = ("Content-Type", "text/plain")
48+ badAccept = ("Accept", "text/plain")
49+ badMethod = methodGet
50+ badUrl = "home/nonexistent"
51+ badBody = "nonsense"
52+ goodContentType = ("Content-Type", "application/json")
53+ goodAccept = ("Accept", "application/json")
54+ goodMethod = methodPost
55+ goodUrl = "home/5"
56+ goodBody = encode (5 :: Int)
57+
58+ it "has 404 as its highest priority error" $ do
59+ request badMethod badUrl [badContentType, badAccept] badBody
60+ `shouldRespondWith` 404
61+
62+ it "has 405 as its second highest priority error" $ do
63+ request badMethod goodUrl [badContentType, badAccept] badBody
64+ `shouldRespondWith` 405
65+
66+ it "has 415 as its third highest priority error" $ do
67+ request goodMethod goodUrl [badContentType, badAccept] badBody
68+ `shouldRespondWith` 415
69+
70+ it "has 400 as its fourth highest priority error" $ do
71+ request goodMethod goodUrl [goodContentType, badAccept] badBody
72+ `shouldRespondWith` 400
73+
74+ it "has 406 as its fifth highest priority error" $ do
75+ request goodMethod goodUrl [goodContentType, badAccept] goodBody
76+ `shouldRespondWith` 406
77+
78+