2016-02-18 16:36:24 +01:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
2016-01-19 00:19:51 +01:00
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
2016-02-18 16:36:24 +01:00
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2015-09-23 18:26:05 +02:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2016-01-18 13:22:58 +01:00
|
|
|
|
|
|
|
#include "overlapping-compat.h"
|
|
|
|
|
2015-07-23 13:00:37 +02:00
|
|
|
-- |
|
|
|
|
-- Module : Servant.Mock
|
|
|
|
-- Copyright : 2015 Alp Mestanogullari
|
|
|
|
-- License : BSD3
|
|
|
|
--
|
|
|
|
-- Maintainer : Alp Mestanogullari <alpmestan@gmail.com>
|
|
|
|
-- Stability : experimental
|
|
|
|
-- Portability : portable
|
|
|
|
--
|
|
|
|
-- Automatically derive a mock webserver that implements some API type,
|
|
|
|
-- just from the said API type's definition.
|
|
|
|
--
|
|
|
|
-- Using this module couldn't be simpler. Given some API type, like:
|
|
|
|
--
|
|
|
|
-- > type API = "user" :> Get '[JSON] User
|
|
|
|
--
|
|
|
|
-- that describes your web application, all you have to do is define
|
|
|
|
-- a 'Proxy' to it:
|
|
|
|
--
|
|
|
|
-- > myAPI :: Proxy API
|
|
|
|
-- > myAPI = Proxy
|
|
|
|
--
|
|
|
|
-- and call 'mock', which has the following type:
|
|
|
|
--
|
|
|
|
-- @
|
2016-04-11 15:54:38 +02:00
|
|
|
-- 'mock' :: 'HasMock' api context => 'Proxy' api -> 'Proxy' context -> 'Server' api
|
2015-07-23 13:00:37 +02:00
|
|
|
-- @
|
|
|
|
--
|
|
|
|
-- What this says is, given some API type @api@ that it knows it can
|
|
|
|
-- "mock", 'mock' hands you an implementation of the API type. It does so
|
|
|
|
-- by having each request handler generate a random value of the
|
|
|
|
-- appropriate type (@User@ in our case). All you need for this to work is
|
|
|
|
-- to provide 'Arbitrary' instances for the data types returned as response
|
|
|
|
-- bodies, hence appearing next to 'Delete', 'Get', 'Patch', 'Post' and 'Put'.
|
|
|
|
--
|
|
|
|
-- To put this all to work and run the mock server, just call 'serve' on the
|
|
|
|
-- result of 'mock' to get an 'Application' that you can then run with warp.
|
|
|
|
--
|
|
|
|
-- @
|
|
|
|
-- main :: IO ()
|
|
|
|
-- main = Network.Wai.Handler.Warp.run 8080 $
|
2016-04-10 18:03:48 +02:00
|
|
|
-- 'serve' myAPI ('mock' myAPI Proxy)
|
2015-07-23 13:00:37 +02:00
|
|
|
-- @
|
2015-07-22 21:22:42 +02:00
|
|
|
module Servant.Mock ( HasMock(..) ) where
|
|
|
|
|
2015-07-22 22:06:46 +02:00
|
|
|
#if !MIN_VERSION_base(4,8,0)
|
2015-08-17 23:50:42 +02:00
|
|
|
import Control.Applicative
|
2015-07-22 22:06:46 +02:00
|
|
|
#endif
|
2015-08-17 23:50:42 +02:00
|
|
|
import Control.Monad.IO.Class
|
|
|
|
import Data.ByteString.Lazy.Char8 (pack)
|
|
|
|
import Data.Proxy
|
|
|
|
import GHC.TypeLits
|
|
|
|
import Network.HTTP.Types.Status
|
|
|
|
import Network.Wai
|
|
|
|
import Servant
|
|
|
|
import Servant.API.ContentTypes
|
|
|
|
import Test.QuickCheck.Arbitrary (Arbitrary (..), vector)
|
|
|
|
import Test.QuickCheck.Gen (Gen, generate)
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2015-07-23 13:00:37 +02:00
|
|
|
-- | 'HasMock' defines an interpretation of API types
|
|
|
|
-- than turns them into random-response-generating
|
|
|
|
-- request handlers, hence providing an instance for
|
|
|
|
-- all the combinators of the core /servant/ library.
|
2016-02-28 23:23:32 +01:00
|
|
|
class HasServer api context => HasMock api context where
|
2015-07-23 13:00:37 +02:00
|
|
|
-- | Calling this method creates request handlers of
|
|
|
|
-- the right type to implement the API described by
|
|
|
|
-- @api@ that just generate random response values of
|
|
|
|
-- the right type. E.g:
|
|
|
|
--
|
|
|
|
-- @
|
|
|
|
-- type API = "user" :> Get '[JSON] User
|
|
|
|
-- :<|> "book" :> Get '[JSON] Book
|
|
|
|
--
|
|
|
|
-- api :: Proxy API
|
|
|
|
-- api = Proxy
|
|
|
|
--
|
|
|
|
-- -- let's say we will start with the frontend,
|
|
|
|
-- -- and hence need a placeholder server
|
|
|
|
-- server :: Server API
|
2016-04-10 18:03:48 +02:00
|
|
|
-- server = mock api Proxy
|
2015-07-23 13:00:37 +02:00
|
|
|
-- @
|
|
|
|
--
|
|
|
|
-- What happens here is that @'Server' API@
|
|
|
|
-- actually "means" 2 request handlers, of the following types:
|
|
|
|
--
|
|
|
|
-- @
|
2016-04-07 23:34:23 +02:00
|
|
|
-- getUser :: Handler User
|
|
|
|
-- getBook :: Handler Book
|
2015-07-23 13:00:37 +02:00
|
|
|
-- @
|
|
|
|
--
|
|
|
|
-- So under the hood, 'mock' uses the 'IO' bit to generate
|
|
|
|
-- random values of type 'User' and 'Book' every time these
|
|
|
|
-- endpoints are requested.
|
2016-02-28 23:23:32 +01:00
|
|
|
mock :: Proxy api -> Proxy context -> Server api
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance (HasMock a context, HasMock b context) => HasMock (a :<|> b) context where
|
|
|
|
mock _ context = mock (Proxy :: Proxy a) context :<|> mock (Proxy :: Proxy b) context
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance (KnownSymbol path, HasMock rest context) => HasMock (path :> rest) context where
|
2015-07-22 21:22:42 +02:00
|
|
|
mock _ = mock (Proxy :: Proxy rest)
|
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance (KnownSymbol s, FromHttpApiData a, HasMock rest context) => HasMock (Capture s a :> rest) context where
|
|
|
|
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-05-26 17:55:02 +02:00
|
|
|
instance (KnownSymbol s, FromHttpApiData a, HasMock rest context) => HasMock (CaptureAll s a :> rest) context where
|
|
|
|
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance (AllCTUnrender ctypes a, HasMock rest context) => HasMock (ReqBody ctypes a :> rest) context where
|
|
|
|
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance HasMock rest context => HasMock (RemoteHost :> rest) context where
|
|
|
|
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance HasMock rest context => HasMock (IsSecure :> rest) context where
|
|
|
|
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance HasMock rest context => HasMock (Vault :> rest) context where
|
|
|
|
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance HasMock rest context => HasMock (HttpVersion :> rest) context where
|
|
|
|
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance (KnownSymbol s, FromHttpApiData a, HasMock rest context)
|
|
|
|
=> HasMock (QueryParam s a :> rest) context where
|
|
|
|
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance (KnownSymbol s, FromHttpApiData a, HasMock rest context)
|
|
|
|
=> HasMock (QueryParams s a :> rest) context where
|
|
|
|
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance (KnownSymbol s, HasMock rest context) => HasMock (QueryFlag s :> rest) context where
|
|
|
|
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance (KnownSymbol h, FromHttpApiData a, HasMock rest context) => HasMock (Header h a :> rest) context where
|
|
|
|
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-01-06 18:20:20 +01:00
|
|
|
instance (Arbitrary a, KnownNat status, ReflectMethod method, AllCTRender ctypes a)
|
2016-02-28 23:23:32 +01:00
|
|
|
=> HasMock (Verb method status ctypes a) context where
|
2016-01-19 00:19:51 +01:00
|
|
|
mock _ _ = mockArbitrary
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-01-18 13:22:58 +01:00
|
|
|
instance OVERLAPPING_
|
|
|
|
(GetHeaders (Headers headerTypes a), Arbitrary (HList headerTypes),
|
|
|
|
Arbitrary a, KnownNat status, ReflectMethod method, AllCTRender ctypes a)
|
2016-02-28 23:23:32 +01:00
|
|
|
=> HasMock (Verb method status ctypes (Headers headerTypes a)) context where
|
2016-01-19 00:19:51 +01:00
|
|
|
mock _ _ = mockArbitrary
|
2016-01-18 13:22:58 +01:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance HasMock Raw context where
|
2016-01-19 00:19:51 +01:00
|
|
|
mock _ _ = \_req respond -> do
|
2015-07-22 21:22:42 +02:00
|
|
|
bdy <- genBody
|
|
|
|
respond $ responseLBS status200 [] bdy
|
|
|
|
|
2015-08-17 23:50:42 +02:00
|
|
|
where genBody = pack <$> generate (vector 100 :: Gen [Char])
|
2015-07-22 21:22:42 +02:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
instance (HasContextEntry context (NamedContext name subContext), HasMock rest subContext) =>
|
|
|
|
HasMock (WithNamedContext name subContext rest) context where
|
2016-01-19 00:19:51 +01:00
|
|
|
|
2016-02-28 23:23:32 +01:00
|
|
|
mock _ _ = mock (Proxy :: Proxy rest) (Proxy :: Proxy subContext)
|
2016-01-18 21:27:19 +01:00
|
|
|
|
2015-07-22 21:22:42 +02:00
|
|
|
mockArbitrary :: (MonadIO m, Arbitrary a) => m a
|
|
|
|
mockArbitrary = liftIO (generate arbitrary)
|
|
|
|
|
|
|
|
-- utility instance
|
|
|
|
instance (Arbitrary (HList ls), Arbitrary a)
|
|
|
|
=> Arbitrary (Headers ls a) where
|
|
|
|
arbitrary = Headers <$> arbitrary <*> arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary (HList '[]) where
|
|
|
|
arbitrary = pure HNil
|
|
|
|
|
|
|
|
instance (Arbitrary a, Arbitrary (HList hs))
|
|
|
|
=> Arbitrary (HList (Header h a ': hs)) where
|
|
|
|
arbitrary = HCons <$> fmap Header arbitrary <*> arbitrary
|
2016-07-08 09:11:34 +02:00
|
|
|
|
|
|
|
instance Arbitrary NoContent where
|
|
|
|
arbitrary = pure NoContent
|