Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
Merge pull request #6 from kumar303/timeout
Browse files Browse the repository at this point in the history
Handle XHR timeouts in API (bug 981647)
  • Loading branch information
kumar303 committed Mar 11, 2014
2 parents fc6ca73 + abb44b9 commit 73aa086
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
25 changes: 17 additions & 8 deletions lib/fxpay.js
Expand Up @@ -13,13 +13,16 @@
opt.mozPay = opt.mozPay || navigator.mozPay;
opt.maxTries = opt.maxTries || undefined;
opt.pollIntervalMs = opt.pollIntervalMs || undefined;
opt.apiTimeoutMs = opt.apiTimeoutMs || undefined;
opt.apiUrlBase = (opt.apiUrlBase ||
'https://marketplace.firefox.com');
opt.apiVersionPrefix = (opt.apiVersionPrefix || '/api/v1');

var log = opt.log;
var api = new API(opt.apiUrlBase, {log: log,
versionPrefix: opt.apiVersionPrefix});
var api = new API(opt.apiUrlBase,
{log: log,
timeoutMs: opt.apiTimeoutMs,
versionPrefix: opt.apiVersionPrefix});

log.debug('starting purchase for product', productId);

Expand Down Expand Up @@ -94,7 +97,8 @@
// The transaction is complete.
return cb(null, data);
} else if (data.state === 'PENDING') {
log.debug('Re-trying pending transaction');
log.debug('Re-trying pending transaction in',
opt.pollIntervalMs, 'ms');
window.setTimeout(function() {
getTransactionResult(api, transStatusPath, cb, {
log: log,
Expand All @@ -114,12 +118,10 @@

function API(baseUrl, opt) {
opt = opt || {};
opt.log = opt.log || window.console;
opt.versionPrefix = opt.versionPrefix || undefined;

this.log = opt.log;
this.baseUrl = baseUrl;
this.versionPrefix = opt.versionPrefix;
this.log = opt.log || window.console;
this.timeoutMs = opt.timeoutMs || 5000;
this.versionPrefix = opt.versionPrefix || undefined;
}

exports.API = API;
Expand All @@ -138,6 +140,7 @@

API.prototype.post = function(path, data, cb) {
var log = this.log;
var api = this;
var url;
if (/^http(s):\/\/.*/.test(path)) {
// Requesting an absolute URL so no need to prefix it.
Expand Down Expand Up @@ -179,6 +182,11 @@
}

cb(null, data);
},
timeout: function() {
log.error('xhr request to', url, 'timed out after',
api.timeoutMs, 'ms');
cb('API_REQUEST_TIMEOUT');
}
};

Expand All @@ -187,6 +195,7 @@
}

log.debug('opening POST to', url);
xhr.timeout = api.timeoutMs;
xhr.open("POST", url, true);
xhr.send(data);
};
Expand Down
18 changes: 17 additions & 1 deletion tests/test-fxpay.js
Expand Up @@ -196,7 +196,7 @@ describe('fxpay', function () {
it('should report XHR abort', function (done) {
server.respondWith(function(xhr, id) {
// We use a custom event because xhr.abort() triggers load first
// (probably a sinon bug).
// https://github.com/cjohansen/Sinon.JS/issues/432
dispatchXhrEvent(xhr, 'abort');
});

Expand Down Expand Up @@ -293,6 +293,22 @@ describe('fxpay', function () {
server.respond();
});

it('should timeout', function (done) {
server.respondWith(function(xhr, id) {
// We simulate a timeout event here because Sinon
// doesn't seem to support the XHR.timeout property.
// https://github.com/cjohansen/Sinon.JS/issues/431
dispatchXhrEvent(xhr, 'timeout');
});

api.post('/timeout', null, function(err) {
assert.equal(err, 'API_REQUEST_TIMEOUT');
done();
});

server.respond();
});

it('should allow you to get unversioned URLs', function (done) {
assert.equal(api.url('/not/versioned', {versioned: false}),
baseUrl + '/not/versioned');
Expand Down

0 comments on commit 73aa086

Please sign in to comment.