@@ -0,0 +1,189 @@
2+{-# LANGUAGE DataKinds #-}
3+{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TypeFamilies #-}
7+{-# LANGUAGE TypeOperators #-}
9+-- Module : Servant.Mock
10+-- Copyright : 2015 Alp Mestanogullari
13+-- Maintainer : Alp Mestanogullari <alpmestan@gmail.com>
14+-- Stability : experimental
15+-- Portability : portable
17+-- Automatically derive a mock webserver that implements some API type,
18+-- just from the said API type's definition.
20+-- Using this module couldn't be simpler. Given some API type, like:
22+-- > type API = "user" :> Get '[JSON] User
24+-- that describes your web application, all you have to do is define
27+-- > myAPI :: Proxy API
30+-- and call 'mock', which has the following type:
33+-- 'mock' :: 'HasMock' api => 'Proxy' api -> 'Server' api
36+-- What this says is, given some API type @api@ that it knows it can
37+-- "mock", 'mock' hands you an implementation of the API type. It does so
38+-- by having each request handler generate a random value of the
39+-- appropriate type (@User@ in our case). All you need for this to work is
40+-- to provide 'Arbitrary' instances for the data types returned as response
41+-- bodies, hence appearing next to 'Delete', 'Get', 'Patch', 'Post' and 'Put'.
43+-- To put this all to work and run the mock server, just call 'serve' on the
44+-- result of 'mock' to get an 'Application' that you can then run with warp.
48+-- main = Network.Wai.Handler.Warp.run 8080 $
49+-- 'serve' myAPI ('mock' myAPI)
51+module Servant.Mock ( HasMock(..) ) where
53+#if !MIN_VERSION_base(4,8,0)
54+import Control.Applicative
56+import Control.Monad.IO.Class
57+import Data.ByteString.Lazy.Char8 (pack)
60+import Network.HTTP.Types.Status
63+import Servant.API.ContentTypes
64+import Test.QuickCheck.Arbitrary (Arbitrary (..), vector)
65+import Test.QuickCheck.Gen (Gen, generate)
67+-- | 'HasMock' defines an interpretation of API types
68+-- than turns them into random-response-generating
69+-- request handlers, hence providing an instance for
70+-- all the combinators of the core /servant/ library.
71+class HasServer api => HasMock api where
72+ -- | Calling this method creates request handlers of
73+ -- the right type to implement the API described by
74+ -- @api@ that just generate random response values of
75+ -- the right type. E.g:
78+ -- type API = "user" :> Get '[JSON] User
79+ -- :<|> "book" :> Get '[JSON] Book
84+ -- -- let's say we will start with the frontend,
85+ -- -- and hence need a placeholder server
86+ -- server :: Server API
90+ -- What happens here is that @'Server' API@
91+ -- actually "means" 2 request handlers, of the following types:
94+ -- getUser :: EitherT ServantErr IO User
95+ -- getBook :: EitherT ServantErr IO Book
98+ -- So under the hood, 'mock' uses the 'IO' bit to generate
99+ -- random values of type 'User' and 'Book' every time these
100+ -- endpoints are requested.
101+ mock :: Proxy api -> Server api
103+instance (HasMock a, HasMock b) => HasMock (a :<|> b) where
104+ mock _ = mock (Proxy :: Proxy a) :<|> mock (Proxy :: Proxy b)
106+instance (KnownSymbol path, HasMock rest) => HasMock (path :> rest) where
107+ mock _ = mock (Proxy :: Proxy rest)
109+instance (KnownSymbol s, FromText a, HasMock rest) => HasMock (Capture s a :> rest) where
110+ mock _ = \_ -> mock (Proxy :: Proxy rest)
112+instance (AllCTUnrender ctypes a, HasMock rest) => HasMock (ReqBody ctypes a :> rest) where
113+ mock _ = \_ -> mock (Proxy :: Proxy rest)
115+instance HasMock rest => HasMock (RemoteHost :> rest) where
116+ mock _ = \_ -> mock (Proxy :: Proxy rest)
118+instance HasMock rest => HasMock (IsSecure :> rest) where
119+ mock _ = \_ -> mock (Proxy :: Proxy rest)
121+instance HasMock rest => HasMock (Vault :> rest) where
122+ mock _ = \_ -> mock (Proxy :: Proxy rest)
124+instance HasMock rest => HasMock (HttpVersion :> rest) where
125+ mock _ = \_ -> mock (Proxy :: Proxy rest)
127+instance (KnownSymbol s, FromText a, HasMock rest)
128+ => HasMock (QueryParam s a :> rest) where
129+ mock _ = \_ -> mock (Proxy :: Proxy rest)
131+instance (KnownSymbol s, FromText a, HasMock rest)
132+ => HasMock (QueryParams s a :> rest) where
133+ mock _ = \_ -> mock (Proxy :: Proxy rest)
135+instance (KnownSymbol s, HasMock rest) => HasMock (QueryFlag s :> rest) where
136+ mock _ = \_ -> mock (Proxy :: Proxy rest)
138+instance (KnownSymbol s, FromText a, HasMock rest)
139+ => HasMock (MatrixParam s a :> rest) where
140+ mock _ = \_ -> mock (Proxy :: Proxy rest)
142+instance (KnownSymbol s, FromText a, HasMock rest)
143+ => HasMock (MatrixParams s a :> rest) where
144+ mock _ = \_ -> mock (Proxy :: Proxy rest)
146+instance (KnownSymbol s, HasMock rest) => HasMock (MatrixFlag s :> rest) where
147+ mock _ = \_ -> mock (Proxy :: Proxy rest)
149+instance (KnownSymbol h, FromText a, HasMock rest) => HasMock (Header h a :> rest) where
150+ mock _ = \_ -> mock (Proxy :: Proxy rest)
152+instance (Arbitrary a, AllCTRender ctypes a) => HasMock (Delete ctypes a) where
153+ mock _ = mockArbitrary
155+instance (Arbitrary a, AllCTRender ctypes a) => HasMock (Get ctypes a) where
156+ mock _ = mockArbitrary
158+instance (Arbitrary a, AllCTRender ctypes a) => HasMock (Patch ctypes a) where
159+ mock _ = mockArbitrary
161+instance (Arbitrary a, AllCTRender ctypes a) => HasMock (Post ctypes a) where
162+ mock _ = mockArbitrary
164+instance (Arbitrary a, AllCTRender ctypes a) => HasMock (Put ctypes a) where
165+ mock _ = mockArbitrary
167+instance HasMock Raw where
168+ mock _ = \req respond -> do
170+ respond $ responseLBS status200 [] bdy
172+ where genBody = pack <$> generate (vector 100 :: Gen [Char])
174+mockArbitrary :: (MonadIO m, Arbitrary a) => m a
175+mockArbitrary = liftIO (generate arbitrary)
178+instance (Arbitrary (HList ls), Arbitrary a)
179+ => Arbitrary (Headers ls a) where
180+ arbitrary = Headers <$> arbitrary <*> arbitrary
182+instance Arbitrary (HList '[]) where
183+ arbitrary = pure HNil
185+instance (Arbitrary a, Arbitrary (HList hs))
186+ => Arbitrary (HList (Header h a ': hs)) where
187+ arbitrary = HCons <$> fmap Header arbitrary <*> arbitrary