Commit 2c58c598 authored by analog-nico's avatar analog-nico
Browse files

Fixed no and multiple invocations of the then method

parent 34464dd5
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@ function ownCallback(err, httpResponse, body) {
        } catch (e) { }
    }

    if (self._rp_then_invoked !== true) {
        return;
    }

    if (err) {
        self._rp_reject({
            error: err,
@@ -40,6 +44,7 @@ function ownCallback(err, httpResponse, body) {
            self._rp_resolve(body);
        }
    }

}

var originalInit = request.Request.prototype.init;
@@ -65,8 +70,13 @@ request.Request.prototype.then = function (onFulfilled, onRejected) {

    var self = this;

    if (self._rp_then_invoked === true) {
        throw new Error('Request-Promise currently only allows to call the rp(...).then(...) method once. Please use chaining like rp(...).then(...).then(...) instead.');
    }

    return new Bluebird(function (resolve, reject) {

        self._rp_then_invoked = true;
        self._rp_resolve = resolve;
        self._rp_reject = reject;

+59 −4
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ var Bluebird = require('bluebird');

describe('Request-Promise', function () {

    var server;
    var server, lastResponseBody;

    before(function () {
        // This creates a local server to test for various status codes. A request to /404 returns a 404, etc
@@ -17,7 +17,8 @@ describe('Request-Promise', function () {
            var status = parseInt(path.split('/')[1]);
            if(isNaN(status)) { status = 555; }
            response.writeHead(status);
            response.end(request.method + ' ' + request.url);
            lastResponseBody = request.method + ' ' + request.url;
            response.end(lastResponseBody);
        });
        server.listen(4000);
    });
@@ -360,7 +361,61 @@ describe('Request-Promise', function () {

    describe('defaults', function () {});

    describe('with callback', function () {});
    describe('inspect reject reason objects', function () {});
    describe('should still allow a callback', function () {

        it('without using the then method', function (done) {

            rp('http://localhost:4000/200', function (err, httpResponse, body) {

                try {

                    if (err) { throw err; }
                    expect(body).to.eql('GET /200');
                    done();

                } catch (e) {
                    done(e);
                }

            });

        });

        xit('with using the then method at the same time');
        xit('and still work if the callback throws an exception');

        it('without using the then method and no callback provided', function (done) {

            rp('http://localhost:4000/234'); // 234 is only used here.

            setTimeout(function () {
                if (lastResponseBody !== 'GET /234') {
                    done (new Error('The server did not receive the request!'));
                } else {
                    done();
                }
            }, 20);

        });

    });

    describe('should be Promises/A+ compliant', function () {

        xit('and take only a fulfilled handler');
        xit('and take only a rejected handler');
        xit('and allow calling then after the request already finished');

        xit('by allowing chaining');

        it('but not allow the then method to be invoked more than once', function () {
            var req = rp('http://localhost:4000/200');
            req.then(function () {}, function () {});
            // FIXME: Using the following line instead of the previous one produces an error.
//            req.then(function () {});
            expect(function () { req.then(); }).to.throw('Request-Promise currently only allows to call the rp(...).then(...) method once. Please use chaining like rp(...).then(...).then(...) instead.');
        });

    });

});