Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use http.request, not deprecated http.createClient

  • Loading branch information...
commit 9ba77cd028c0503b39e2025b9f7ce175cbaf5cee 1 parent b35bab3
@emanchado emanchado authored
Showing with 124 additions and 101 deletions.
  1. +3 −3 lib/heads.js
  2. +24 −19 lib/utils.js
  3. +62 −42 test/head-test.js
  4. +35 −37 test/helpers.js
View
6 lib/heads.js
@@ -172,8 +172,8 @@ var RoboHydraHeadProxy = function(props) {
RoboHydraHead.call(this, props);
this.mountPath = this.normalizePath(props.mountPath || '/');
- this.httpCreateClientFunction =
- props.httpCreateClientFunction || http.createClient;
+ this.httpRequestFunction =
+ props.httpRequestFunction || http.request;
};
for (p in RoboHydraHead.prototype) {
RoboHydraHeadProxy.prototype[p] = RoboHydraHead.prototype[p];
@@ -192,7 +192,7 @@ RoboHydraHeadProxy.prototype._handle = function(req, res) {
proxyUrl.pathname = proxyUrl.pathname.replace(new RegExp('/$'), '') +
'/' + requestUrlExtra;
proxyRequest(req, res, proxyUrl,
- {httpCreateClientFunction: this.httpCreateClientFunction});
+ {httpRequestFunction: this.httpRequestFunction});
};
exports.InvalidRoboHydraHeadException = InvalidRoboHydraHeadException;
View
43 lib/utils.js
@@ -38,8 +38,8 @@ function serveStaticFile(filePath, res, opts) {
function proxyRequest(req, res, proxyTo, opts) {
opts = opts || {};
- var httpCreateClientFunction = opts.httpCreateClientFunction ||
- http.createClient;
+ var httpRequestFunction = opts.httpRequestFunction ||
+ http.request;
var proxyUrl = proxyTo;
if (typeof(proxyTo) === 'string') {
@@ -50,29 +50,34 @@ function proxyRequest(req, res, proxyTo, opts) {
(proxyUrl.protocol === 'https:' ? 443 : 80);
var proxyToPath = proxyUrl.pathname;
- var proxy = httpCreateClientFunction(proxyToPort, proxyToHost),
- proxyReq = proxy.request(req.method, proxyToPath, req.headers);
+ var proxyReq = httpRequestFunction(
+ {host: proxyToHost,
+ port: proxyToPort,
+ method: req.method,
+ path: proxyToPath,
+ headers: req.headers},
+ function (proxyRes) {
+ // Copy over headers and status code from proxied request
+ res.statusCode = proxyRes.statusCode;
+ res.headers = proxyRes.headers;
- proxy.on('error', function (err) {
- res.statusCode = 502;
- res.send('Bad Gateway! Could not proxy request. Invalid host or proxy destination down?');
- });
+ proxyRes.on("data", function (chunk) {
+ res.write(chunk);
+ });
- proxyReq.addListener("response", function (proxyRes) {
- // Copy over headers and status code from proxied request
- res.statusCode = proxyRes.statusCode;
- res.headers = proxyRes.headers;
-
- proxyRes.addListener("data", function (chunk) {
- res.write(chunk, "binary");
+ proxyRes.on("end", function () {
+ res.end();
+ });
});
- proxyRes.addListener("end", function () {
- res.end();
- });
+ proxyReq.on('error', function (err) {
+ res.statusCode = 502;
+ res.send('Bad Gateway! Could not proxy request. Invalid host or proxy destination down?');
});
- proxyReq.write(req.rawBody, "binary");
+ if (req.rawBody) {
+ proxyReq.write(req.rawBody);
+ }
proxyReq.end();
}
View
104 test/head-test.js
@@ -1,10 +1,10 @@
/*global require, describe, it, expect*/
var buster = require("buster");
-var helpers = require("./helpers"),
- checkRouting = helpers.checkRouting,
- withResponse = helpers.withResponse,
- fakeFs = helpers.fakeFs,
- fakeHttpCreateClient = helpers.fakeHttpCreateClient;
+var helpers = require("./helpers"),
+ checkRouting = helpers.checkRouting,
+ withResponse = helpers.withResponse,
+ fakeFs = helpers.fakeFs,
+ fakeHttpRequest = helpers.fakeHttpRequest;
var heads = require("../lib/heads"),
RoboHydraHead = heads.RoboHydraHead,
RoboHydraHeadStatic = heads.RoboHydraHeadStatic,
@@ -467,11 +467,13 @@ describe("Proxying RoboHydra heads", function() {
});
it("proxy from default mountPath = /", function(done) {
- var fakeHttpCC = fakeHttpCreateClient(function(m, p, h) {
+ var fakeHttpR = fakeHttpRequest(function(m, p, h) {
return "Proxied " + m + " response for " + p;
});
- var head = new RoboHydraHeadProxy({proxyTo: 'http://example.com/mounted',
- httpCreateClientFunction: fakeHttpCC});
+ var head = new RoboHydraHeadProxy({
+ proxyTo: 'http://example.com/mounted',
+ httpRequestFunction: fakeHttpR
+ });
checkRouting(head, [
['/', 'Proxied GET response for /mounted/'],
@@ -480,12 +482,14 @@ describe("Proxying RoboHydra heads", function() {
});
it("ignore the path property (hint: it's mountPath)", function(done) {
- var fakeHttpCC = fakeHttpCreateClient(function(m, p, h) {
+ var fakeHttpR = fakeHttpRequest(function(m, p, h) {
return "Proxied " + m + " response for " + p;
});
- var head = new RoboHydraHeadProxy({path: '/foo',
- proxyTo: 'http://example.com/mounted',
- httpCreateClientFunction: fakeHttpCC});
+ var head = new RoboHydraHeadProxy({
+ path: '/foo',
+ proxyTo: 'http://example.com/mounted',
+ httpRequestFunction: fakeHttpR
+ });
checkRouting(head, [
['/', 'Proxied GET response for /mounted/'],
@@ -494,15 +498,19 @@ describe("Proxying RoboHydra heads", function() {
});
it("can proxy simple GET requests", function(done) {
- var fakeHttpCC = fakeHttpCreateClient(function(m, p, h) {
+ var fakeHttpR = fakeHttpRequest(function(m, p, h) {
return "Proxied " + m + " response for " + p;
});
- var head = new RoboHydraHeadProxy({mountPath: '/foobar',
- proxyTo: 'http://example.com/mounted',
- httpCreateClientFunction: fakeHttpCC});
- var head2 = new RoboHydraHeadProxy({mountPath: '/foobar',
- proxyTo: 'http://example.com/mounted/',
- httpCreateClientFunction: fakeHttpCC});
+ var head = new RoboHydraHeadProxy({
+ mountPath: '/foobar',
+ proxyTo: 'http://example.com/mounted',
+ httpRequestFunction: fakeHttpR
+ });
+ var head2 = new RoboHydraHeadProxy({
+ mountPath: '/foobar',
+ proxyTo: 'http://example.com/mounted/',
+ httpRequestFunction: fakeHttpR
+ });
checkRouting(head, [
['/foobar/', 'Proxied GET response for /mounted/'],
@@ -518,12 +526,14 @@ describe("Proxying RoboHydra heads", function() {
});
it("can proxy GET requests with parameters", function(done) {
- var fakeHttpCC = fakeHttpCreateClient(function(m, p, h) {
+ var fakeHttpR = fakeHttpRequest(function(m, p, h) {
return "Proxied " + m + " response for " + p;
});
- var head = new RoboHydraHeadProxy({mountPath: '/foobar',
- proxyTo: 'http://example.com/mounted',
- httpCreateClientFunction: fakeHttpCC});
+ var head = new RoboHydraHeadProxy({
+ mountPath: '/foobar',
+ proxyTo: 'http://example.com/mounted',
+ httpRequestFunction: fakeHttpR
+ });
checkRouting(head, [
['/foobar/?var=val&lang=scala', 'Proxied GET response for /mounted/?var=val&lang=scala']
@@ -531,15 +541,19 @@ describe("Proxying RoboHydra heads", function() {
});
it("can proxy simple GET requests to a site's root path", function(done) {
- var fakeHttpCC = fakeHttpCreateClient(function(m, p, h) {
+ var fakeHttpR = fakeHttpRequest(function(m, p, h) {
return "Proxied " + m + " response for " + p;
});
- var head = new RoboHydraHeadProxy({mountPath: '/foobar',
- proxyTo: 'http://example.com',
- httpCreateClientFunction: fakeHttpCC});
- var head2 = new RoboHydraHeadProxy({mountPath: '/foobar',
- proxyTo: 'http://example.com/',
- httpCreateClientFunction: fakeHttpCC});
+ var head = new RoboHydraHeadProxy({
+ mountPath: '/foobar',
+ proxyTo: 'http://example.com',
+ httpRequestFunction: fakeHttpR
+ });
+ var head2 = new RoboHydraHeadProxy({
+ mountPath: '/foobar',
+ proxyTo: 'http://example.com/',
+ httpRequestFunction: fakeHttpR
+ });
checkRouting(head, [
['/foobar/', 'Proxied GET response for /'],
@@ -555,14 +569,16 @@ describe("Proxying RoboHydra heads", function() {
});
it("can proxy simple POST requests", function(done) {
- var fakeHttpCC = fakeHttpCreateClient(function(m, p, h, data) {
+ var fakeHttpR = fakeHttpRequest(function(m, p, h, data) {
var res = "Proxied " + m + " response for " + p;
return res + (typeof(data) === 'undefined' ? '' :
' with data "' + data + '"');
});
- var head = new RoboHydraHeadProxy({mountPath: '/foobar',
- proxyTo: 'http://example.com/mounted',
- httpCreateClientFunction: fakeHttpCC});
+ var head = new RoboHydraHeadProxy({
+ mountPath: '/foobar',
+ proxyTo: 'http://example.com/mounted',
+ httpRequestFunction: fakeHttpR
+ });
checkRouting(head, [
[{path: '/foobar/',
@@ -581,14 +597,16 @@ describe("Proxying RoboHydra heads", function() {
});
it("can proxy POST requests with GET parameters", function(done) {
- var fakeHttpCC = fakeHttpCreateClient(function(m, p, h, data) {
+ var fakeHttpR = fakeHttpRequest(function(m, p, h, data) {
var res = "Proxied " + m + " response for " + p;
return res + (typeof(data) === 'undefined' ? '' :
" with data \"" + data + "\"");
});
- var head = new RoboHydraHeadProxy({mountPath: '/foobar',
- proxyTo: 'http://example.com/mounted',
- httpCreateClientFunction: fakeHttpCC});
+ var head = new RoboHydraHeadProxy({
+ mountPath: '/foobar',
+ proxyTo: 'http://example.com/mounted',
+ httpRequestFunction: fakeHttpR
+ });
checkRouting(head, [
[{path: '/foobar/?getparam=value',
@@ -599,15 +617,17 @@ describe("Proxying RoboHydra heads", function() {
});
it("can proxy requests to non-standard ports", function(done) {
- var fakeHttpCC = fakeHttpCreateClient(function(m, p, h, d, host, port) {
+ var fakeHttpR = fakeHttpRequest(function(m, p, h, d, host, port) {
var res = "Proxied " + m + " response for " + host + ":" + port +
" -> " + p;
return res + (typeof(d) === 'undefined' ? '' :
" with data \"" + d + "\"");
});
- var head = new RoboHydraHeadProxy({mountPath: '/foobar',
- proxyTo: 'http://example.com:3000/',
- httpCreateClientFunction: fakeHttpCC});
+ var head = new RoboHydraHeadProxy({
+ mountPath: '/foobar',
+ proxyTo: 'http://example.com:3000/',
+ httpRequestFunction: fakeHttpR
+ });
checkRouting(head, [
['/foobar/', 'Proxied GET response for example.com:3000 -> /']
View
72 test/helpers.js
@@ -104,39 +104,37 @@ function fakeFs(fileMap) {
};
}
-function fakeHttpCreateClient(responseFunction) {
- return function(p, h) {
+function fakeHttpRequest(requestDispatcher) {
+ return function(options, resCallback) {
+ var h = options.host;
+ var p = options.port;
+ var method = options.method || 'GET';
+ var path = options.path;
+ var headers = options.headers || {};
+
return {
- request: function(method, path, headers) {
- return {
- handlers: [],
-
- addListener: function(event, handler) {
- this.handlers[event] = handler;
- },
-
- write: function(data, mode) {
- this.data = data;
- },
-
- end: function() {
- var self = this;
- this.handlers.response({
- addListener: function(event, handler) {
- self.handlers[event] = handler;
- if (event === 'data') {
- self.handlers[event](responseFunction(method, path, headers, self.data, h, p));
- }
- if (event === 'end') {
- self.handlers[event]();
- }
- }
- });
- }
- };
- },
+ handlers: [],
on: function(event, handler) {
+ this.handlers[event] = handler;
+ },
+
+ write: function(data, mode) {
+ this.data = data;
+ },
+
+ end: function() {
+ var self = this;
+ resCallback({
+ on: function(event, handler) {
+ if (event === 'data') {
+ handler(new Buffer(requestDispatcher(method, path, headers, self.data, h, p)));
+ }
+ if (event === 'end') {
+ handler();
+ }
+ }
+ });
}
};
};
@@ -172,10 +170,10 @@ function headWithFail(path, hydraUtils, assertionMessage) {
});
}
-exports.withResponse = withResponse;
-exports.checkRouting = checkRouting;
-exports.fakeFs = fakeFs;
-exports.fakeHttpCreateClient = fakeHttpCreateClient;
-exports.fakeReq = fakeReq;
-exports.headWithFail = headWithFail;
-exports.headWithPass = headWithPass;
+exports.withResponse = withResponse;
+exports.checkRouting = checkRouting;
+exports.fakeFs = fakeFs;
+exports.fakeHttpRequest = fakeHttpRequest;
+exports.fakeReq = fakeReq;
+exports.headWithFail = headWithFail;
+exports.headWithPass = headWithPass;
Please sign in to comment.
Something went wrong with that request. Please try again.