@@ -20,29 +20,13 @@ This is an addon plugin for the [Chai Assertion Library](https://www.chaijs.com/
2020Use this plugin as you would all other Chai plugins.
2121
2222``` js
23- const chai = require (' chai' );
24- const chaiHttp = require (' chai-http' );
25-
26- chai .use (chaiHttp);
27- ```
28-
29- To use Chai HTTP in an ECMAScript module (ESM)
30-
31- ``` js
23+ import chaiModule from " chai" ;
3224import chaiHttp from " chai-http" ;
33- import * as chaiModule from ' chai ' ;
25+
3426const chai = chaiModule .use (chaiHttp);
3527```
3628
37- To use Chai HTTP on a web page, just include the [ ` dist/chai-http.js ` ] ( dist/chai-http.js ) file:
38-
39- ``` html
40- <script src =" chai.js" ></script >
41- <script src =" chai-http.js" ></script >
42- <script >
43- chai .use (chaiHttp);
44- </script >
45- ```
29+ To use Chai HTTP on a web page, please use the latest v4 version for now.
4630
4731## Integration Testing
4832
@@ -65,7 +49,7 @@ port to listen on for a given test.
6549__ Note:__ This feature is only supported on Node.js, not in web browsers.
6650
6751``` js
68- chai .request (app)
52+ chai .request . execute (app)
6953 .get (' /' )
7054```
7155
@@ -76,7 +60,7 @@ keep the server open, perhaps if you're making multiple requests, you must call
7660` .keepOpen() ` after ` .request() ` , and manually close the server down:
7761
7862``` js
79- const requester = chai .request (app).keepOpen ()
63+ const requester = chai .request . Request (app).keepOpen ()
8064
8165Promise .all ([
8266 requester .get (' /a' ),
@@ -92,7 +76,7 @@ Promise.all([
9276You may also use a base url as the foundation of your request.
9377
9478``` js
95- chai .request (' http://localhost:8080' )
79+ chai .request . execute (' http://localhost:8080' )
9680 .get (' /' )
9781```
9882
@@ -114,7 +98,7 @@ Examples:
11498` .set() `
11599``` js
116100// Set a request header
117- chai .request (app)
101+ chai .request . execute (app)
118102 .put (' /user/me' )
119103 .set (' Content-Type' , ' application/json' )
120104 .send ({ password: ' 123' , confirmPassword: ' 123' })
@@ -123,15 +107,15 @@ chai.request(app)
123107` .send() `
124108``` js
125109// Send some JSON
126- chai .request (app)
110+ chai .request . execute (app)
127111 .put (' /user/me' )
128112 .send ({ password: ' 123' , confirmPassword: ' 123' })
129113```
130114
131115` .type() `
132116``` js
133117// Send some Form Data
134- chai .request (app)
118+ chai .request . execute (app)
135119 .post (' /user/me' )
136120 .type (' form' )
137121 .send ({
@@ -144,20 +128,20 @@ chai.request(app)
144128` .attach() `
145129``` js
146130// Attach a file
147- chai .request (app)
131+ chai .request . execute (app)
148132 .post (' /user/avatar' )
149133 .attach (' imageField' , fs .readFileSync (' avatar.png' ), ' avatar.png' )
150134```
151135
152136` .auth() `
153137``` js
154138// Authenticate with Basic authentication
155- chai .request (app)
139+ chai .request . execute (app)
156140 .get (' /protected' )
157141 .auth (' user' , ' pass' )
158142
159143// Authenticate with Bearer Token
160- chai .request (app)
144+ chai .request . execute (app)
161145 .get (' /protected' )
162146 .auth (accessToken, { type: ' bearer' })
163147
@@ -166,7 +150,7 @@ chai.request(app)
166150` .query() `
167151``` js
168152// Chain some GET query parameters
169- chai .request (app)
153+ chai .request . execute (app)
170154 .get (' /search' )
171155 .query ({name: ' foo' , limit: 10 }) // /search?name=foo&limit=10
172156```
@@ -182,7 +166,7 @@ const { expect } = chai;
182166To make the request and assert on its response, the ` end ` method can be used:
183167
184168``` js
185- chai .request (app)
169+ chai .request . execute (app)
186170 .put (' /user/me' )
187171 .send ({ password: ' 123' , confirmPassword: ' 123' })
188172 .end ((err , res ) => {
@@ -205,7 +189,7 @@ callback has completed, and the assertions can be verified:
205189
206190``` js
207191it (' fails, as expected' , function (done ) { // <= Pass in done callback
208- chai .request (' http://localhost:8080' )
192+ chai .request . execute (' http://localhost:8080' )
209193 .get (' /' )
210194 .end ((err , res ) => {
211195 expect (res).to .have .status (123 );
@@ -214,7 +198,7 @@ it('fails, as expected', function(done) { // <= Pass in done callback
214198});
215199
216200it (' succeeds silently!' , () => { // <= No done callback
217- chai .request (' http://localhost:8080' )
201+ chai .request . execute (' http://localhost:8080' )
218202 .get (' /' )
219203 .end ((err , res ) => {
220204 expect (res).to .have .status (123 ); // <= Test completes before this runs
@@ -232,7 +216,7 @@ If `Promise` is available, `request()` becomes a Promise capable library -
232216and chaining of ` then ` s becomes possible:
233217
234218``` js
235- chai .request (app)
219+ chai .request . execute (app)
236220 .put (' /user/me' )
237221 .send ({ password: ' 123' , confirmPassword: ' 123' })
238222 .then ((res ) => {
@@ -243,23 +227,6 @@ chai.request(app)
243227 });
244228```
245229
246- __ Note:__ Some older web browsers do not have native promise support. You can use any spec compliant library, such as:
247- - [ kriskowal/q] ( https://github.com/kriskowal/q )
248- - [ stefanpenner/es6-promise] ( https://github.com/stefanpenner/es6-promise )
249- - [ petkaantonov/bluebird] ( https://github.com/petkaantonov/bluebird )
250- - [ then/promise] ( https://github.com/then/promise )
251- You will need to set the library you use to ` global.Promise ` , before
252- requiring in chai-http. For example:
253-
254- ``` js
255- // Add promise support if this does not exist natively.
256- if (! global .Promise ) {
257- global .Promise = require (' q' );
258- }
259- const chai = require (' chai' );
260- chai .use (require (' chai-http' ));
261- ```
262-
263230#### Retaining cookies with each request
264231
265232Sometimes you need to keep cookies from one request, and send them with the
0 commit comments