on master help sign in
add headers test
commit84e3e65fe87a460325c5ef74101c1e37015d5a17 authorMark Wotton <mwotton@gmail.com> dateMon 14 Sep 2020, 12:07 (6 years ago) parent73ebfe0
4 files changed+64−11
package.yaml+3−3 roboservant.cabal+9−8 test/Headers.hs+47 test/Spec.hs+5
package.yaml+3−3
@@ -20,10 +20,10 @@ dependencies:
2020- containers
2121- hedgehog
2222- mtl
23−- servant >= 0.17
24−- servant-client >= 0.17
23+- servant # >= 0.17
24+- servant-client # >= 0.17
2525- servant-flatten
26−- servant-server >= 0.17
26+- servant-server # >= 0.17
2727- string-conversions
2828
2929ghc-options: -Wall
roboservant.cabal+9−8
@@ -1,10 +1,10 @@
11cabal-version: 1.12
22
3−-- This file has been generated from package.yaml by hpack version 0.31.2.
3+-- This file has been generated from package.yaml by hpack version 0.33.0.
44--
55-- see: https://github.com/sol/hpack
66--
7−-- hash: 179b83962de30f5bcf8b13d2f0648b301a753367094171f21a68e60b5cb4e4d5
7+-- hash: 0bfe1c9487789846b43d871520e7e546842a8ff9acb003e426d5282dac1e6aa1
88
99name: roboservant
1010version: 0.1.0.2
@@ -42,10 +42,10 @@ library
4242 , containers
4343 , hedgehog
4444 , mtl
45− , servant >=0.17
46− , servant-client >=0.17
45+ , servant
46+ , servant-client
4747 , servant-flatten
48− , servant-server >=0.17
48+ , servant-server
4949 , string-conversions
5050 default-language: Haskell2010
5151
@@ -54,6 +54,7 @@ test-suite roboservant-test
5454 main-is: Spec.hs
5555 other-modules:
5656 Foo
57+ Headers
5758 UnsafeIO
5859 Paths_roboservant
5960 hs-source-dirs:
@@ -67,9 +68,9 @@ test-suite roboservant-test
6768 , hedgehog
6869 , mtl
6970 , roboservant
70− , servant >=0.17
71− , servant-client >=0.17
71+ , servant
72+ , servant-client
7273 , servant-flatten
73− , servant-server >=0.17
74+ , servant-server
7475 , string-conversions
7576 default-language: Haskell2010
test/Headers.hsnew file+47
@@ -0,0 +1,47 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE DerivingStrategies #-}
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE TypeOperators #-}
7+
8+module Headers where
9+
10+import Data.Aeson
11+import Data.Typeable (Typeable)
12+import GHC.Generics (Generic)
13+import Servant
14+
15+newtype Foo = Foo Int
16+ deriving (Generic, Eq, Show, Typeable)
17+ deriving newtype (FromHttpApiData, ToHttpApiData)
18+
19+instance ToJSON Foo
20+
21+instance FromJSON Foo
22+
23+type Api =
24+ "item" :> Get '[JSON] Foo
25+ :<|> "itemAdd" :> Header "one" Foo :> Header "two" Foo :> Get '[JSON] Foo
26+ :<|> "item" :> Capture "itemId" Foo :> Get '[JSON] ()
27+
28+intro :: Handler Foo
29+intro = pure (Foo 1)
30+
31+combine :: Maybe Foo -> Maybe Foo -> Handler Foo
32+combine (Just (Foo a)) (Just (Foo b)) = pure (Foo (a + b))
33+combine (Just a) Nothing = pure a
34+combine Nothing (Just a) = pure a
35+combine Nothing Nothing = pure (Foo 1)
36+
37+
38+eliminate :: Foo -> Handler ()
39+eliminate (Foo a)
40+ | a > 10 = throwError $ err500 {errBody = "eliminate blew up, oh no!"}
41+ | otherwise = pure ()
42+
43+server :: Server Api
44+server =
45+ intro
46+ :<|> combine
47+ :<|> eliminate
test/Spec.hs+5
@@ -2,6 +2,7 @@
22{-# LANGUAGE TypeApplications #-}
33
44import qualified Foo
5+import qualified Headers
56import Hedgehog (Group (..), checkSequential, withTests)
67import qualified Roboservant as RS
78import qualified UnsafeIO
@@ -17,6 +18,10 @@ main :: IO ()
1718main = do
1819 assert "should find an error in Foo" . not
1920 =<< checkSequential (Group "Foo" [("Foo", withTests 100000 $ RS.prop_sequential @Foo.FooApi Foo.fooServer)])
21+
22+ assert "should find an error in Headers" . not
23+ =<< checkSequential (Group "Headers" [("Headers", withTests 10000 $ RS.prop_sequential @Headers.Api Headers.server)])
24+
2025 -- The UnsafeIO checker does not actually really use the contextually aware stuff, though it
2126 -- could: it's mostly here to show how to test for concurrency problems.
2227 unsafeServer <- UnsafeIO.makeServer