You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To make the request and assert on its response, the `end` method can be used:
167
194
168
195
```js
169
-
chai.request.execute(app)
196
+
import {request} from'chai-http';
197
+
198
+
request.execute(app)
170
199
.put('/user/me')
171
200
.send({ password:'123', confirmPassword:'123' })
172
201
.end((err, res) => {
@@ -188,8 +217,10 @@ accomplished using the
188
217
callback has completed, and the assertions can be verified:
189
218
190
219
```js
220
+
import {request} from'chai-http';
221
+
191
222
it('fails, as expected', function(done) { // <= Pass in done callback
192
-
chai.request.execute('http://localhost:8080')
223
+
request.execute('http://localhost:8080')
193
224
.get('/')
194
225
.end((err, res) => {
195
226
expect(res).to.have.status(123);
@@ -198,7 +229,7 @@ it('fails, as expected', function(done) { // <= Pass in done callback
198
229
});
199
230
200
231
it('succeeds silently!', () => { // <= No done callback
201
-
chai.request.execute('http://localhost:8080')
232
+
request.execute('http://localhost:8080')
202
233
.get('/')
203
234
.end((err, res) => {
204
235
expect(res).to.have.status(123); // <= Test completes before this runs
@@ -212,11 +243,13 @@ error parameter when signaling completion.
212
243
213
244
#### Dealing with the response - Promises
214
245
215
-
If `Promise` is available, `request()` becomes a Promise capable library -
246
+
If `Promise` is available, `request` becomes a Promise capable library -
216
247
and chaining of `then`s becomes possible:
217
248
218
249
```js
219
-
chai.request.execute(app)
250
+
import {request} from'chai-http';
251
+
252
+
request.execute(app)
220
253
.put('/user/me')
221
254
.send({ password:'123', confirmPassword:'123' })
222
255
.then((res) => {
@@ -233,8 +266,10 @@ Sometimes you need to keep cookies from one request, and send them with the
233
266
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:
234
267
235
268
```js
269
+
import {request} from'chai-http';
270
+
236
271
// Log in
237
-
constagent=chai.request.agent(app)
272
+
constagent=request.agent(app)
238
273
agent
239
274
.post('/session')
240
275
.send({ username:'me', password:'123' })
@@ -249,7 +284,7 @@ agent
249
284
});
250
285
```
251
286
252
-
Note: The server started by `chai.request.agent(app)` will not automatically close following the test(s). You should call `agent.close()` after your tests to ensure your program exits.
287
+
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.
0 commit comments