Use this plugin as you would all other Chai plugins.
import * as chai from "chai";import chaiHttp from "chai-http";chai.use(chaiHttp);// if you need to access `request`import {default as chaiHttp, request} from "chai-http";chai.use(chaiHttp);request.get(...).send(...);// or setting up an apprequest.execute(app);
To use Chai HTTP on a web page, please use the latest v4 version for now.
Integration Testing
Chai HTTP provides an 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
allows you to specify the http VERB request (get, post, etc)
that you wish to invoke.
Application / Server
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.
Note: This feature is only supported on Node.js, not in web browsers.
import {request} from 'chai-http';request.execute(app) .get('/')
When passing an app to request.execute(), it will automatically open the server for
incoming requests (by calling listen()) and, once a request has been made
the server will automatically shut down (by calling .close()). If you want to
keep the server open, perhaps if you're making multiple requests, you must call
.keepOpen() after .request(), and manually close the server down:
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, this is
accomplished using the
done callback, which signal that the
callback has completed, and the assertions can be verified:
import {request} from 'chai-http';it('fails, as expected', function(done) { // <= Pass in done callback request.execute('http://localhost:8080') .get('/') .end((err, res) => { expect(res).to.have.status(123); done(); // <= Call done to signal callback end });});it('succeeds silently!', () => { // <= No done callback request.execute('http://localhost:8080') .get('/')
When done is passed in, Mocha will wait until the call to done(), or until
the timeout 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 -
and chaining of thens becomes possible:
Sometimes you need to keep cookies from one request, and send them with the
next (for example, when you want to login with the first request, then access an authenticated-only resource later). For this, .request.agent() is available:
import {request} from 'chai-http';// Log inconst agent = request.agent(app)agent .post('/session') .send({ username: 'me', password: '123' }) .then((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((res) => { expect(res).to.have.status(200);
Note: The server started by request.agent(app) will not automatically close following the test(s). You should call agent.close() after your tests to ensure your program exits.
Assertions
The Chai HTTP module provides a number of assertions
for the expect and should interfaces.
.status (code)
@param{Number} status number
Assert that a response has a supplied status.
expect(res).to.have.status(200);
.header (key[, value])
@param{String} header key (case insensitive)
@param{String|RegExp} header value (optional)
Assert that a Response or Request object has a header.
If a value is provided, equality to value will be asserted.
You may also pass a regular expression to check.
Note: When running in a web browser, the
same-origin policy
only allows Chai HTTP to read
certain headers,
which can cause assertions to fail.
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.
.end((err, res) => {
expect(res).to.have.status(123); // <= Test completes before this runs