2017-11-20 04:57:08 -05:00
|
|
|
# Axios
|
2019-07-24 09:10:06 -04:00
|
|
|
|
2020-06-10 14:09:15 -04:00
|
|
|
We use [Axios](https://github.com/axios/axios) to communicate with the server in Vue applications and most new code.
|
2017-11-20 04:57:08 -05:00
|
|
|
|
2020-06-10 14:09:15 -04:00
|
|
|
In order to guarantee all defaults are set you *should not use Axios directly*, you should import Axios from `axios_utils`.
|
2017-11-20 04:57:08 -05:00
|
|
|
|
|
|
|
## CSRF token
|
2019-07-24 09:10:06 -04:00
|
|
|
|
2020-06-10 14:09:15 -04:00
|
|
|
All our requests require a CSRF token.
|
|
|
|
To guarantee this token is set, we are importing [Axios](https://github.com/axios/axios), setting the token, and exporting `axios` .
|
2017-11-20 04:57:08 -05:00
|
|
|
|
2020-06-10 14:09:15 -04:00
|
|
|
This exported module should be used instead of directly using Axios to ensure the token is set.
|
2017-11-20 04:57:08 -05:00
|
|
|
|
|
|
|
## Usage
|
2019-07-24 09:10:06 -04:00
|
|
|
|
2017-11-20 04:57:08 -05:00
|
|
|
```javascript
|
2017-12-19 06:13:02 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-11-20 04:57:08 -05:00
|
|
|
|
|
|
|
axios.get(url)
|
|
|
|
.then((response) => {
|
|
|
|
// `data` is the response that was provided by the server
|
|
|
|
const data = response.data;
|
|
|
|
|
|
|
|
// `headers` the headers that the server responded with
|
|
|
|
// All header names are lower cased
|
|
|
|
const paginationData = response.headers;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//handle the error
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2020-06-10 14:09:15 -04:00
|
|
|
## Mock Axios response in tests
|
2017-11-20 04:57:08 -05:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
To help us mock the responses we are using [axios-mock-adapter](https://github.com/ctimmerm/axios-mock-adapter).
|
2017-11-20 04:57:08 -05:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
Advantages over [`spyOn()`](https://jasmine.github.io/api/edge/global.html#spyOn):
|
2018-01-23 07:10:36 -05:00
|
|
|
|
|
|
|
- no need to create response objects
|
|
|
|
- does not allow call through (which we want to avoid)
|
2019-08-19 19:41:23 -04:00
|
|
|
- simple API to test error cases
|
2018-01-23 07:10:36 -05:00
|
|
|
- provides `replyOnce()` to allow for different responses
|
|
|
|
|
2020-06-10 14:09:15 -04:00
|
|
|
We have also decided against using [Axios interceptors](https://github.com/axios/axios#interceptors) because they are not suitable for mocking.
|
2018-01-23 07:10:36 -05:00
|
|
|
|
|
|
|
### Example
|
2017-11-20 04:57:08 -05:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
|
|
|
|
let mock;
|
|
|
|
beforeEach(() => {
|
|
|
|
// This sets the mock adapter on the default instance
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
// Mock any GET request to /users
|
|
|
|
// arguments for reply are (status, data, headers)
|
|
|
|
mock.onGet('/users').reply(200, {
|
|
|
|
users: [
|
|
|
|
{ id: 1, name: 'John Smith' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2018-01-23 06:42:02 -05:00
|
|
|
mock.restore();
|
2017-11-20 04:57:08 -05:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2020-06-10 14:09:15 -04:00
|
|
|
### Mock poll requests in tests with Axios
|
2017-11-20 04:57:08 -05:00
|
|
|
|
2018-01-19 05:09:59 -05:00
|
|
|
Because polling function requires a header object, we need to always include an object as the third argument:
|
2017-11-20 04:57:08 -05:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
mock.onGet('/users').reply(200, { foo: 'bar' }, {});
|
|
|
|
```
|