Builds the Http mock.
The mocked Http implementation
Mocks delete requests. Works analog to onGetReply().
Mocks response exceptions for delete methods. It works like onDeleteReply(), but throws an HttpError instead
Mocks responses for get methods You may pass a specific endpoint as parameter to mock only selected endpoints. This is very useful, when having methods that do several Http requests, so you can mock them one on one.
The following code returns the same content on every get call
HttpMockBuilder
.create()
.onGetReply(200, {response: 'foo}) // mocks all get requests
.onPostReply(201, {response: 'foo}) // mocks all post requests
.build()
The next code returns the different content depending on the passed endpoint
HttpMockBuilder
.create()
.onGetReply(200, {response: 'foo}, '/url/specific') // mocks get request for '/url/specific'
.build()
The status to be returned
The data to be returned
If given, the mock applies for that specific url, other for all method calls
The builder instance (Fluent API)
Mocks response exceptions for get methods. It works like onGetReply(), but throws an HttpError instead
The status to be returned in exception object
The error message
Eventual data carried with the error object
The specific url for which the exception should be thrown
The builder instance (Fluent API)
Mocks post requests. Works analog to onGetReply().
Mocks response exceptions for post methods. It works like onPostReply(), but throws an HttpError instead
Mocks put requests. Works analog to onGetReply().
Mocks response exceptions for put methods. It works like onPutReply(), but throws an HttpError instead
Creates a builder instance
the builder
Http Mocker Builder for easy to http testing
Example:
const mockedHttp = HttpMockBuilder .create() .onGetReply(200, {foo: 'get'}) .onPostThrowError(500, 'Post Error', {e: 'post'}) .onPutThrowError(404, 'Put Error', {e: 'put'}) .onDeleteThrowError(403, 'Delete Error', {e: 'delete'}) .build(); const response = await mockedHttp.get('/url'); await mockedHttp.post('/url/post', {faz: 'post'}); // will throw exception