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

Make it possible to pass arguments to the xhr object #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ http.request = function (params, cb) {
params.host = params.host.split(':')[0];
}
if (!params.port) params.port = params.scheme == 'https' ? 443 : 80;

var req = new Request(new xhrHttp, params);

var xhrParams = params.xhrParams || {};

var req = new Request(new xhrHttp(xhrParams), params);
if (cb) req.on('response', cb);
return req;
};
Expand Down
22 changes: 21 additions & 1 deletion test/request_url.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ global.window = {
};

var noop = function() {};
global.window.XMLHttpRequest = function() {
global.window.XMLHttpRequest = function(xhrParams) {
this.open = noop;
this.send = noop;

if(!xhrParams) return;

Object.keys(xhrParams).forEach(function(key) {
this[key] = xhrParams[key];
}.bind(this));
};

var test = require('tape').test;
Expand Down Expand Up @@ -72,3 +78,17 @@ test('Test withCredentials param', function(t) {

t.end();
});

test('Test additional xhr params', function(t) {
var request = http.request({
url: '/api/foo',
xhrParams: {
mozSystem: true,
mozAnon: true
}
});

t.equal( request.xhr.mozSystem, true, 'xhr.mozSystem should be true');
t.equal( request.xhr.mozAnon, true, 'mozAnon should be true');
t.end();
});