@@ -28,7 +28,11 @@ chai.use(chaiHttp);
28
28
// if you need to access `request`
29
29
import {default as chaiHttp, request} from "chai-http";
30
30
chai.use(chaiHttp);
31
+
31
32
request.get(...).send(...);
33
+
34
+
// or setting up an app
35
+
request.execute(app);
32
36
```
33
37
34
38
To use Chai HTTP on a web page, please use the latest v4 version for now.
@@ -54,7 +58,9 @@ port to listen on for a given test.
54
58
__Note:__ This feature is only supported on Node.js, not in web browsers.
55
59
56
60
```js
57
-
chai.request.execute(app)
61
+
import {request} from 'chai-http';
62
+
63
+
request.execute(app)
58
64
.get('/')
59
65
```
60
66
@@ -65,7 +71,9 @@ keep the server open, perhaps if you're making multiple requests, you must call
65
71
`.keepOpen()` after `.request()`, and manually close the server down:
66
72
67
73
```js
68
-
const requester = chai.request.Request(app).keepOpen()
74
+
import {request} from 'chai-http';
75
+
76
+
const requester = request.Request(app).keepOpen()
69
77
70
78
Promise.all([
71
79
requester.get('/a'),
@@ -81,7 +89,9 @@ Promise.all([
81
89
You may also use a base url as the foundation of your request.
82
90
83
91
```js
84
-
chai.request.execute('http://localhost:8080')
92
+
import {request} from 'chai-http';
93
+
94
+
request.execute('http://localhost:8080')
85
95
.get('/')
86
96
```
87
97
@@ -102,25 +112,31 @@ Examples:
102
112
103
113
`.set()`
104
114
```js
115
+
import {request} from 'chai-http';
116
+
105
117
// Set a request header
106
-
chai.request.execute(app)
118
+
request.execute(app)
107
119
.put('/user/me')
108
120
.set('Content-Type', 'application/json')
109
121
.send({ password: '123', confirmPassword: '123' })
110
122
```
111
123
112
124
`.send()`
113
125
```js
126
+
import {request} from 'chai-http';
127
+
114
128
// Send some JSON
115
-
chai.request.execute(app)
129
+
request.execute(app)
116
130
.put('/user/me')
117
131
.send({ password: '123', confirmPassword: '123' })
118
132
```
119
133
120
134
`.type()`
121
135
```js
136
+
import {request} from 'chai-http';
137
+
122
138
// Send some Form Data
123
-
chai.request.execute(app)
139
+
request.execute(app)
124
140
.post('/user/me')
125
141
.type('form')
126
142
.send({
@@ -132,30 +148,36 @@ chai.request.execute(app)
132
148
133
149
`.attach()`
134
150
```js
151
+
import {request} from 'chai-http';
152
+
135
153
// Attach a file
136
-
chai.request.execute(app)
154
+
request.execute(app)
137
155
.post('/user/avatar')
138
156
.attach('imageField', fs.readFileSync('avatar.png'), 'avatar.png')
139
157
```
140
158
141
159
`.auth()`
142
160
```js
161
+
import {request} from 'chai-http';
162
+
143
163
// Authenticate with Basic authentication
144
-
chai.request.execute(app)
164
+
request.execute(app)
145
165
.get('/protected')
146
166
.auth('user', 'pass')
147
167
148
168
// Authenticate with Bearer Token
149
-
chai.request.execute(app)
169
+
request.execute(app)
150
170
.get('/protected')
151
171
.auth(accessToken, { type: 'bearer' })
152
172
153
173
```
154
174
155
175
`.query()`
156
176
```js
177
+
import {request} from 'chai-http';
178
+
157
179
// Chain some GET query parameters
158
-
chai.request.execute(app)
180
+
request.execute(app)
159
181
.get('/search')
160
182
.query({name: 'foo', limit: 10}) // /search?name=foo&limit=10
161
183
```
@@ -171,7 +193,9 @@ const { expect } = chai;
171
193
To make the request and assert on its response, the `end` method can be used:
172
194
173
195
```js
174
-
chai.request.execute(app)
196
+
import {request} from 'chai-http';
197
+
198
+
request.execute(app)
175
199
.put('/user/me')
176
200
.send({ password: '123', confirmPassword: '123' })
177
201
.end((err, res) => {
@@ -193,8 +217,10 @@ accomplished using the
193
217
callback has completed, and the assertions can be verified:
194
218
195
219
```js
220
+
import {request} from 'chai-http';
221
+
196
222
it('fails, as expected', function(done) { // <= Pass in done callback
197
-
chai.request.execute('http://localhost:8080')
223
+
request.execute('http://localhost:8080')
198
224
.get('/')
199
225
.end((err, res) => {
200
226
expect(res).to.have.status(123);
@@ -203,7 +229,7 @@ it('fails, as expected', function(done) { // <= Pass in done callback
203
229
});
204
230
205
231
it('succeeds silently!', () => { // <= No done callback
206
-
chai.request.execute('http://localhost:8080')
232
+
request.execute('http://localhost:8080')
207
233
.get('/')
208
234
.end((err, res) => {
209
235
expect(res).to.have.status(123); // <= Test completes before this runs
@@ -221,7 +247,9 @@ If `Promise` is available, `request()` becomes a Promise capable library -
221
247
and chaining of `then`s becomes possible:
222
248
223
249
```js
224
-
chai.request.execute(app)
250
+
import {request} from 'chai-http';
251
+
252
+
request.execute(app)
225
253
.put('/user/me')
226
254
.send({ password: '123', confirmPassword: '123' })
227
255
.then((res) => {
@@ -238,8 +266,10 @@ Sometimes you need to keep cookies from one request, and send them with the
238
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:
239
267
240
268
```js
269
+
import {request} from 'chai-http';
270
+
241
271
// Log in
242
-
const agent = chai.request.agent(app)
272
+
const agent = request.agent(app)
243
273
agent
244
274
.post('/session')
245
275
.send({ username: 'me', password: '123' })
@@ -254,7 +284,7 @@ agent
254
284
});
255
285
```
256
286
257
-
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.
258
288
259
289
## Assertions
260
290
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4