Skip to content

Commit

Permalink
Merge pull request #162 from kodie/master
Browse files Browse the repository at this point in the history
Implement redirect mocking
  • Loading branch information
ctimmerm committed Dec 25, 2018
2 parents 355ae84 + 447c64b commit ae6d725
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -52,7 +52,7 @@ var MockAdapter = require('axios-mock-adapter');
// This sets the mock adapter on the default instance
var mock = new MockAdapter(axios);

// Mock GET request to /users when param `searchText` is 'John'
// Mock GET request to /users when param `searchText` is 'John'
// arguments for reply are (status, data, headers)
mock.onGet('/users', { params: { searchText: 'John' } }).reply(200, {
users: [
Expand Down Expand Up @@ -117,6 +117,14 @@ mock.onGet('/users').reply(function(config) {
});
```

Passing a function to `reply` that returns an axios request, essentially mocking a redirect

```js
mock.onPost('/foo').reply(function(config) {
return axios.get('/bar');
});
```

Using a regex

```js
Expand Down
6 changes: 5 additions & 1 deletion src/handle_request.js
Expand Up @@ -55,7 +55,11 @@ function handleRequest(mockAdapter, resolve, reject, config) {
} else {
result.then(
function(result) {
utils.settle(resolve, reject, makeResponse(result, config), mockAdapter.delayResponse);
if (result.config && result.status) {
utils.settle(resolve, reject, makeResponse([result.status, result.data, result.headers], result.config), 0);
} else {
utils.settle(resolve, reject, makeResponse(result, config), mockAdapter.delayResponse);
}
},
function(error) {
if (mockAdapter.delayResponse > 0) {
Expand Down
14 changes: 14 additions & 0 deletions test/basics.spec.js
Expand Up @@ -78,6 +78,20 @@ describe('MockAdapter basics', function() {
});
});

it('accepts a callback that returns an axios request', function() {
mock
.onGet('/bar').reply(200, { foo: 'bar' })
.onGet('/foo').reply(function() {
return instance.get('/bar')
});

return instance.get('/foo').then(function(response) {
expect(response.status).to.equal(200);
expect(response.config.url).to.equal('/bar');
expect(response.data.foo).to.equal('bar');
});
});

it('matches on a regex', function() {
mock.onGet(/\/fo+/).reply(200);

Expand Down

0 comments on commit ae6d725

Please sign in to comment.