Skip to content

Commit

Permalink
Merge f7717ea into 3fd809c
Browse files Browse the repository at this point in the history
  • Loading branch information
farmisen committed May 15, 2016
2 parents 3fd809c + f7717ea commit f919691
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 87 deletions.
41 changes: 3 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ json, or even file attachments added to it, all with a simple API:
chai.request(app)
.put('/user/me')
.set('X-API-Key', 'foobar')
.send({ password: '123', confirmPassword: '123' })
.send({ passsword: '123', confirmPassword: '123' })
```

```js
Expand Down Expand Up @@ -120,48 +120,13 @@ To make the request and assert on its response, the `end` method can be used:
```js
chai.request(app)
.put('/user/me')
.send({ password: '123', confirmPassword: '123' })
.send({ passsword: '123', confirmPassword: '123' })
.end(function (err, res) {
expect(err).to.be.null;
expect(res).to.have.status(200);
});
```

##### Caveat

Because the `end` function is passed a callback, assertions are run
asynchronously. Therefore, a mechanism must be used to notify the testing
framework that the callback has completed. Otherwise, the test will pass before
the assertions are checked.

For example, in the [Mocha test framework](http://mochajs.org/), this is
accomplished using the
[`done` callback](https://mochajs.org/#asynchronous-code), which signal that the
callback has completed, and the assertions can be verified:

```js
it('fails, as expected', function(done) { // <= Pass in done callback
chai.request('http://localhost:8080')
.get('/')
.end(function(err, res) {
expect(res).to.have.status(123);
done(); // <= Call done to signal callback end
});
}) ;

it('succeeds silently!', function() { // <= No done callback
chai.request('http://localhost:8080')
.get('/')
.end(function(err, res) {
expect(res).to.have.status(123); // <= Test completes before this runs
});
}) ;
```

When `done` is passed in, Mocha will wait until the call to `done()`, or until
the [timeout](http://mochajs.org/#timeouts) expires. `done` also accepts an
error parameter when signaling completion.

#### Dealing with the response - Promises

If `Promise` is available, `request()` becomes a Promise capable library -
Expand All @@ -170,7 +135,7 @@ and chaining of `then`s becomes possible:
```js
chai.request(app)
.put('/user/me')
.send({ password: '123', confirmPassword: '123' })
.send({ passsword: '123', confirmPassword: '123' })
.then(function (res) {
expect(res).to.have.status(200);
})
Expand Down
16 changes: 10 additions & 6 deletions dist/chai-http.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions dist/chai-http.js.map

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions dist/chai-http.min.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions dist/chai-http.min.js.map

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ var http = require('http')
*
*/

module.exports = function (app) {
module.exports = function (app, options) {

/*!
* @param {Mixed} function or server
Expand All @@ -180,7 +180,7 @@ module.exports = function (app) {

methods.forEach(function (method) {
obj[method] = function (path) {
return new Test(server, method, path);
return new Test(server, method, path, options);
};
});
obj.del = obj.delete;
Expand Down Expand Up @@ -208,9 +208,10 @@ module.exports.addPromises = function (Promise) {
* @api private
*/

function Test (app, method, path) {
function Test (app, method, path, options) {
Request.call(this, method, path);
this.app = app;
this.options = options || {badStatusCausesError: true};
this.url = typeof app === 'string' ? app + path : serverAddress(app, path);
}
util.inherits(Test, Request);
Expand Down Expand Up @@ -249,7 +250,7 @@ Test.prototype.then = function (onResolve, onReject) {
var self = this;
this._promise = new Test.Promise(function (resolve, reject) {
self.end(function (err, res) {
if (err) {
if (err && self.options.badStatusCausesError) {
reject(err);
} else {
resolve(res);
Expand Down
48 changes: 30 additions & 18 deletions test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,36 @@ describe('request', function () {
});
});

it('can be augmented with promises', function (done) {
request('https://httpbin.org')
.get('/get')
.set('X-API-Key', 'test3')
.then(function (res) {
res.should.have.status(200);
res.body.headers['X-Api-Key'].should.equal('test3');
throw new Error('Testing catch');
})
.then(function () {
throw new Error('This should not have fired');
})
.catch(function (err) {
if (err.message !== 'Testing catch') {
throw err;
}
})
.then(done, done);
describe('Promises', function () {
it('can be augmented with promises', function (done) {
request('https://httpbin.org')
.get('/get')
.set('X-API-Key', 'test3')
.then(function (res) {
res.should.have.status(200);
res.body.headers['X-Api-Key'].should.equal('test3');
throw new Error('Testing catch');
})
.then(function () {
throw new Error('This should not have fired');
})
.catch(function (err) {
if (err.message !== 'Testing catch') {
throw err;
}
})
.then(done, done);
});

it('can test for HTTP failure responses', function (done) {
request('https://httpbin.org', {badStatusCausesError: false})
.get('/status/400')
.set('X-API-Key', 'test4')
.then(function (res) {
res.should.have.status(400);
})
.then(done, done);
});
});
});

Expand Down

0 comments on commit f919691

Please sign in to comment.