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

Commit

Permalink
refactor tests for error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyej authored and typerandom committed Jan 18, 2016
1 parent b318ca1 commit 51f508e
Showing 1 changed file with 85 additions and 40 deletions.
125 changes: 85 additions & 40 deletions test/sp.ds.requestExecutor_test.js
Expand Up @@ -102,51 +102,96 @@ describe('ds:', function () {
reqExec.execute({uri: uri, method:'GET'}, cbSpy);
});

it('should include the original request error in case of request error', function (done) {
var cbSpy;
// This triggers one of the possible http request errors
var uri = 'http://doesntexist/v1/test';

function cb(err, body) {
err.should.be.an.instanceof(Error);
err.should.have.property('inner')
.that.is.an.instanceof(Error)
.and.property('code', 'ENOTFOUND');
expect(body).to.be.null;
cbSpy.should.have.been.calledOnce;
done();
describe('in case of request error', function () {
var uri;
var method;
var error;
var body;

function executeRequest(done) {
var cbSpy = sinon.spy(function () {
error = arguments[0];
body = arguments[1];
done();
});

reqExec.execute({uri: uri, method: method}, cbSpy);
}
cbSpy = sinon.spy(cb);

reqExec.execute({uri: uri, method:'GET'}, cbSpy);
});

it('should include HTTP details in message in case of request error', function (done) {
// This triggers one of the possible http request errors
var uri = 'http://doesntexist/v1/test';

var cbSpy = sinon.spy(function (err, body) {
err.should.be.an.instanceof(Error);

err.should.have.property('stack')
.that.is.not.empty.and
.that.is.not.null.and
.that.is.not.undefined;

err.should.have.property('message')
.that.equal('Unable to execute http request GET http://doesntexist/v1/test: getaddrinfo ENOTFOUND doesntexist');

err.should.have.property('inner')
.that.is.an.instanceof(Error)
.and.property('code', 'ENOTFOUND');

expect(body).to.be.null;
cbSpy.should.have.been.calledOnce;
beforeEach(function () {
// This triggers one of the possible http request errors
uri = 'http://doesntexist/v1/test';
});

done();
describe('when request method is GET', function () {
beforeEach(function (done) {
method = 'GET';
executeRequest(done);
});

describe('body', function () {
it('should be null', function () {
expect(body).to.be.null;
});
});

describe('error', function () {
it('should be an instance of Error', function () {
error.should.be.an.instanceof(Error);
});

describe('stack property', function () {
it('should not be empty/null/undefined', function () {
error.should.have.property('stack')
.that.is.not.empty.and
.that.is.not.null.and
.that.is.not.undefined;
});
});

describe('inner property', function () {
it('should be an instance of Error', function () {
error.should.have.property('inner')
.that.is.an.instanceof(Error);
});

it('should have the "code" property set to "ENOTFOUND"', function () {
error.inner.should.have.property('code', 'ENOTFOUND');
});
});

describe('message property', function () {
it('should include request method', function () {
error.should.have.property('message');
error.message.should.include(method);
});

it('should include request uri', function () {
error.message.should.include(uri);
});

it('should include error.inner.code', function () {
error.message.should.include(error.inner.code);
});
});
});
});

reqExec.execute({uri: uri, method:'GET'}, cbSpy);
describe('when request method is undefined', function () {
beforeEach(function (done) {
method = undefined;
executeRequest(done);
});

describe('error', function () {
describe('message property', function () {
it('should include request method as "GET"', function () {
error.should.have.property('message');
error.message.should.include('GET');
});
});
});
});
});
});
});
Expand Down

0 comments on commit 51f508e

Please sign in to comment.