Skip to content

Commit 92a23cd

Browse files
committed
[js] Do not drop user auth info from the WebDriver server URL
Fixes #1167
1 parent c42999e commit 92a23cd

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

javascript/node/selenium-webdriver/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* FIXED: the `webdriver.promise.ControlFlow` now has a consistent execution
66
order for tasks/callbacks scheduled in different turns of the JS event loop.
77
Refer to the `webdriver.promise` documentation for more details.
8+
* FIXED: do not drop user auth from the WebDriver server URL.
89
* FIXED: a single `firefox.Binary` instance may be used to configure and
910
launch multiple FirefoxDriver sessions.
1011

javascript/node/selenium-webdriver/http/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var HttpClient = function(serverUrl, opt_agent, opt_proxy) {
5555
* @private {!Object}
5656
*/
5757
this.options_ = {
58+
auth: parsedUrl.auth,
5859
host: parsedUrl.hostname,
5960
path: parsedUrl.pathname,
6061
port: parsedUrl.port
@@ -81,6 +82,7 @@ HttpClient.prototype.send = function(httpRequest, callback) {
8182

8283
var options = {
8384
method: httpRequest.method,
85+
auth: this.options_.auth,
8486
host: this.options_.host,
8587
port: this.options_.port,
8688
path: path,

javascript/node/selenium-webdriver/test/http/http_test.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
var assert = require('assert');
1919
var http = require('http');
20+
var url = require('url');
2021

2122
var HttpClient = require('../../http').HttpClient;
2223
var HttpRequest = require('../../_base').require('webdriver.http.Request');
@@ -25,7 +26,7 @@ var promise = require('../..').promise;
2526
var test = require('../../lib/test');
2627

2728
describe('HttpClient', function() {
28-
this.timeout(4*1000);
29+
this.timeout(4 * 1000);
2930

3031
var server = new Server(function(req, res) {
3132
if (req.method == 'GET' && req.url == '/echo') {
@@ -44,6 +45,30 @@ describe('HttpClient', function() {
4445
res.writeHead(303, {});
4546
res.end();
4647

48+
} else if (req.method == 'GET' && req.url == '/protected') {
49+
var denyAccess = function() {
50+
res.writeHead(401, {'WWW-Authenticate': 'Basic realm="test"'});
51+
res.end('Access denied');
52+
};
53+
54+
var basicAuthRegExp = /^\s*basic\s+([a-z0-9\-\._~\+\/]+)=*\s*$/i
55+
var auth = req.headers.authorization;
56+
var match = basicAuthRegExp.exec(auth || '');
57+
if (!match) {
58+
denyAccess();
59+
return;
60+
}
61+
62+
var userNameAndPass = new Buffer(match[1], 'base64').toString();
63+
var parts = userNameAndPass.split(':', 2);
64+
if (parts[0] !== 'genie' && parts[1] !== 'bottle') {
65+
denyAccess();
66+
return;
67+
}
68+
69+
res.writeHead(200, {'content-type': 'text/plain'});
70+
res.end('Access granted!');
71+
4772
} else if (req.method == 'GET' && req.url == '/proxy') {
4873
res.writeHead(200, req.headers);
4974
res.end();
@@ -86,6 +111,30 @@ describe('HttpClient', function() {
86111
});
87112
});
88113

114+
test.it('can use basic auth', function() {
115+
var parsed = url.parse(server.url());
116+
parsed.auth = 'genie:bottle';
117+
118+
var client = new HttpClient(url.format(parsed));
119+
var request = new HttpRequest('GET', '/protected');
120+
return promise.checkedNodeCall(client.send.bind(client, request))
121+
.then(function(response) {
122+
assert.equal(200, response.status);
123+
assert.equal('text/plain', response.headers['content-type']);
124+
assert.equal('Access granted!', response.body);
125+
});
126+
});
127+
128+
test.it('fails requests missing required basic auth', function() {
129+
var client = new HttpClient(server.url());
130+
var request = new HttpRequest('GET', '/protected');
131+
return promise.checkedNodeCall(client.send.bind(client, request))
132+
.then(function(response) {
133+
assert.equal(401, response.status);
134+
assert.equal('Access denied', response.body);
135+
});
136+
});
137+
89138
test.it('automatically follows redirects', function() {
90139
var request = new HttpRequest('GET', '/redirect');
91140
var client = new HttpClient(server.url());

0 commit comments

Comments
 (0)