Skip to content

Commit

Permalink
Merge pull request #503 from bbc/IPLAYERTVV1-6515-crossDomainGetTimeout
Browse files Browse the repository at this point in the history
IPLAYERTVV1-6515 Add timeout to crossDomainGet CORS
  • Loading branch information
rthompson1991 authored Apr 12, 2018
2 parents 37f70da + 91fa231 commit 9df0279
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions static/script-tests/tests/devices/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ require(
expect(mockXMLHttpRequest.send).toHaveBeenCalledWith('<submit>Data</submit>');
});

it('sends xhr request with specified timeout', function() {
var opts = {
timeout: 1000
};

device = new Device({'networking': { 'supportsCORS': true }});
spyOn(device, '_newXMLHttpRequest').and.returnValue(mockXMLHttpRequest);

device.executeCrossDomainGet('http://test.uri/', opts);
expect(mockXMLHttpRequest.timeout).toEqual(1000);
});

it('uses GET if there is no opts.method', function() {
spyOn(XMLHttpRequest.prototype, 'open');
spyOn(XMLHttpRequest.prototype, 'send');
Expand Down
7 changes: 7 additions & 0 deletions static/script/devices/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,9 @@ define(
*/
loadURL: function loadURL (url, opts) {
var xhr = this._newXMLHttpRequest();
if (opts.timeout) {
xhr.timeout = opts.timeout;
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
xhr.onreadystatechange = null;
Expand Down Expand Up @@ -834,6 +837,10 @@ define(
};
}

if (opts.timeout) {
modifiedOpts.timeout = opts.timeout;
}

this.loadURL(url, modifiedOpts);
} else {
callbackKey = jsonpOptions.callbackKey || 'callback';
Expand Down

0 comments on commit 9df0279

Please sign in to comment.