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

Allow test frameworks to mock out XMLHttpRequest #69

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var http = module.exports;
var EventEmitter = require('events').EventEmitter;
var Request = require('./lib/request');
var url = require('url')
var url = require('url');

http.request = function (params, cb) {
if (typeof params === 'string') {
Expand Down Expand Up @@ -33,8 +33,8 @@ http.request = function (params, cb) {
params.host = params.host.split(':')[0];
}
if (!params.port) params.port = params.protocol == 'https:' ? 443 : 80;
var req = new Request(new xhrHttp, params);

var req = new Request(new (getXMLHttpRequest()), params);
if (cb) req.on('response', cb);
return req;
};
Expand All @@ -49,7 +49,7 @@ http.get = function (params, cb) {
http.Agent = function () {};
http.Agent.defaultMaxSockets = 4;

var xhrHttp = (function () {
var getXMLHttpRequest = function () {
if (typeof window === 'undefined') {
throw new Error('no window object present');
}
Expand Down Expand Up @@ -78,12 +78,12 @@ var xhrHttp = (function () {
}
catch (e) {}
}
throw new Error('ajax not supported in this browser')
throw new Error('ajax not supported in this browser');
}
else {
throw new Error('ajax not supported in this browser');
}
})();
};

http.STATUS_CODES = {
100 : 'Continue',
Expand Down Expand Up @@ -142,4 +142,4 @@ http.STATUS_CODES = {
509 : 'Bandwidth Limit Exceeded',
510 : 'Not Extended', // RFC 2774
511 : 'Network Authentication Required' // RFC 6585
};
};