| Copyright | 2015 Alp Mestanogullari |
|---|---|
| License | BSD3 |
| Maintainer | Alp Mestanogullari <alpmestan@gmail.com> |
| Stability | experimental |
| Portability | portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Servant.Mock
Contents
Description
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::HasMockapi context =>Proxyapi ->Proxycontext ->Serverapi
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 $servemyAPI (mockmyAPI Proxy)
Synopsis
- class (HasServer api context, HasContextEntry (context .++ DefaultErrorFormatters) ErrorFormatters) => HasMock api context where
Documentation
class (HasServer api context, HasContextEntry (context .++ DefaultErrorFormatters) ErrorFormatters) => HasMock api context where Source #
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.
Methods
mock :: Proxy api -> Proxy context -> Server api Source #
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 actually "means" 2 request handlers, of the following types:Server API
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.