Unverified Commit 6b9fd011 authored by not-an-aardvark's avatar not-an-aardvark
Browse files

Add cancellation support (fixes #113)

parent 27945f3d
Loading
Loading
Loading
Loading
+10 −3
Original line number Original line Diff line number Diff line
'use strict';
'use strict';


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


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


Bluebird.config({cancellation: true});


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



request.bindCLS = function RP$bindCLS() {
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');
    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');
+3 −0
Original line number Original line Diff line number Diff line
@@ -19,6 +19,9 @@ module.exports = function startServer(port, cb) {
                    res.writeHead(301, { Location: '/200' });
                    res.writeHead(301, { Location: '/200' });
                    res.end();
                    res.end();
                    break;
                    break;
                case 503:
                    // Send no response at all
                    break;
                default:
                default:
                    res.writeHead(status, {'Content-Type': 'text/plain'});
                    res.writeHead(status, {'Content-Type': 'text/plain'});
                    var body = req.method === 'POST' ? ' - ' + JSON.stringify(req.body) : '';
                    var body = req.method === 'POST' ? ' - ' + JSON.stringify(req.body) : '';
+19 −3
Original line number Original line Diff line number Diff line
'use strict';
'use strict';


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


            var p = rp('http://localhost:4000/200').promise();
            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 () {
    describe('should still allow to require Request independently', function () {