chaijs/chai-http — pull request #11 by keithamus (2014-10-02 13:41:32+00:00)
Title: Refactor agent

Hi, I've spent some time work with this library (specifically Chai-HTTP's `chai.request()` feature), and I've made some improvements to the interaction with these:

- Generally speaking, chai.request() is more inline with how [SuperTest](https://github.com/visionmedia/supertest) behaves.
- Dropped `harbor` and `pauli` deps - instead getting node to assign a random port (`.listen(0)`).
- `Test` constructor inherits from `SuperAgent` meaning all methods it has are available.
- The above means that `chai.request()` is compatible with all SuperAgent plugins, e.g. `chai.request(app).use(require('no-cache'))`
- `Test` has Promises support (`.then` and `.catch`) which makes it a joy to work with when using [Chai-As-Promised](https://github.com/domenic/chai-as-promised/blob/master/lib/chai-as-promised.js) or returning back to Mocha.
- Promises support tries to work with the `global.Promise` object (present in Node .11) but can be overriden - by calling `request.addPromises(myCompatiblePromiseLibrary)`. For Node 0.10 and below, [es6-promise](https://github.com/jakearchibald/es6-promise) or [es6-shim](https://github.com/paulmillr/es6-shim/) can be dropped in to your project before requiring chai-http.
- `chai.request.agent` exists, which persists cookies between requests.
- I've also added two new assertions:
- `should.have.param('queryparam', 'queryvalue');` - asserts on req.url.params, and uses [qs](https://github.com/hapijs/qs) so deep params can be asserted on.
- `should.have.cookie('cookiename', 'cookievalue');` asserts on `req.headers.cookie` or `res.headers['set-cookie']` for a much easier way to check cookie values coming back and forth in requests.

These changes make it a breeze to do stuff like this:
```javascript
var request = chai.request.agent(app);
request.post('/login')
.send({ username: 'myuser', password: '1234' })
.then(function (res) {
res.should.have.cookie('session');
return request.put('/user/myuser', { password: '5678' });
})
.then(function (res) {
res.should.have.cookie('session');
userModel.get('myuser').should.eventually.have.property('password', '5678');
});
```

### Breaking Changes

Unfortunately, because `chai.request()` inherits from SuperAgent - it means the `.res()` and `.req()` methods have gone, instead, `.req()` stuff can be done directly on the instance (`.set()`, `.send()` etc) and `.res(function (res) { })` becomes `.end(function (err, res) {});` (or use Promises).

If you'd like I can spend some time adding `.req()` and `.res()` back in, to make sure this is completely 100% compatible - but they feel kind of redundant.

Also, if you'd like this to be broken into individual PRs I am happy to do that as well.

Thanks for your hard work @logicalparadox with this library - it's testing HTTP apps much nicer!