Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cancellation support #123

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/rp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var Bluebird = require('bluebird'),
var Bluebird = require('bluebird').getNewLibraryCopy(),
configure = require('@request/promise-core/configure/request2'),
stealthyRequire = require('stealthy-require');

Expand All @@ -20,6 +20,7 @@ try {
throw err;
}

Bluebird.config({cancellation: true});

configure({
request: request,
Expand All @@ -28,11 +29,17 @@ configure({
'then',
'catch',
'finally',
'cancel',
'promise'
]
],
constructorMixin: function (resolve, reject, onCancel) {
var self = this;
onCancel(function () {
self.abort();
});
}
});


request.bindCLS = function RP$bindCLS() {
throw new Error('CLS support was dropped. To get it back read: https://github.com/request/request-promise/wiki/Getting-Back-Support-for-Continuation-Local-Storage');
};
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module.exports = function startServer(port, cb) {
res.writeHead(301, { Location: '/200' });
res.end();
break;
case 503:
// Send no response at all
break;
default:
res.writeHead(status, {'Content-Type': 'text/plain'});
var body = req.method === 'POST' ? ' - ' + JSON.stringify(req.body) : '';
Expand Down
22 changes: 19 additions & 3 deletions test/spec/request-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var Bluebird = require('bluebird'),
childProcess = require('child_process'),
var childProcess = require('child_process'),
errors = require('../../errors'),
path = require('path'),
rp = require('../../'),
Expand Down Expand Up @@ -78,10 +77,27 @@ describe('Request-Promise', function () {

var p = rp('http://localhost:4000/200').promise();

expect(p instanceof Bluebird).to.eql(true);
// This will not actually be an instanceof Bluebird since request-promise creates a new Bluebird copy.
// Instead, verify that the Promise contains the bluebird functions.
expect(p.constructor.name).to.equal('Promise');
expect(p.then).to.be.a('function');
expect(p.delay).to.be.a('function');
expect(p.map).to.be.a('function');
expect(p.cancel).to.be.a('function');

});

it('.cancel() cancelling the Bluebird promise and aborting the request', function (done) {
var req = rp('http://localhost:4000/503');
req.once('abort', done);
req.cancel();
});

it('.cancel() on promises chained from the Bluebird promise, aborting the request', function (done) {
var req = rp('http://localhost:4000/503');
req.once('abort', done);
req.then(function noop() { }).cancel();
});
});

describe('should still allow to require Request independently', function () {
Expand Down