Axios
We use axios to communicate with the server in Vue applications and most new code.
In order to guarantee all defaults are set you should not use axios
directly, you should import axios
from axios_utils
.
CSRF token
All our request require a CSRF token.
To guarantee this token is set, we are importing axios, setting the token, and exporting axios
.
This exported module should be used instead of directly using axios
to ensure the token is set.
Usage
import axios from './lib/utils/axios_utils';
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
});
Mock axios response in tests
To help us mock the responses we are using axios-mock-adapter.
Advantages over spyOn()
:
- no need to create response objects
- does not allow call through (which we want to avoid)
- simple API to test error cases
- provides
replyOnce()
to allow for different responses
We have also decided against using axios interceptors because they are not suitable for mocking.
Example
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(() => {
mock.restore();
});
Mock poll requests in tests with axios
Because polling function requires a header object, we need to always include an object as the third argument:
mock.onGet('/users').reply(200, { foo: 'bar' }, {});
Help and feedback
If there's something you don't like about this feature
To propose functionality that GitLab does not yet offer
To further help GitLab in shaping new features
If you didn't find what you were looking for
If you want help with something very specific to your use case, and can use some community support
POST ON GITLAB FORUM
If you have problems setting up or using this feature (depending on your GitLab subscription)
REQUEST SUPPORT
To view all GitLab tiers and features or to upgrade
If you want to try all features available in GitLab.com
If you want to try all features available in GitLab self-managed
If you spot an error or a need for improvement and would like to fix it yourself in a merge request
EDIT THIS PAGE
If you would like to suggest an improvement to this doc