Skip to content

Commit 8e1b5fe

Browse files
committed
Merge pull request #18 from sideroad/master
Be able to use proxy
2 parents 3746f28 + d8005bf commit 8e1b5fe

3 files changed

Lines changed: 76 additions & 8 deletions

File tree

lib/webpagetest.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,25 @@ var filenames = {
5555
};
5656

5757
// GET helper function
58-
function get(config, pathname, callback, encoding) {
58+
function get(config, pathname, proxy, callback, encoding) {
5959
var protocol = (config.protocol === 'https:' ? https : http),
60-
options = {
60+
options = proxy ? {
61+
host: proxy.split(':')[0],
62+
port: proxy.split(':')[1],
63+
path: config.protocol+ '//' +config.hostname+':' + config.port+pathname,
64+
headers: {
65+
Host: config.hostname
66+
}
67+
} :
68+
{
6169
path: pathname,
6270
host: config.hostname,
63-
port: config.port
71+
port: config.port,
72+
headers: {}
6473
};
6574

6675
if (encoding !== 'binary') {
67-
options.headers = {'accept-encoding': 'gzip,deflate'};
76+
options.headers['accept-encoding'] = 'gzip,deflate';
6877
}
6978

7079
return protocol.get(options, function getResponse(res) {
@@ -164,7 +173,7 @@ function api(pathname, callback, query, options) {
164173
} else {
165174

166175
// make the real API call
167-
get.call(this, config, pathname, function apiCallback(err, data, info) {
176+
get.call(this, config, pathname, options.proxy, function apiCallback(err, data, info) {
168177
if (!err) {
169178
try {
170179
if (options.parser) {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
},
3535
"dependencies": {
3636
"commander": "~2.1.0",
37-
"mocha": "~1.17.0",
38-
"xml2js": "~0.4.1",
3937
"csv": "~0.3.6",
40-
"entities": "~0.3.0"
38+
"entities": "~0.3.0",
39+
"mocha": "~1.17.0",
40+
"xml2js": "~0.4.1"
4141
},
4242
"devDependencies": {
4343
"nock": "~0.27.1"

test/proxy-test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (c) 2013, Twitter Inc.
3+
* Copyright (c) 2014, Google Inc.
4+
* Copyright (c) 2014, Marcel Duran and other contributors
5+
* Released under the MIT License
6+
*/
7+
8+
var assert = require('assert'),
9+
http = require('http'),
10+
url = require('url'),
11+
WebPageTest = require('../lib/webpagetest'),
12+
wpt = new WebPageTest();
13+
14+
// proxy for test on 5432 port
15+
http.createServer(function(req, res) {
16+
var requestUrl = url.parse(req.url);
17+
var body = [];
18+
19+
req.on('data', function(data) {
20+
body.push(data);
21+
});
22+
req.on('end', function() {
23+
var orgreq = http.request({
24+
host: req.headers.host,
25+
port: requestUrl.port || 80,
26+
path: requestUrl.path,
27+
method: req.method,
28+
headers: req.headers
29+
},
30+
function(orgres) {
31+
res.writeHead(orgres.statusCode, orgres.headers);
32+
orgres.on('data', function(chunk) {
33+
res.write(chunk);
34+
});
35+
orgres.on('end', function() {
36+
res.end();
37+
});
38+
});
39+
if(body.length > 0) {
40+
orgreq.write(body.join(''));
41+
}
42+
orgreq.end();
43+
});
44+
}).listen(5432);
45+
46+
describe('Run via proxy', function() {
47+
describe('An Example WebPageTest Server', function() {
48+
49+
it('gets a test status request', function(done) {
50+
wpt.getTestStatus('120816_V2_2', {
51+
proxy: 'localhost:5432'
52+
}, function (err, data) {
53+
if (err) return done(err);
54+
assert.equal(data.data.id, '120816_V2_2');
55+
done();
56+
});
57+
});
58+
});
59+
});

0 commit comments

Comments
 (0)