Skip to content

Commit 3506234

Browse files
committed
Merge pull request #65 from callumacrae/http-auth
Added auth functionality to server option
2 parents ed4b371 + b8f5ac0 commit 3506234

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

lib/helper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ function normalizeServer(server) {
181181
return {
182182
protocol: server.protocol,
183183
hostname: server.hostname,
184+
auth: server.auth,
184185
pathname: server.pathname,
185186
port: parseInt(server.port, 10) || (server.protocol === 'https:' ? 443 : 80)
186187
};

lib/webpagetest.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,34 @@ var filenames = {
5858
// GET helper function
5959
function get(config, pathname, proxy, callback, encoding) {
6060
var protocol = (config.protocol === 'https:' ? https : http),
61-
options = proxy ? {
62-
host: proxy.split(':')[0],
63-
port: proxy.split(':')[1],
64-
path: config.protocol+ '//' +config.hostname+':' + config.port+pathname,
65-
headers: {
66-
Host: config.hostname
67-
}
68-
} :
69-
{
70-
path: pathname,
71-
host: config.hostname,
72-
port: config.port,
73-
headers: {}
74-
};
61+
options;
62+
63+
if (proxy) {
64+
var pathForProxy = config.protocol + '//';
65+
66+
if (config.auth) {
67+
pathForProxy += config.auth + '@';
68+
}
69+
70+
pathForProxy += config.hostname + ':' + config.port + pathname;
71+
72+
options = {
73+
host: proxy.split(':')[0],
74+
port: proxy.split(':')[1],
75+
path: pathForProxy,
76+
headers: {
77+
Host: config.hostname
78+
}
79+
};
80+
} else {
81+
options = {
82+
path: pathname,
83+
host: config.hostname,
84+
auth: config.auth,
85+
port: config.port,
86+
headers: {}
87+
};
88+
}
7589

7690
if (encoding !== 'binary') {
7791
options.headers['accept-encoding'] = 'gzip,deflate';

test/edge-cases-test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ describe('Edge Cases of', function() {
144144
it('when getting a valid standard server uri returns server info object', function() {
145145
var server = helper.normalizeServer('example.com');
146146
assert.deepEqual(server, {
147+
auth: null,
147148
protocol: 'http:',
148149
hostname: 'example.com',
149150
pathname: '/',
@@ -152,8 +153,9 @@ describe('Edge Cases of', function() {
152153
});
153154

154155
it('when getting a valid full server uri returns server info object', function() {
155-
var server = helper.normalizeServer('http://example.com:8000/foo');
156+
var server = helper.normalizeServer('http://foo:bar@example.com:8000/foo');
156157
assert.deepEqual(server, {
158+
auth: 'foo:bar',
157159
protocol: 'http:',
158160
hostname: 'example.com',
159161
pathname: '/foo',
@@ -164,6 +166,7 @@ describe('Edge Cases of', function() {
164166
it('when getting a https server uri returns server info object', function() {
165167
var server = helper.normalizeServer('https://example.com');
166168
assert.deepEqual(server, {
169+
auth: null,
167170
protocol: 'https:',
168171
hostname: 'example.com',
169172
pathname: '/',

0 commit comments

Comments
 (0)