Commit d8bc8977 authored by analog-nico's avatar analog-nico
Browse files

Updated tests

parent f8a01123
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ var chalk = require('chalk');
var rimraf = require('rimraf');

var chai = require("chai");
chai.use(require("chai-as-promised"));
global.expect = chai.expect;


+1 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
  },
  "devDependencies": {
    "chai": "^1.9.2",
    "chai-as-promised": "^4.1.1",
    "chalk": "^0.5.1",
    "gulp": "^3.8.8",
    "gulp-istanbul": "^0.3.1",
+118 −140
Original line number Diff line number Diff line
@@ -3,9 +3,11 @@
var rp = require('../lib/rp.js');
var http = require('http');
var url = require('url');
var util = require('util');
var Bluebird = require('bluebird');


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

    var server;

    before(function () {
@@ -24,43 +26,66 @@ describe('Request-Promise', function () {
        server.close();
    });

    it('should resolve for 200 status code', function (done) {
        rp('http://localhost:4000/200')
            .then(function(){
                done();
            }).catch(function(){
                done(new Error('A 200 response should resolve, not reject'));

    describe('should reject HTTP errors', function () {

        it('like an unreachable host', function () {
            return expect(rp('http://localhost:1/200')).to.be.rejected;
        });

    });

    it('should resolve for 201 status code', function (done) {
        rp('http://localhost:4000/201')
            .then(function(){
                done();
            }).catch(function(){
                done(new Error('A 201 response should resolve, not reject'));
    describe('should handle status codes', function () {

        it('by resolving a 200', function () {
            return expect(rp('http://localhost:4000/200')).to.be.fulfilled;
        });

        it('by resolving a 201', function () {
            return expect(rp('http://localhost:4000/201')).to.be.fulfilled;
        });

    it('should resolve for 204 status code', function (done) {
        rp('http://localhost:4000/204')
            .then(function(){
                done();
            }).catch(function(){
                done(new Error('A 204 response should resolve, not reject'));
        it('by resolving a 204', function () {
            return expect(rp('http://localhost:4000/204')).to.be.fulfilled;
        });

        describe('with options.simple = true', function(){

            it('by rejecting a 404', function () {
                return expect(rp('http://localhost:4000/404')).to.be.rejected;
            });

    it('should reject for http errors', function(done){
        rp('http://localhost:1/200')
            .then(function(){
                done(new Error('A failed request should reject, not resolve'));
            }).catch(function(){
                done();
            it('by rejecting a 500', function () {
                return expect(rp('http://localhost:4000/500')).to.be.rejected;
            });

        });

    it('should accept the method in wrong case', function (done) {
        describe('with options.simple = false', function(){

            it('by resolving a 404', function (){
                var options = {
                    url: 'http://localhost:4000/404',
                    simple: false
                };
                return expect(rp(options)).to.be.fulfilled;
            });

            it('by resolving a 500', function (){
                var options = {
                    url: 'http://localhost:4000/500',
                    simple: false
                };
                return expect(rp(options)).to.be.fulfilled;
            });

        });

    });

    describe('should process the options', function () {

        it('by correcting options.method with wrong case', function (done) {

            rp({ uri: 'http://localhost:4000/500', method: 'Get' })
                .then(function () {
@@ -74,139 +99,92 @@ describe('Request-Promise', function () {

        });

    describe('simple tests', function(){
        xit('falling back to the default for a non-boolean options.simple');
        xit('falling back to the default for a non-boolean options.resolveWithFullResponse');

        it('should reject for 404 status code', function (done) {
            rp('http://localhost:4000/404')
                .then(function () {
                    done(new Error('A 404 response code should reject, not resolve'));
                }).catch(function(){
                    done();
                });
        });
        it('by not cross-polluting the options of later requests', function () {

        it('should reject for 500 status code', function (done) {
            rp('http://localhost:4000/500')
            return Bluebird.resolve()
                .then(function () {
                    done(new Error('A 500 response code should reject, not resolve'));
                }).catch(function(){
                    done();
                });
        });

    });

    describe('simple tests with changing options object', function(){


        it('should reject for 500 status code', function (done) {
                    var options = {
                        uri : 'http://localhost:4000/500', // UR - I -
                        method : 'GET',
                        simple : true
                    };

            rp('http://localhost:4000/500')
                    return expect(rp(options), 'First request').to.be.rejected;

                })
                .then(function () {
                    done(new Error('A 500 response code should reject, not resolve'));
                }).catch(function(){
                    done();
                });
        });

        it('should resolve for 200 status code', function (done) {
                    var options = {
                        url : 'http://localhost:4000/200', // UR - L -
                        method : 'GET',
                        simple : true
                    };

            rp(options)
                .then(function(){
                    done();
                }).catch(function(){
                    done(new Error('A 200 response should resolve, not reject'));
                });
                    return expect(rp(options), 'Second request').to.be.fulfilled;

                });

        });

    describe('non-simple tests', function(){
        xit('by not cross-polluting the options of parallel requests');

        it('should resolve for 404 status code', function(done){
            var options = {
                url: 'http://localhost:4000/404',
                simple: false
            };
            rp(options)
                .then(function(){
                    done();
                }).catch(function(){
                    done(new Error('A 404 response code should resolve, not reject'));
                });
        });
        it('resolveWithFullResponse = true', function () {

        it('should resolve for 500 status code', function(done){
            var options = {
                url: 'http://localhost:4000/500',
                simple: false
                url: 'http://localhost:4000/200',
                method: 'GET',
                resolveWithFullResponse: true
            };
            rp(options)
                .then(function(){
                    done();
                }).catch(function(){
                    done(new Error('A 500 response code should resolve, not reject'));
                });

            return rp(options)
                .then(function(response){
                    expect(response.statusCode).to.eql(200);
                    expect(response.request.method).to.eql('GET');
                    expect(response.body).to.eql('GET /200');
                });

        });

        xit('including a transform function');
        xit('by ignoring options.transform if it is not a function');

    describe('HTTP methods', function(){
        it('should support PATCH', function(done){
            var options = {
                url: 'http://localhost:4000/200',
                method: 'PATCH'
            };
            rp(options)
                .then(function(){
                    done();
                }).catch(function(){
                    done(new Error('A 200 response code for a PATCH request should resolve, not reject'));
    });

    describe('should cover the HTTP method shortcuts', function () {

        it('rp.get', function () {
            return expect(rp.get('http://localhost:4000/200')).to.eventually.eql('GET /200');
        });

        it('should support DELETE', function(done){
            var options = {
                url: 'http://localhost:4000/200',
                method: 'DELETE'
            };
            rp(options)
                .then(function(){
                    done();
                }).catch(function(){
                    done(new Error('A 200 response code for a DELETE request should resolve, not reject'));
        xit('rp.head', function () {
            return expect(rp.head('http://localhost:4000/200')).to.eventually.eql('HEAD /200');
        });

        it('rp.post', function () {
            return expect(rp.post('http://localhost:4000/200')).to.eventually.eql('POST /200');
        });

        it('rp.put', function () {
            return expect(rp.put('http://localhost:4000/200')).to.eventually.eql('PUT /200');
        });

    describe('with option resolveWithFullResponse', function () {
        it('rp.patch', function () {
            return expect(rp.patch('http://localhost:4000/200')).to.eventually.eql('PATCH /200');
        });

        it('should include the response', function (done) {
            var options = {
                url: 'http://localhost:4000/200',
                method: 'GET',
                resolveWithFullResponse: true
            };
            rp(options)
                .then(function(response){
                    expect(response.statusCode).to.eql(200);
                    expect(response.request.method).to.eql('GET');
                    expect(response.body).to.eql('GET /200');
                    done();
                }).catch(done);
        it('rp.del', function () {
            return expect(rp.del('http://localhost:4000/200')).to.eventually.eql('DELETE /200');
        });

    });

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

    describe('with callback', function () {});
    describe('inspect reject reason objects', function () {});

});