|
2 | 2 | {-# LANGUAGE DataKinds #-}
|
3 | 3 | {-# LANGUAGE FlexibleContexts #-}
|
4 | 4 | {-# LANGUAGE FlexibleInstances #-}
|
5 |
| -{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | +{-# LANGUAGE MultiParamTypeClasses #-} |
6 | 6 | {-# LANGUAGE PolyKinds #-}
|
| 7 | +{-# LANGUAGE ScopedTypeVariables #-} |
7 | 8 | {-# LANGUAGE TypeFamilies #-}
|
8 | 9 | {-# LANGUAGE TypeOperators #-}
|
9 | 10 | {-# OPTIONS_GHC -fno-warn-orphans #-}
|
@@ -66,14 +67,15 @@ import Network.HTTP.Types.Status
|
66 | 67 | import Network.Wai
|
67 | 68 | import Servant
|
68 | 69 | import Servant.API.ContentTypes
|
| 70 | +import Servant.Server.Internal.Config |
69 | 71 | import Test.QuickCheck.Arbitrary (Arbitrary (..), vector)
|
70 | 72 | import Test.QuickCheck.Gen (Gen, generate)
|
71 | 73 |
|
72 | 74 | -- | 'HasMock' defines an interpretation of API types
|
73 | 75 | -- than turns them into random-response-generating
|
74 | 76 | -- request handlers, hence providing an instance for
|
75 | 77 | -- all the combinators of the core /servant/ library.
|
76 |
| -class HasServer api => HasMock api where |
| 78 | +class HasServer api config => HasMock api config where |
77 | 79 | -- | Calling this method creates request handlers of
|
78 | 80 | -- the right type to implement the API described by
|
79 | 81 | -- @api@ that just generate random response values of
|
@@ -103,65 +105,67 @@ class HasServer api => HasMock api where
|
103 | 105 | -- So under the hood, 'mock' uses the 'IO' bit to generate
|
104 | 106 | -- random values of type 'User' and 'Book' every time these
|
105 | 107 | -- endpoints are requested.
|
106 |
| - mock :: Proxy api -> Server api |
| 108 | + mock :: Proxy api -> Proxy config -> Server api |
107 | 109 |
|
108 |
| -instance (HasMock a, HasMock b) => HasMock (a :<|> b) where |
109 |
| - mock _ = mock (Proxy :: Proxy a) :<|> mock (Proxy :: Proxy b) |
| 110 | +instance (HasMock a config, HasMock b config) => HasMock (a :<|> b) config where |
| 111 | + mock _ config = mock (Proxy :: Proxy a) config :<|> mock (Proxy :: Proxy b) config |
110 | 112 |
|
111 |
| -instance (KnownSymbol path, HasMock rest) => HasMock (path :> rest) where |
| 113 | +instance (KnownSymbol path, HasMock rest config) => HasMock (path :> rest) config where |
112 | 114 | mock _ = mock (Proxy :: Proxy rest)
|
113 | 115 |
|
114 |
| -instance (KnownSymbol s, FromHttpApiData a, HasMock rest) => HasMock (Capture s a :> rest) where |
115 |
| - mock _ = \_ -> mock (Proxy :: Proxy rest) |
| 116 | +instance (KnownSymbol s, FromHttpApiData a, HasMock rest config) => HasMock (Capture s a :> rest) config where |
| 117 | + mock _ config = \_ -> mock (Proxy :: Proxy rest) config |
116 | 118 |
|
117 |
| -instance (AllCTUnrender ctypes a, HasMock rest) => HasMock (ReqBody ctypes a :> rest) where |
118 |
| - mock _ = \_ -> mock (Proxy :: Proxy rest) |
| 119 | +instance (AllCTUnrender ctypes a, HasMock rest config) => HasMock (ReqBody ctypes a :> rest) config where |
| 120 | + mock _ config = \_ -> mock (Proxy :: Proxy rest) config |
119 | 121 |
|
120 |
| -instance HasMock rest => HasMock (RemoteHost :> rest) where |
121 |
| - mock _ = \_ -> mock (Proxy :: Proxy rest) |
| 122 | +instance HasMock rest config => HasMock (RemoteHost :> rest) config where |
| 123 | + mock _ config = \_ -> mock (Proxy :: Proxy rest) config |
122 | 124 |
|
123 |
| -instance HasMock rest => HasMock (IsSecure :> rest) where |
124 |
| - mock _ = \_ -> mock (Proxy :: Proxy rest) |
| 125 | +instance HasMock rest config => HasMock (IsSecure :> rest) config where |
| 126 | + mock _ config = \_ -> mock (Proxy :: Proxy rest) config |
125 | 127 |
|
126 |
| -instance HasMock rest => HasMock (Vault :> rest) where |
127 |
| - mock _ = \_ -> mock (Proxy :: Proxy rest) |
| 128 | +instance HasMock rest config => HasMock (Vault :> rest) config where |
| 129 | + mock _ config = \_ -> mock (Proxy :: Proxy rest) config |
128 | 130 |
|
129 |
| -instance HasMock rest => HasMock (HttpVersion :> rest) where |
130 |
| - mock _ = \_ -> mock (Proxy :: Proxy rest) |
| 131 | +instance HasMock rest config => HasMock (HttpVersion :> rest) config where |
| 132 | + mock _ config = \_ -> mock (Proxy :: Proxy rest) config |
131 | 133 |
|
132 |
| -instance (KnownSymbol s, FromHttpApiData a, HasMock rest) |
133 |
| - => HasMock (QueryParam s a :> rest) where |
134 |
| - mock _ = \_ -> mock (Proxy :: Proxy rest) |
| 134 | +instance (KnownSymbol s, FromHttpApiData a, HasMock rest config) |
| 135 | + => HasMock (QueryParam s a :> rest) config where |
| 136 | + mock _ config = \_ -> mock (Proxy :: Proxy rest) config |
135 | 137 |
|
136 |
| -instance (KnownSymbol s, FromHttpApiData a, HasMock rest) |
137 |
| - => HasMock (QueryParams s a :> rest) where |
138 |
| - mock _ = \_ -> mock (Proxy :: Proxy rest) |
| 138 | +instance (KnownSymbol s, FromHttpApiData a, HasMock rest config) |
| 139 | + => HasMock (QueryParams s a :> rest) config where |
| 140 | + mock _ config = \_ -> mock (Proxy :: Proxy rest) config |
139 | 141 |
|
140 |
| -instance (KnownSymbol s, HasMock rest) => HasMock (QueryFlag s :> rest) where |
141 |
| - mock _ = \_ -> mock (Proxy :: Proxy rest) |
| 142 | +instance (KnownSymbol s, HasMock rest config) => HasMock (QueryFlag s :> rest) config where |
| 143 | + mock _ config = \_ -> mock (Proxy :: Proxy rest) config |
142 | 144 |
|
143 |
| -instance (KnownSymbol h, FromHttpApiData a, HasMock rest) => HasMock (Header h a :> rest) where |
144 |
| - mock _ = \_ -> mock (Proxy :: Proxy rest) |
| 145 | +instance (KnownSymbol h, FromHttpApiData a, HasMock rest config) => HasMock (Header h a :> rest) config where |
| 146 | + mock _ config = \_ -> mock (Proxy :: Proxy rest) config |
145 | 147 |
|
146 | 148 | instance (Arbitrary a, KnownNat status, ReflectMethod method, AllCTRender ctypes a)
|
147 |
| - => HasMock (Verb method status ctypes a) where |
148 |
| - mock _ = mockArbitrary |
| 149 | + => HasMock (Verb method status ctypes a) config where |
| 150 | + mock _ _ = mockArbitrary |
149 | 151 |
|
150 | 152 | instance OVERLAPPING_
|
151 | 153 | (GetHeaders (Headers headerTypes a), Arbitrary (HList headerTypes),
|
152 | 154 | Arbitrary a, KnownNat status, ReflectMethod method, AllCTRender ctypes a)
|
153 |
| - => HasMock (Verb method status ctypes (Headers headerTypes a)) where |
154 |
| - mock _ = mockArbitrary |
| 155 | + => HasMock (Verb method status ctypes (Headers headerTypes a)) config where |
| 156 | + mock _ _ = mockArbitrary |
155 | 157 |
|
156 |
| -instance HasMock Raw where |
157 |
| - mock _ = \_req respond -> do |
| 158 | +instance HasMock Raw config where |
| 159 | + mock _ _ = \_req respond -> do |
158 | 160 | bdy <- genBody
|
159 | 161 | respond $ responseLBS status200 [] bdy
|
160 | 162 |
|
161 | 163 | where genBody = pack <$> generate (vector 100 :: Gen [Char])
|
162 | 164 |
|
163 |
| -instance HasMock rest => HasMock (WithNamedConfig name config rest) where |
164 |
| - mock _ = mock (Proxy :: Proxy rest) |
| 165 | +instance (HasConfigEntry config (NamedConfig name subConfig), HasMock rest subConfig) => |
| 166 | + HasMock (WithNamedConfig name subConfig rest) config where |
| 167 | + |
| 168 | + mock _ _ = mock (Proxy :: Proxy rest) (Proxy :: Proxy subConfig) |
165 | 169 |
|
166 | 170 | mockArbitrary :: (MonadIO m, Arbitrary a) => m a
|
167 | 171 | mockArbitrary = liftIO (generate arbitrary)
|
|
0 commit comments