Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixing bug where we are redirected back to our site from an external …

…site
  • Loading branch information...
commit 8b620365e0e3e8612b5f626f5b79e94d5169c467 1 parent 4e4335c
@smacintyre smacintyre authored
Showing with 29 additions and 0 deletions.
  1. +6 −0 lib/browser.js
  2. +23 −0 test/browser.navigation.test.js
View
6 lib/browser.js
@@ -30,6 +30,7 @@ var startingPort = 9000;
* Options:
*
* - `external` enable fetching and evaluation of external resources [false]
+ * - `externalHost` hostname if redirect back to from external site [undefined]
*
* @param {String|http.Server|Number} html
* @param {Object|String} options
@@ -47,6 +48,7 @@ var Browser = module.exports = exports = function Browser(html, options, c) {
// Initialize
options = options || {};
this.external = options.external;
+ this.externalHost = options.externalHost;
this.history = [];
this.cookieJar = new CookieJar;
this.followRedirects = true;
@@ -134,6 +136,10 @@ Browser.prototype.hostBrowser = function(uri) {
if (!browsers) {
browsers = Browser.browsers = {};
}
+ // If we are redirected back, use the same browser
+ if (browsers['undefined'] && browsers['undefined'].externalHost == otherHostname) {
+ otherHostname = 'undefined';
+ }
if (!browsers[this.host]) {
browsers[this.host] = this;
}
View
23 test/browser.navigation.test.js
@@ -42,6 +42,10 @@ app.get('/redirect', function(req, res){
res.redirect('/one');
});
+app.get('/redirect/external', function(req, res){
+ res.redirect('http://bit.ly/mQETJ8');
+});
+
app.get('/xml', function(req, res){
res.contentType('.xml');
res.send('<user><name>tj</name></user>');
@@ -318,6 +322,25 @@ module.exports = {
});
},
+ 'test .request() redirect to a full uri and back again to app': function(done) {
+ Browser.browsers = {}
+ // Use externalHost to pretend to be nodejs.org
+ // The app redirects us out to bit.ly which redirects us back
+ var browser = tobi.createBrowser(app, {externalHost: 'nodejs.org'});
+ browser.followRedirects = false;
+ browser.request('GET', '/redirect/external', {}, function(res, $){
+ res.should.have.status(301);
+ Browser.browsers.should.have.property('bit.ly');
+ Browser.browsers.should.have.property('undefined');
+ browser.request('GET', res.headers['location'], {}, function(res, $){
+ res.should.have.status(200);
+ Browser.browsers.should.not.have.property('nodejs.org');
+ $('p').should.have.text('Hello World');
+ done();
+ });
+ });
+ },
+
'test .request redirecting from a full non-https uri to a https uri': function(done){
var browser = tobi.createBrowser(80, 'bit.ly')
browser.request('GET', 'http://bit.ly/jrs5ME', {}, function (res, $) {
Please sign in to comment.
Something went wrong with that request. Please try again.