chai-http

Extend Chai Assertion library with tests for http apis

Chai HTTP

HTTP integration testing with Chai assertions.

  • integration test request composition
  • test http apps or external services
  • assertions for common http tasks
  • chai expect and should interfaces

This is a addon plugin for the Chai Assertion Library. Install via npm.

npm install chai-http

Use this plugin as you would all other Chai plugins.

var chai = require('chai')
  , chaiHttp = require('chai-http');
 
chai.use(chaiHttp);

Chai HTTP provides and interface for live integration testing via superagent. To do this, you must first construct a request to an application or url.

Upon construction you are provided a chainable api that allow to you specify the http VERB request (get, post, etc) that you wish to invoke.

You may use a function (such as an express or connect app) or a node.js http(s) server as the foundation for your request. If the server is not running, chai-http will find a suitable port to listen on for a given test.

chai.request(app)
  .get('/')

You may also use a base url as the foundation of your request.

chai.request('http://localhost:8080')
  .get('/')

Once a request is created with a given VERB, it can have headers, form data, json, or even file attachments added to it, all with a simple API:

// Send some JSON 
chai.request(app)
  .put('/user/me')
  .set('X-API-Key', 'foobar')
  .send({ passsword: '123', confirmPassword: '123' })
// Send some Form Data 
chai.request(app)
  .post('/user/me')
  .field('_method', 'put')
  .field('password', '123')
  .field('confirmPassword', '123')
// Attach a file 
chai.request(app)
  .post('/user/avatar')
  .attach(fs.readFileSync('avatar.png'), 'avatar.png')
// Authenticate with Basic authentication 
chai.request(app)
  .auth('user', 'pass')
  .get('/protected')
// Chain some GET query parameters 
chai.request(app)
  .get('/search')
  .query('name', 'foo')
  .query('limit', '10') // /search?name=foo&limit=10 

To make the request and assert on its response, the end method can be used:

chai.request(app)
  .put('/user/me')
  .send({ passsword: '123', confirmPassword: '123' })
  .end(function (errres) {
     expect(err).to.be.null;
     expect(res).to.have.status(200);
  });

If Promise is available, request() becomes a Promise capable library - and chaining of thens becomes possible:

chai.request(app)
  .put('/user/me')
  .send({ passsword: '123', confirmPassword: '123' })
  .then(function (res) {
     expect(res).to.have.status(200);
  })
  .catch(function (err) {
     throw err;
  })

Sometimes you need to keep cookies from one request, and send them with the next. For this, .request.agent() is available:

// Log in 
var agent = chai.request.agent(app)
agent
  .post('/session')
  .send({ username: 'me', password: '123' })
  .then(function (res) {
    expect(res).to.have.cookie('sessionid');
    // The `agent` now has the sessionid cookie saved, and will send it 
    // back to the server in the next request: 
    return agent.get('/user/me')
      .then(function (res) {
         expect(res).to.have.status(200);
      })
  })
  • @param {Function} resolveCB
  • @cb {Response}
  • @param {Function} rejectCB
  • @cb {Error}

Invoke the request to to the server. The response will be passed as a parameter to the resolveCb, while any errors will be passed to rejectCb.

chai.request(app)
  .get('/')
  .then(function (res) {
    expect(res).to.have.status(200);
  }, function (err) {
     throw err;
  });
  • @param {Function} rejectCB
  • @cb {Error}

Invoke the request to to the server, catching any errors with this callback. Behaves the same as Promises.

chai.request(app)
  .get('/')
  .catch(function (err) {
    throw err;
  });

The Chai HTTP module provides a number of assertions for the expect and should interfaces.

  • @param {Number} status number

Assert that a response has a supplied status.

expect(res).to.have.status(200);
  • @param {String} header key
  • @param {String} header value (optional)

Assert that an object has a header. If a value is provided, equality to value will be asserted.

expect(req).to.have.header('x-api-key');
expect(req).to.have.header('content-type', 'text/plain');

Assert that an object has headers.

expect(req).to.have.headers;

Assert that a string represents valid ip address.

expect('127.0.0.1').to.be.an.ip;
expect('2001:0db8:85a3:0000:0000:8a2e:0370:7334').to.be.an.ip;

Assert that a Response or Request object has a given content-type.

expect(req).to.be.json;
expect(req).to.be.html;
expect(req).to.be.text;

Assert that a Response object has a redirect status code.

expect(res).to.redirect;
  • @param {String} location url

Assert that a Response object has a redirects to the supplied location.

expect(res).to.redirectTo('http://example.com');
  • @param {String} parameter name
  • @param {String} parameter value

Assert that a Request object has a query string parameter with a given key, (optionally) equal to value

expect(req).to.have.param('orderby');
expect(req).to.have.param('orderby', 'date');
expect(req).to.not.have.param('limit');
  • @param {String} parameter name
  • @param {String} parameter value

Assert that a Request or Response object has a cookie header with a given key, (optionally) equal to value

expect(req).to.have.cookie('session_id');
expect(req).to.have.cookie('session_id', '1234');
expect(req).to.not.have.cookie('PHPSESSID');
expect(res).to.have.cookie('session_id');
expect(res).to.have.cookie('session_id', '1234');
expect(res).to.not.have.cookie('PHPSESSID');

(The MIT License)

Copyright (c) Jake Luer jake@alogicalparadox.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.