Merge pull request #600 from phadej/remove-servant-mock
Move servant-mock to https://github.com/haskell-servant/servant-mock
This commit is contained in:
commit
1842de4775
14 changed files with 0 additions and 427 deletions
|
@ -1 +0,0 @@
|
|||
:set -Wall -itest -isrc -optP-include -optPdist/build/autogen/cabal_macros.h -Iinclude
|
|
@ -1,30 +0,0 @@
|
|||
Copyright (c) 2015-2016, Servant Contributors
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer in the documentation and/or other materials provided
|
||||
with the distribution.
|
||||
|
||||
* Neither the name of Alp Mestanogullari nor the names of other
|
||||
contributors may be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@ -1,2 +0,0 @@
|
|||
import Distribution.Simple
|
||||
main = defaultMain
|
|
@ -1,26 +0,0 @@
|
|||
{-# LANGUAGE DataKinds #-}
|
||||
{-# LANGUAGE DeriveGeneric #-}
|
||||
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||
{-# LANGUAGE TypeOperators #-}
|
||||
|
||||
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
|
||||
|
||||
import Data.Aeson
|
||||
import GHC.Generics
|
||||
import Network.Wai.Handler.Warp
|
||||
import Servant
|
||||
import Servant.Mock
|
||||
import Test.QuickCheck.Arbitrary
|
||||
|
||||
newtype User = User { username :: String }
|
||||
deriving (Eq, Show, Arbitrary, Generic)
|
||||
|
||||
instance ToJSON User
|
||||
|
||||
type API = "user" :> Get '[JSON] User
|
||||
|
||||
api :: Proxy API
|
||||
api = Proxy
|
||||
|
||||
main :: IO ()
|
||||
main = run 8080 (serve api $ mock api Proxy)
|
|
@ -1,8 +0,0 @@
|
|||
#if __GLASGOW_HASKELL__ >= 710
|
||||
#define OVERLAPPABLE_ {-# OVERLAPPABLE #-}
|
||||
#define OVERLAPPING_ {-# OVERLAPPING #-}
|
||||
#else
|
||||
{-# LANGUAGE OverlappingInstances #-}
|
||||
#define OVERLAPPABLE_
|
||||
#define OVERLAPPING_
|
||||
#endif
|
|
@ -1,72 +0,0 @@
|
|||
name: servant-mock
|
||||
version: 0.8.1
|
||||
synopsis: Derive a mock server for free from your servant API types
|
||||
description:
|
||||
Derive a mock server for free from your servant API types
|
||||
.
|
||||
See the @Servant.Mock@ module for the documentation and an example.
|
||||
homepage: http://github.com/haskell-servant/servant
|
||||
license: BSD3
|
||||
license-file: LICENSE
|
||||
author: Servant Contributors
|
||||
maintainer: haskell-servant-maintainers@googlegroups.com
|
||||
copyright: 2015-2016 Servant Contributors
|
||||
category: Web
|
||||
build-type: Simple
|
||||
extra-source-files: include/*.h
|
||||
cabal-version: >=1.10
|
||||
bug-reports: http://github.com/haskell-servant/servant/issues
|
||||
source-repository head
|
||||
type: git
|
||||
location: http://github.com/haskell-servant/servant.git
|
||||
|
||||
flag example
|
||||
description: Build the example too
|
||||
default: True
|
||||
|
||||
library
|
||||
exposed-modules:
|
||||
Servant.Mock
|
||||
build-depends:
|
||||
base >=4.7 && <5,
|
||||
bytestring >= 0.10 && <0.11,
|
||||
http-types >= 0.8 && <0.10,
|
||||
servant == 0.8.*,
|
||||
servant-server == 0.8.*,
|
||||
transformers >= 0.3 && <0.6,
|
||||
QuickCheck >= 2.7 && <2.10,
|
||||
wai >= 3.0 && <3.3
|
||||
hs-source-dirs: src
|
||||
default-language: Haskell2010
|
||||
include-dirs: include
|
||||
ghc-options: -Wall
|
||||
|
||||
executable mock-app
|
||||
main-is: main.hs
|
||||
hs-source-dirs: example
|
||||
default-language: Haskell2010
|
||||
build-depends: aeson, base, servant-mock, servant-server, QuickCheck, warp
|
||||
if flag(example)
|
||||
buildable: True
|
||||
else
|
||||
buildable: False
|
||||
ghc-options: -Wall
|
||||
|
||||
test-suite spec
|
||||
type: exitcode-stdio-1.0
|
||||
ghc-options: -Wall
|
||||
default-language: Haskell2010
|
||||
hs-source-dirs: test
|
||||
main-is: Spec.hs
|
||||
other-modules:
|
||||
Servant.MockSpec
|
||||
build-depends:
|
||||
base,
|
||||
hspec,
|
||||
hspec-wai,
|
||||
QuickCheck,
|
||||
servant,
|
||||
servant-server,
|
||||
servant-mock,
|
||||
aeson,
|
||||
wai
|
|
@ -1,188 +0,0 @@
|
|||
{-# LANGUAGE CPP #-}
|
||||
{-# LANGUAGE DataKinds #-}
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
{-# LANGUAGE FlexibleInstances #-}
|
||||
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||
{-# LANGUAGE PolyKinds #-}
|
||||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
{-# LANGUAGE TypeFamilies #-}
|
||||
{-# LANGUAGE TypeOperators #-}
|
||||
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
||||
|
||||
#include "overlapping-compat.h"
|
||||
|
||||
-- |
|
||||
-- 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:
|
||||
--
|
||||
-- @
|
||||
-- 'mock' :: 'HasMock' api context => 'Proxy' api -> 'Proxy' context -> 'Server' api
|
||||
-- @
|
||||
--
|
||||
-- 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 $
|
||||
-- 'serve' myAPI ('mock' myAPI Proxy)
|
||||
-- @
|
||||
module Servant.Mock ( HasMock(..) ) where
|
||||
|
||||
#if !MIN_VERSION_base(4,8,0)
|
||||
import Control.Applicative
|
||||
#endif
|
||||
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)
|
||||
|
||||
-- | '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.
|
||||
class HasServer api context => HasMock api context where
|
||||
-- | 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
|
||||
-- server = mock api Proxy
|
||||
-- @
|
||||
--
|
||||
-- What happens here is that @'Server' API@
|
||||
-- actually "means" 2 request handlers, of the following types:
|
||||
--
|
||||
-- @
|
||||
-- getUser :: Handler User
|
||||
-- getBook :: Handler Book
|
||||
-- @
|
||||
--
|
||||
-- So under the hood, 'mock' uses the 'IO' bit to generate
|
||||
-- random values of type 'User' and 'Book' every time these
|
||||
-- endpoints are requested.
|
||||
mock :: Proxy api -> Proxy context -> Server api
|
||||
|
||||
instance (HasMock a context, HasMock b context) => HasMock (a :<|> b) context where
|
||||
mock _ context = mock (Proxy :: Proxy a) context :<|> mock (Proxy :: Proxy b) context
|
||||
|
||||
instance (KnownSymbol path, HasMock rest context) => HasMock (path :> rest) context where
|
||||
mock _ = mock (Proxy :: Proxy rest)
|
||||
|
||||
instance (KnownSymbol s, FromHttpApiData a, HasMock rest context) => HasMock (Capture s a :> rest) context where
|
||||
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
||||
|
||||
instance (KnownSymbol s, FromHttpApiData a, HasMock rest context) => HasMock (CaptureAll s a :> rest) context where
|
||||
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
||||
|
||||
instance (AllCTUnrender ctypes a, HasMock rest context) => HasMock (ReqBody ctypes a :> rest) context where
|
||||
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
||||
|
||||
instance HasMock rest context => HasMock (RemoteHost :> rest) context where
|
||||
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
||||
|
||||
instance HasMock rest context => HasMock (IsSecure :> rest) context where
|
||||
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
||||
|
||||
instance HasMock rest context => HasMock (Vault :> rest) context where
|
||||
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
||||
|
||||
instance HasMock rest context => HasMock (HttpVersion :> rest) context where
|
||||
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
||||
|
||||
instance (KnownSymbol s, FromHttpApiData a, HasMock rest context)
|
||||
=> HasMock (QueryParam s a :> rest) context where
|
||||
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
||||
|
||||
instance (KnownSymbol s, FromHttpApiData a, HasMock rest context)
|
||||
=> HasMock (QueryParams s a :> rest) context where
|
||||
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
||||
|
||||
instance (KnownSymbol s, HasMock rest context) => HasMock (QueryFlag s :> rest) context where
|
||||
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
||||
|
||||
instance (KnownSymbol h, FromHttpApiData a, HasMock rest context) => HasMock (Header h a :> rest) context where
|
||||
mock _ context = \_ -> mock (Proxy :: Proxy rest) context
|
||||
|
||||
instance (Arbitrary a, KnownNat status, ReflectMethod method, AllCTRender ctypes a)
|
||||
=> HasMock (Verb method status ctypes a) context where
|
||||
mock _ _ = mockArbitrary
|
||||
|
||||
instance OVERLAPPING_
|
||||
(GetHeaders (Headers headerTypes a), Arbitrary (HList headerTypes),
|
||||
Arbitrary a, KnownNat status, ReflectMethod method, AllCTRender ctypes a)
|
||||
=> HasMock (Verb method status ctypes (Headers headerTypes a)) context where
|
||||
mock _ _ = mockArbitrary
|
||||
|
||||
instance HasMock Raw context where
|
||||
mock _ _ = \_req respond -> do
|
||||
bdy <- genBody
|
||||
respond $ responseLBS status200 [] bdy
|
||||
|
||||
where genBody = pack <$> generate (vector 100 :: Gen [Char])
|
||||
|
||||
instance (HasContextEntry context (NamedContext name subContext), HasMock rest subContext) =>
|
||||
HasMock (WithNamedContext name subContext rest) context where
|
||||
|
||||
mock _ _ = mock (Proxy :: Proxy rest) (Proxy :: Proxy subContext)
|
||||
|
||||
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
|
||||
|
||||
instance Arbitrary NoContent where
|
||||
arbitrary = pure NoContent
|
|
@ -1,86 +0,0 @@
|
|||
{-# LANGUAGE ConstraintKinds #-}
|
||||
{-# LANGUAGE DataKinds #-}
|
||||
{-# LANGUAGE DeriveGeneric #-}
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE TypeOperators #-}
|
||||
|
||||
module Servant.MockSpec where
|
||||
|
||||
import Data.Aeson as Aeson
|
||||
import Data.Proxy
|
||||
import GHC.Generics
|
||||
import Network.Wai
|
||||
import Servant.API
|
||||
import Test.Hspec hiding (pending)
|
||||
import Test.Hspec.Wai
|
||||
import Test.QuickCheck
|
||||
|
||||
import Servant
|
||||
import Servant.API.Internal.Test.ComprehensiveAPI
|
||||
import Servant.Mock
|
||||
|
||||
-- This declaration simply checks that all instances are in place.
|
||||
_ = mock comprehensiveAPI (Proxy :: Proxy '[NamedContext "foo" '[]])
|
||||
|
||||
data Body
|
||||
= Body
|
||||
| ArbitraryBody
|
||||
deriving (Generic)
|
||||
|
||||
instance ToJSON Body
|
||||
|
||||
instance Arbitrary Body where
|
||||
arbitrary = return ArbitraryBody
|
||||
|
||||
data TestHeader
|
||||
= TestHeader
|
||||
| ArbitraryHeader
|
||||
deriving (Show)
|
||||
|
||||
instance ToHttpApiData TestHeader where
|
||||
toHeader = toHeader . show
|
||||
toUrlPiece = toUrlPiece . show
|
||||
toQueryParam = toQueryParam . show
|
||||
|
||||
|
||||
instance Arbitrary TestHeader where
|
||||
arbitrary = return ArbitraryHeader
|
||||
|
||||
spec :: Spec
|
||||
spec = do
|
||||
describe "mock" $ do
|
||||
context "Get" $ do
|
||||
let api :: Proxy (Get '[JSON] Body)
|
||||
api = Proxy
|
||||
app = serve api (mock api Proxy)
|
||||
with (return app) $ do
|
||||
it "serves arbitrary response bodies" $ do
|
||||
get "/" `shouldRespondWith` 200{
|
||||
matchBody = Just $ Aeson.encode ArbitraryBody
|
||||
}
|
||||
|
||||
context "response headers" $ do
|
||||
let withHeader :: Proxy (Get '[JSON] (Headers '[Header "foo" TestHeader] Body))
|
||||
withHeader = Proxy
|
||||
withoutHeader :: Proxy (Get '[JSON] (Headers '[] Body))
|
||||
withoutHeader = Proxy
|
||||
toApp :: (HasMock api '[]) => Proxy api -> IO Application
|
||||
toApp api = return $ serve api (mock api (Proxy :: Proxy '[]))
|
||||
with (toApp withHeader) $ do
|
||||
it "serves arbitrary response bodies" $ do
|
||||
get "/" `shouldRespondWith` 200{
|
||||
matchHeaders = return $ MatchHeader $ \ h ->
|
||||
if h == [("Content-Type", "application/json"), ("foo", "ArbitraryHeader")]
|
||||
then Nothing
|
||||
else Just ("headers not correct\n")
|
||||
}
|
||||
|
||||
with (toApp withoutHeader) $ do
|
||||
it "works for no additional headers" $ do
|
||||
get "/" `shouldRespondWith` 200{
|
||||
matchHeaders = return $ MatchHeader $ \ h ->
|
||||
if h == [("Content-Type", "application/json")]
|
||||
then Nothing
|
||||
else Just ("headers not correct\n")
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
|
|
@ -1,5 +0,0 @@
|
|||
dependencies:
|
||||
- name: servant
|
||||
path: ../servant
|
||||
- name: servant-server
|
||||
path: ../servant-server
|
|
@ -4,4 +4,3 @@ servant-client
|
|||
servant-docs
|
||||
servant-foreign
|
||||
servant-js
|
||||
servant-mock
|
||||
|
|
|
@ -5,7 +5,6 @@ packages:
|
|||
- servant-docs/
|
||||
- servant-foreign/
|
||||
- servant-js/
|
||||
- servant-mock/
|
||||
- servant-server/
|
||||
extra-deps:
|
||||
- base-compat-0.9.1
|
||||
|
@ -18,10 +17,6 @@ extra-deps:
|
|||
- hspec-expectations-0.7.2
|
||||
- http-api-data-0.2.2
|
||||
- primitive-0.6.1.0
|
||||
- servant-0.7.1
|
||||
- servant-client-0.7.1
|
||||
- servant-docs-0.7.1
|
||||
- servant-server-0.7.1
|
||||
- should-not-typecheck-2.1.0
|
||||
- time-locale-compat-0.1.1.1
|
||||
- wai-app-static-3.1.5
|
||||
|
|
|
@ -5,7 +5,6 @@ packages:
|
|||
- servant-docs/
|
||||
- servant-foreign/
|
||||
- servant-js/
|
||||
- servant-mock/
|
||||
- servant-server/
|
||||
extra-deps: []
|
||||
flags: {}
|
||||
|
|
|
@ -5,7 +5,6 @@ packages:
|
|||
- servant-docs/
|
||||
- servant-foreign/
|
||||
- servant-js/
|
||||
- servant-mock/
|
||||
- servant-server/
|
||||
- doc/tutorial
|
||||
extra-deps:
|
||||
|
|
Loading…
Reference in a new issue