Skip to content

Commit

Permalink
Merge pull request #99 from adamjmcgrath/http-proxy
Browse files Browse the repository at this point in the history
Fix http proxy with https server
  • Loading branch information
marcelduran committed Oct 16, 2017
2 parents 25ee9bd + 084ae3d commit 9520d45
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
9 changes: 6 additions & 3 deletions lib/webpagetest.js
Expand Up @@ -57,27 +57,30 @@ var filenames = {

// GET helper function
function get(config, pathname, proxy, callback, encoding) {
var protocol = (config.protocol === 'https:' ? https : http),
var protocol,
options;

if (proxy) {
var proxyUrl = url.parse(proxy);
var pathForProxy = config.protocol + '//';

if (config.auth) {
pathForProxy += config.auth + '@';
}

pathForProxy += config.hostname + ':' + config.port + pathname;
protocol = (proxyUrl.protocol === 'https:' ? https : http);

options = {
host: proxy.split(':')[0],
port: proxy.split(':')[1],
host: proxyUrl.hostname,
port: proxyUrl.port,
path: pathForProxy,
headers: {
Host: config.hostname
}
};
} else {
protocol = (config.protocol === 'https:' ? https : http);
options = {
path: pathname,
host: config.hostname,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -28,7 +28,7 @@
"node": ">=0.10.1"
},
"dependencies": {
"commander": "^2.9.0",
"commander": "2.10.0",
"csv": "^1.1.1",
"entities": "^1.1.1",
"mocha": "^3.4.1",
Expand Down
9 changes: 5 additions & 4 deletions test/proxy-test.js
Expand Up @@ -7,13 +7,14 @@

var assert = require('assert'),
http = require('http'),
https = require('https'),
url = require('url'),
WebPageTest = require('../lib/webpagetest'),
NockServer = require('./helpers/nock-server'),
ResponseObjects = require('./helpers/response-objects');

var wptNockServer = new NockServer('http://wpt.com'),
wpt = new WebPageTest('wpt.com');
var wptNockServer = new NockServer('https://wpt.com'),
wpt = new WebPageTest('https://wpt.com');

// proxy for test on 9001 port
http.createServer(function(req, res) {
Expand All @@ -24,7 +25,7 @@ http.createServer(function(req, res) {
body.push(data);
});
req.on('end', function() {
var orgreq = http.request({
var orgreq = https.request({
host: req.headers.host,
port: requestUrl.port || 80,
path: requestUrl.path,
Expand Down Expand Up @@ -52,7 +53,7 @@ describe('Run via proxy', function() {

it('gets a test status request', function(done) {
wpt.getTestStatus('120816_V2_2', {
proxy: '127.0.0.1:9001'
proxy: 'http://127.0.0.1:9001'
}, function (err, data) {
if (err) return done(err);
assert.deepEqual(data, ResponseObjects.testStatus);
Expand Down

0 comments on commit 9520d45

Please sign in to comment.