Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding possibility to pass custom data to request config #1952

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/core/mergeConfig.js
Expand Up @@ -38,7 +38,7 @@ module.exports = function mergeConfig(config1, config2) {
'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'maxContentLength',
'validateStatus', 'maxRedirects', 'httpAgent', 'httpsAgent', 'cancelToken',
'socketPath'
'socketPath', 'userData'
], function defaultToConfig2(prop) {
if (typeof config2[prop] !== 'undefined') {
config[prop] = config2[prop];
Expand Down
2 changes: 2 additions & 0 deletions lib/defaults.js
Expand Up @@ -95,4 +95,6 @@ utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE);
});

defaults.userData = {};

module.exports = defaults;
37 changes: 37 additions & 0 deletions test/unit/adapters/http.js
Expand Up @@ -646,6 +646,43 @@ describe('supports http with nodejs', function () {
});
});
});

it('should correctly handle empty userData in config', function (done) {
server = http.createServer(function (req, res) {
res.end();
}).listen(4444, function () {
// var userData = { key: 'value' };
axios.get('http://localhost:4444').then(function (res) {
assert.equal(res.config.userData !== undefined, true);
done();
});
});
});

it('should correctly handle userData in config on response', function (done) {
server = http.createServer(function (req, res) {
res.end();
}).listen(4444, function () {
var userData = { key: 'value' };
axios.get('http://localhost:4444', { userData: userData }).then(function (res) {
assert.equal(res.config.userData.key, 'value');
done();
});
});
});

it('should correctly handle userData in config on error', function (done) {
server = http.createServer(function (req, res) {
res.statusCode = 400;
res.end();
}).listen(4444, function () {
var userData = { key: 'value' };
axios.get('http://localhost:4444', { userData: userData }).catch(function (err) {
assert.equal(err.config.userData.key, 'value');
done();
});
});
});
});