diff --git a/client/karma.js b/client/karma.js index 935ad4493..dbf928c59 100644 --- a/client/karma.js +++ b/client/karma.js @@ -9,7 +9,9 @@ var Karma = function(socket, context, navigator, location) { var startEmitted = false; var store = {}; var self = this; - var browserId = (location.search.match(/\?id=(.*)/) || [])[1] || util.generateId('manual-'); + var queryParams = util.parseQueryParams(location.search); + var browserId = queryParams.id || util.generateId('manual-'); + var returnUrl = queryParams.return_url || null; var resultsBufferLimit = 1; var resultsBuffer = []; @@ -127,7 +129,12 @@ var Karma = function(socket, context, navigator, location) { // tests could run in the same event loop, we wouldn't notice. setTimeout(function() { socket.emit('complete', result || {}); - clearContext(); + if (returnUrl) { + socket.disconnect(); + location.href = returnUrl; + } else { + clearContext(); + } }, 0); }; diff --git a/client/util.js b/client/util.js index 63c4dfc66..e723ee1a4 100644 --- a/client/util.js +++ b/client/util.js @@ -17,3 +17,16 @@ exports.isUndefined = function(value) { exports.isDefined = function(value) { return !exports.isUndefined(value); }; + +exports.parseQueryParams = function(locationSearch) { + var params = {}; + var pairs = locationSearch.substr(1).split('&'); + var keyValue; + + for (var i = 0; i < pairs.length; i++) { + keyValue = pairs[i].split('='); + params[decodeURIComponent(keyValue[0])] = decodeURIComponent(keyValue[1]); + } + + return params; +}; diff --git a/test/client/karma.spec.js b/test/client/karma.spec.js index 5c3795275..d7e14e2d5 100644 --- a/test/client/karma.spec.js +++ b/test/client/karma.spec.js @@ -227,5 +227,18 @@ describe('Karma', function() { k.complete(); expect(spyResult).toHaveBeenCalled(); }); + + + it('should disconnect navigate the client to return_url if specified', function() { + windowLocation.search = '?id=567&return_url=http://return.com'; + socket = new MockSocket(); + k = new Karma(socket, {}, windowNavigator, windowLocation); + + spyOn(socket, 'disconnect'); + + k.complete(); + expect(socket.disconnect).toHaveBeenCalled(); + expect(windowLocation.href).toBe('http://return.com'); + }); }); }); diff --git a/test/client/mocks.js b/test/client/mocks.js index d3630de64..67a746d1d 100644 --- a/test/client/mocks.js +++ b/test/client/mocks.js @@ -28,6 +28,10 @@ var MockSocket = function() { this.socket = {transport: {name: 'websocket'}}; + this.disconnect = function() { + this.emit('disconnect'); + }; + // MOCK API this._setTransportNameTo = function(transportName) { this.socket.transport.name = transportName; diff --git a/test/client/util.spec.js b/test/client/util.spec.js new file mode 100644 index 000000000..8c7c01d58 --- /dev/null +++ b/test/client/util.spec.js @@ -0,0 +1,11 @@ +var util = require('../../client/util'); + + +describe('util', function() { + + describe('parseQueryParams', function() { + var params = util.parseQueryParams('?id=123&return_url=http://whatever.com'); + + expect(params).toEqual({id: '123', return_url: 'http://whatever.com'}); + }); +});