@eggjs/supertest 8.2.0 → 8.3.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -56
- package/dist/agent.d.ts +33 -0
- package/dist/agent.js +74 -0
- package/dist/error/AssertError.d.ts +8 -0
- package/dist/error/AssertError.js +15 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +24 -0
- package/dist/request.d.ts +25 -0
- package/dist/request.js +51 -0
- package/dist/test.d.ts +100 -0
- package/dist/test.js +237 -0
- package/dist/types.d.ts +14 -0
- package/package.json +23 -50
- package/dist/commonjs/agent.d.ts +0 -28
- package/dist/commonjs/agent.js +0 -94
- package/dist/commonjs/error/AssertError.d.ts +0 -5
- package/dist/commonjs/error/AssertError.js +0 -16
- package/dist/commonjs/index.d.ts +0 -14
- package/dist/commonjs/index.js +0 -47
- package/dist/commonjs/package.json +0 -3
- package/dist/commonjs/request.d.ts +0 -21
- package/dist/commonjs/request.js +0 -63
- package/dist/commonjs/test.d.ts +0 -96
- package/dist/commonjs/test.js +0 -326
- package/dist/commonjs/types.d.ts +0 -10
- package/dist/commonjs/types.js +0 -3
- package/dist/esm/agent.d.ts +0 -28
- package/dist/esm/agent.js +0 -87
- package/dist/esm/error/AssertError.d.ts +0 -5
- package/dist/esm/error/AssertError.js +0 -12
- package/dist/esm/index.d.ts +0 -14
- package/dist/esm/index.js +0 -30
- package/dist/esm/package.json +0 -3
- package/dist/esm/request.d.ts +0 -21
- package/dist/esm/request.js +0 -56
- package/dist/esm/test.d.ts +0 -96
- package/dist/esm/test.js +0 -322
- package/dist/esm/types.d.ts +0 -10
- package/dist/esm/types.js +0 -2
- package/dist/package.json +0 -4
- package/src/agent.ts +0 -96
- package/src/error/AssertError.ts +0 -12
- package/src/index.ts +0 -39
- package/src/request.ts +0 -62
- package/src/test.ts +0 -372
- package/src/types.ts +0 -17
package/README.md
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
# @eggjs/supertest
|
|
2
2
|
|
|
3
3
|
[![NPM version][npm-image]][npm-url]
|
|
4
|
-
[![code coverage][coverage-badge]][coverage]
|
|
5
|
-
[](https://github.com/eggjs/supertest/actions/workflows/nodejs.yml)
|
|
6
4
|
[](https://makeapullrequest.com)
|
|
7
5
|
[![MIT License][license-badge]][license]
|
|
8
6
|
[![npm download][download-image]][download-url]
|
|
@@ -10,14 +8,12 @@
|
|
|
10
8
|
|
|
11
9
|
[npm-image]: https://img.shields.io/npm/v/@eggjs/supertest.svg?style=flat-square
|
|
12
10
|
[npm-url]: https://npmjs.org/package/@eggjs/supertest
|
|
13
|
-
[coverage-badge]: https://img.shields.io/codecov/c/github/eggjs/supertest.svg
|
|
14
|
-
[coverage]: https://codecov.io/gh/eggjs/supertest
|
|
15
11
|
[license-badge]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square
|
|
16
12
|
[license]: https://github.com/eggjs/supertest/blob/master/LICENSE
|
|
17
13
|
[download-image]: https://img.shields.io/npm/dm/@eggjs/supertest.svg?style=flat-square
|
|
18
14
|
[download-url]: https://npmjs.org/package/@eggjs/supertest
|
|
19
15
|
|
|
20
|
-
> HTTP assertions made easy via [superagent](http://github.com/ladjs/superagent).
|
|
16
|
+
> HTTP assertions made easy via [superagent](http://github.com/ladjs/superagent). Maintained for [Forward Email](https://github.com/forwardemail) and [Lad](https://github.com/ladjs).
|
|
21
17
|
> Forked for TypeScript friendly
|
|
22
18
|
|
|
23
19
|
Document see [SuperTest](https://ladjs.github.io/superagent/)
|
|
@@ -35,7 +31,7 @@ Install SuperTest as an npm module and save it to your package.json file as a de
|
|
|
35
31
|
npm install @eggjs/supertest --save-dev
|
|
36
32
|
```
|
|
37
33
|
|
|
38
|
-
|
|
34
|
+
Once installed it can now be referenced by simply calling `require('supertest');`
|
|
39
35
|
|
|
40
36
|
## Example
|
|
41
37
|
|
|
@@ -52,7 +48,7 @@ const express = require('express');
|
|
|
52
48
|
|
|
53
49
|
const app = express();
|
|
54
50
|
|
|
55
|
-
app.get('/user', function(req, res) {
|
|
51
|
+
app.get('/user', function (req, res) {
|
|
56
52
|
res.status(200).json({ name: 'john' });
|
|
57
53
|
});
|
|
58
54
|
|
|
@@ -61,7 +57,7 @@ request(app)
|
|
|
61
57
|
.expect('Content-Type', /json/)
|
|
62
58
|
.expect('Content-Length', '15')
|
|
63
59
|
.expect(200)
|
|
64
|
-
.end(function(err, res) {
|
|
60
|
+
.end(function (err, res) {
|
|
65
61
|
if (err) throw err;
|
|
66
62
|
});
|
|
67
63
|
```
|
|
@@ -74,7 +70,7 @@ const express = require('express');
|
|
|
74
70
|
|
|
75
71
|
const app = express();
|
|
76
72
|
|
|
77
|
-
app.get('/user', function(req, res) {
|
|
73
|
+
app.get('/user', function (req, res) {
|
|
78
74
|
res.status(200).json({ name: 'john' });
|
|
79
75
|
});
|
|
80
76
|
|
|
@@ -83,16 +79,17 @@ request(app, { http2: true })
|
|
|
83
79
|
.expect('Content-Type', /json/)
|
|
84
80
|
.expect('Content-Length', '15')
|
|
85
81
|
.expect(200)
|
|
86
|
-
.end(function(err, res) {
|
|
82
|
+
.end(function (err, res) {
|
|
87
83
|
if (err) throw err;
|
|
88
84
|
});
|
|
89
85
|
|
|
90
|
-
request
|
|
86
|
+
request
|
|
87
|
+
.agent(app, { http2: true })
|
|
91
88
|
.get('/user')
|
|
92
89
|
.expect('Content-Type', /json/)
|
|
93
90
|
.expect('Content-Length', '15')
|
|
94
91
|
.expect(200)
|
|
95
|
-
.end(function(err, res) {
|
|
92
|
+
.end(function (err, res) {
|
|
96
93
|
if (err) throw err;
|
|
97
94
|
});
|
|
98
95
|
```
|
|
@@ -100,8 +97,8 @@ request.agent(app, { http2: true })
|
|
|
100
97
|
Here's an example with mocha, note how you can pass `done` straight to any of the `.expect()` calls:
|
|
101
98
|
|
|
102
99
|
```js
|
|
103
|
-
describe('GET /user', function() {
|
|
104
|
-
it('responds with json', function(done) {
|
|
100
|
+
describe('GET /user', function () {
|
|
101
|
+
it('responds with json', function (done) {
|
|
105
102
|
request(app)
|
|
106
103
|
.get('/user')
|
|
107
104
|
.set('Accept', 'application/json')
|
|
@@ -114,8 +111,8 @@ describe('GET /user', function() {
|
|
|
114
111
|
You can use `auth` method to pass HTTP username and password in the same way as in the [superagent](http://ladjs.github.io/superagent/#authentication):
|
|
115
112
|
|
|
116
113
|
```js
|
|
117
|
-
describe('GET /user', function() {
|
|
118
|
-
it('responds with json', function(done) {
|
|
114
|
+
describe('GET /user', function () {
|
|
115
|
+
it('responds with json', function (done) {
|
|
119
116
|
request(app)
|
|
120
117
|
.get('/user')
|
|
121
118
|
.auth('username', 'password')
|
|
@@ -135,15 +132,15 @@ not throw - they will return the assertion as an error to the `.end()` callback.
|
|
|
135
132
|
order to fail the test case, you will need to rethrow or pass `err` to `done()`, as follows:
|
|
136
133
|
|
|
137
134
|
```js
|
|
138
|
-
describe('POST /users', function() {
|
|
139
|
-
it('responds with json', function(done) {
|
|
135
|
+
describe('POST /users', function () {
|
|
136
|
+
it('responds with json', function (done) {
|
|
140
137
|
request(app)
|
|
141
138
|
.post('/users')
|
|
142
|
-
.send({name: 'john'})
|
|
139
|
+
.send({ name: 'john' })
|
|
143
140
|
.set('Accept', 'application/json')
|
|
144
141
|
.expect('Content-Type', /json/)
|
|
145
142
|
.expect(200)
|
|
146
|
-
.end(function(err, res) {
|
|
143
|
+
.end(function (err, res) {
|
|
147
144
|
if (err) return done(err);
|
|
148
145
|
return done();
|
|
149
146
|
});
|
|
@@ -154,16 +151,16 @@ describe('POST /users', function() {
|
|
|
154
151
|
You can also use promises:
|
|
155
152
|
|
|
156
153
|
```js
|
|
157
|
-
describe('GET /users', function() {
|
|
158
|
-
it('responds with json', function() {
|
|
154
|
+
describe('GET /users', function () {
|
|
155
|
+
it('responds with json', function () {
|
|
159
156
|
return request(app)
|
|
160
157
|
.get('/users')
|
|
161
158
|
.set('Accept', 'application/json')
|
|
162
159
|
.expect('Content-Type', /json/)
|
|
163
160
|
.expect(200)
|
|
164
161
|
.then(response => {
|
|
165
|
-
|
|
166
|
-
})
|
|
162
|
+
expect(response.body.email).toEqual('foo@bar.com');
|
|
163
|
+
});
|
|
167
164
|
});
|
|
168
165
|
});
|
|
169
166
|
```
|
|
@@ -171,12 +168,12 @@ describe('GET /users', function() {
|
|
|
171
168
|
Or async/await syntax:
|
|
172
169
|
|
|
173
170
|
```js
|
|
174
|
-
describe('GET /users', function() {
|
|
175
|
-
it('responds with json', async function() {
|
|
171
|
+
describe('GET /users', function () {
|
|
172
|
+
it('responds with json', async function () {
|
|
176
173
|
const response = await request(app)
|
|
177
174
|
.get('/users')
|
|
178
|
-
.set('Accept', 'application/json')
|
|
179
|
-
expect(response.headers[
|
|
175
|
+
.set('Accept', 'application/json');
|
|
176
|
+
expect(response.headers['Content-Type']).toMatch(/json/);
|
|
180
177
|
expect(response.status).toEqual(200);
|
|
181
178
|
expect(response.body.email).toEqual('foo@bar.com');
|
|
182
179
|
});
|
|
@@ -187,20 +184,24 @@ Expectations are run in the order of definition. This characteristic can be used
|
|
|
187
184
|
to modify the response body or headers before executing an assertion.
|
|
188
185
|
|
|
189
186
|
```js
|
|
190
|
-
describe('POST /user', function() {
|
|
191
|
-
it('user.name should be an case-insensitive match for "john"', function(done) {
|
|
187
|
+
describe('POST /user', function () {
|
|
188
|
+
it('user.name should be an case-insensitive match for "john"', function (done) {
|
|
192
189
|
request(app)
|
|
193
190
|
.post('/user')
|
|
194
191
|
.send('name=john') // x-www-form-urlencoded upload
|
|
195
192
|
.set('Accept', 'application/json')
|
|
196
|
-
.expect(function(res) {
|
|
193
|
+
.expect(function (res) {
|
|
197
194
|
res.body.id = 'some fixed id';
|
|
198
195
|
res.body.name = res.body.name.toLowerCase();
|
|
199
196
|
})
|
|
200
|
-
.expect(
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
197
|
+
.expect(
|
|
198
|
+
200,
|
|
199
|
+
{
|
|
200
|
+
id: 'some fixed id',
|
|
201
|
+
name: 'john',
|
|
202
|
+
},
|
|
203
|
+
done
|
|
204
|
+
);
|
|
204
205
|
});
|
|
205
206
|
});
|
|
206
207
|
```
|
|
@@ -223,11 +224,11 @@ initialization app or url, a new `Test` is created per `request.VERB()` call.
|
|
|
223
224
|
```js
|
|
224
225
|
t = request('http://localhost:5555');
|
|
225
226
|
|
|
226
|
-
t.get('/').expect(200, function(err){
|
|
227
|
+
t.get('/').expect(200, function (err) {
|
|
227
228
|
console.log(err);
|
|
228
229
|
});
|
|
229
230
|
|
|
230
|
-
t.get('/').expect('heya', function(err){
|
|
231
|
+
t.get('/').expect('heya', function (err) {
|
|
231
232
|
console.log(err);
|
|
232
233
|
});
|
|
233
234
|
```
|
|
@@ -240,32 +241,28 @@ const should = require('should');
|
|
|
240
241
|
const express = require('express');
|
|
241
242
|
const cookieParser = require('cookie-parser');
|
|
242
243
|
|
|
243
|
-
describe('request.agent(app)', function() {
|
|
244
|
+
describe('request.agent(app)', function () {
|
|
244
245
|
const app = express();
|
|
245
246
|
app.use(cookieParser());
|
|
246
247
|
|
|
247
|
-
app.get('/', function(req, res) {
|
|
248
|
+
app.get('/', function (req, res) {
|
|
248
249
|
res.cookie('cookie', 'hey');
|
|
249
250
|
res.send();
|
|
250
251
|
});
|
|
251
252
|
|
|
252
|
-
app.get('/return', function(req, res) {
|
|
253
|
+
app.get('/return', function (req, res) {
|
|
253
254
|
if (req.cookies.cookie) res.send(req.cookies.cookie);
|
|
254
|
-
else res.send(':(')
|
|
255
|
+
else res.send(':(');
|
|
255
256
|
});
|
|
256
257
|
|
|
257
258
|
const testAgent = agent(app);
|
|
258
259
|
|
|
259
|
-
it('should save cookies', function(done) {
|
|
260
|
-
testAgent
|
|
261
|
-
.get('/')
|
|
262
|
-
.expect('set-cookie', 'cookie=hey; Path=/', done);
|
|
260
|
+
it('should save cookies', function (done) {
|
|
261
|
+
testAgent.get('/').expect('set-cookie', 'cookie=hey; Path=/', done);
|
|
263
262
|
});
|
|
264
263
|
|
|
265
|
-
it('should send cookies', function(done) {
|
|
266
|
-
testAgent
|
|
267
|
-
.get('/return')
|
|
268
|
-
.expect('hey', done);
|
|
264
|
+
it('should send cookies', function (done) {
|
|
265
|
+
testAgent.get('/return').expect('hey', done);
|
|
269
266
|
});
|
|
270
267
|
});
|
|
271
268
|
```
|
|
@@ -317,14 +314,11 @@ Assert header `field` `value` with a string or regular expression.
|
|
|
317
314
|
Pass a custom assertion function. It'll be given the response object to check. If the check fails, throw an error.
|
|
318
315
|
|
|
319
316
|
```js
|
|
320
|
-
request(app)
|
|
321
|
-
.get('/')
|
|
322
|
-
.expect(hasPreviousAndNextKeys)
|
|
323
|
-
.end(done);
|
|
317
|
+
request(app).get('/').expect(hasPreviousAndNextKeys).end(done);
|
|
324
318
|
|
|
325
319
|
function hasPreviousAndNextKeys(res) {
|
|
326
|
-
if (!('next' in res.body)) throw new Error(
|
|
327
|
-
if (!('prev' in res.body)) throw new Error(
|
|
320
|
+
if (!('next' in res.body)) throw new Error('missing next key');
|
|
321
|
+
if (!('prev' in res.body)) throw new Error('missing prev key');
|
|
328
322
|
}
|
|
329
323
|
```
|
|
330
324
|
|
|
@@ -342,6 +336,6 @@ Inspired by [api-easy](https://github.com/flatiron/api-easy) minus vows coupling
|
|
|
342
336
|
|
|
343
337
|
## Contributors
|
|
344
338
|
|
|
345
|
-
[](https://github.com/eggjs/egg/graphs/contributors)
|
|
346
340
|
|
|
347
341
|
Made with [contributors-img](https://contrib.rocks).
|
package/dist/agent.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AgentOptions as AgentOptions$1, App } from "./types.js";
|
|
2
|
+
import { Test } from "./test.js";
|
|
3
|
+
import { agent } from "superagent";
|
|
4
|
+
import { Server } from "node:net";
|
|
5
|
+
|
|
6
|
+
//#region src/agent.d.ts
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Initialize a new `TestAgent`.
|
|
10
|
+
*
|
|
11
|
+
* @param {Function|Server} app
|
|
12
|
+
* @param {Object} options
|
|
13
|
+
*/
|
|
14
|
+
declare class TestAgent extends agent {
|
|
15
|
+
#private;
|
|
16
|
+
app: Server | string;
|
|
17
|
+
_host: string;
|
|
18
|
+
constructor(appOrListener: App, options?: AgentOptions$1);
|
|
19
|
+
host(host: string): this;
|
|
20
|
+
protected _testRequest(method: string, url: string): Test;
|
|
21
|
+
delete(url: string): Test;
|
|
22
|
+
del(url: string): Test;
|
|
23
|
+
get(url: string): Test;
|
|
24
|
+
head(url: string): Test;
|
|
25
|
+
put(url: string): Test;
|
|
26
|
+
post(url: string): Test;
|
|
27
|
+
patch(url: string): Test;
|
|
28
|
+
options(url: string): Test;
|
|
29
|
+
trace(url: string): Test;
|
|
30
|
+
}
|
|
31
|
+
declare const proxyAgent: typeof TestAgent;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { TestAgent, proxyAgent };
|
package/dist/agent.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Test } from "./test.js";
|
|
2
|
+
import http from "node:http";
|
|
3
|
+
import http2 from "node:http2";
|
|
4
|
+
import { agent } from "superagent";
|
|
5
|
+
|
|
6
|
+
//#region src/agent.ts
|
|
7
|
+
/**
|
|
8
|
+
* Initialize a new `TestAgent`.
|
|
9
|
+
*
|
|
10
|
+
* @param {Function|Server} app
|
|
11
|
+
* @param {Object} options
|
|
12
|
+
*/
|
|
13
|
+
var TestAgent = class extends agent {
|
|
14
|
+
app;
|
|
15
|
+
_host;
|
|
16
|
+
#http2 = false;
|
|
17
|
+
constructor(appOrListener, options = {}) {
|
|
18
|
+
super(options);
|
|
19
|
+
if (typeof appOrListener === "function") if (options.http2) {
|
|
20
|
+
this.#http2 = true;
|
|
21
|
+
this.app = http2.createServer(appOrListener);
|
|
22
|
+
} else this.app = http.createServer(appOrListener);
|
|
23
|
+
else this.app = appOrListener;
|
|
24
|
+
}
|
|
25
|
+
host(host) {
|
|
26
|
+
this._host = host;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
_testRequest(method, url) {
|
|
30
|
+
const req = new Test(this.app, method.toUpperCase(), url);
|
|
31
|
+
if (this.#http2) req.http2();
|
|
32
|
+
if (this._host) req.set("host", this._host);
|
|
33
|
+
const that = this;
|
|
34
|
+
req.on("response", that._saveCookies.bind(this));
|
|
35
|
+
req.on("redirect", that._saveCookies.bind(this));
|
|
36
|
+
req.on("redirect", that._attachCookies.bind(this, req));
|
|
37
|
+
that._setDefaults(req);
|
|
38
|
+
that._attachCookies(req);
|
|
39
|
+
return req;
|
|
40
|
+
}
|
|
41
|
+
delete(url) {
|
|
42
|
+
return this._testRequest("delete", url);
|
|
43
|
+
}
|
|
44
|
+
del(url) {
|
|
45
|
+
return this._testRequest("delete", url);
|
|
46
|
+
}
|
|
47
|
+
get(url) {
|
|
48
|
+
return this._testRequest("get", url);
|
|
49
|
+
}
|
|
50
|
+
head(url) {
|
|
51
|
+
return this._testRequest("head", url);
|
|
52
|
+
}
|
|
53
|
+
put(url) {
|
|
54
|
+
return this._testRequest("put", url);
|
|
55
|
+
}
|
|
56
|
+
post(url) {
|
|
57
|
+
return this._testRequest("post", url);
|
|
58
|
+
}
|
|
59
|
+
patch(url) {
|
|
60
|
+
return this._testRequest("patch", url);
|
|
61
|
+
}
|
|
62
|
+
options(url) {
|
|
63
|
+
return this._testRequest("options", url);
|
|
64
|
+
}
|
|
65
|
+
trace(url) {
|
|
66
|
+
return this._testRequest("trace", url);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const proxyAgent = new Proxy(TestAgent, { apply(target, _, argumentsList) {
|
|
70
|
+
return new target(argumentsList[0], argumentsList[1]);
|
|
71
|
+
} });
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
export { TestAgent, proxyAgent };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/error/AssertError.ts
|
|
2
|
+
var AssertError = class extends Error {
|
|
3
|
+
expected;
|
|
4
|
+
actual;
|
|
5
|
+
constructor(message, expected, actual, options) {
|
|
6
|
+
super(message, options);
|
|
7
|
+
this.name = this.constructor.name;
|
|
8
|
+
this.expected = expected;
|
|
9
|
+
this.actual = actual;
|
|
10
|
+
Error.captureStackTrace(this, this.constructor);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { AssertError };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AgentOptions, App } from "./types.js";
|
|
2
|
+
import { AssertFunction, CallbackFunction, ExpectHeader, ResponseError, Test, TestApplication } from "./test.js";
|
|
3
|
+
import { Request, RequestOptions } from "./request.js";
|
|
4
|
+
import { TestAgent, proxyAgent } from "./agent.js";
|
|
5
|
+
|
|
6
|
+
//#region src/index.d.ts
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Test against the given `app`,
|
|
10
|
+
* returning a new `Test`.
|
|
11
|
+
*/
|
|
12
|
+
declare function request(app: App, options?: RequestOptions): Request;
|
|
13
|
+
declare const _default: ((app: App, options?: RequestOptions) => Request) & {
|
|
14
|
+
agent: (app: App, options?: AgentOptions) => TestAgent;
|
|
15
|
+
};
|
|
16
|
+
//#endregion
|
|
17
|
+
export { AssertFunction, CallbackFunction, ExpectHeader, Request, type RequestOptions, ResponseError, Test, TestAgent, TestApplication, proxyAgent as agent, _default as default, request };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Test } from "./test.js";
|
|
2
|
+
import { Request } from "./request.js";
|
|
3
|
+
import { TestAgent, proxyAgent } from "./agent.js";
|
|
4
|
+
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
/**
|
|
7
|
+
* Test against the given `app`,
|
|
8
|
+
* returning a new `Test`.
|
|
9
|
+
*/
|
|
10
|
+
function request(app, options = {}) {
|
|
11
|
+
return new Request(app, options);
|
|
12
|
+
}
|
|
13
|
+
var src_default = new Proxy(request, {
|
|
14
|
+
apply(target, _, argumentsList) {
|
|
15
|
+
return target(argumentsList[0], argumentsList[1]);
|
|
16
|
+
},
|
|
17
|
+
get(target, property, receiver) {
|
|
18
|
+
if (property === "agent") return proxyAgent;
|
|
19
|
+
return Reflect.get(target, property, receiver);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { Request, Test, TestAgent, proxyAgent as agent, src_default as default, request };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { App } from "./types.js";
|
|
2
|
+
import { Test } from "./test.js";
|
|
3
|
+
import { Server } from "node:net";
|
|
4
|
+
|
|
5
|
+
//#region src/request.d.ts
|
|
6
|
+
interface RequestOptions {
|
|
7
|
+
http2?: boolean;
|
|
8
|
+
}
|
|
9
|
+
declare class Request {
|
|
10
|
+
#private;
|
|
11
|
+
app: string | Server;
|
|
12
|
+
constructor(appOrListener: App, options?: RequestOptions);
|
|
13
|
+
protected _testRequest(method: string, url: string): Test;
|
|
14
|
+
delete(url: string): Test;
|
|
15
|
+
del(url: string): Test;
|
|
16
|
+
get(url: string): Test;
|
|
17
|
+
head(url: string): Test;
|
|
18
|
+
put(url: string): Test;
|
|
19
|
+
post(url: string): Test;
|
|
20
|
+
patch(url: string): Test;
|
|
21
|
+
options(url: string): Test;
|
|
22
|
+
trace(url: string): Test;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { Request, RequestOptions };
|
package/dist/request.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Test } from "./test.js";
|
|
2
|
+
import http from "node:http";
|
|
3
|
+
import http2 from "node:http2";
|
|
4
|
+
|
|
5
|
+
//#region src/request.ts
|
|
6
|
+
var Request = class {
|
|
7
|
+
app;
|
|
8
|
+
#http2 = false;
|
|
9
|
+
constructor(appOrListener, options = {}) {
|
|
10
|
+
if (typeof appOrListener === "function") if (options.http2) {
|
|
11
|
+
this.#http2 = true;
|
|
12
|
+
this.app = http2.createServer(appOrListener);
|
|
13
|
+
} else this.app = http.createServer(appOrListener);
|
|
14
|
+
else this.app = appOrListener;
|
|
15
|
+
}
|
|
16
|
+
_testRequest(method, url) {
|
|
17
|
+
const req = new Test(this.app, method.toUpperCase(), url);
|
|
18
|
+
if (this.#http2) req.http2();
|
|
19
|
+
return req;
|
|
20
|
+
}
|
|
21
|
+
delete(url) {
|
|
22
|
+
return this._testRequest("delete", url);
|
|
23
|
+
}
|
|
24
|
+
del(url) {
|
|
25
|
+
return this._testRequest("delete", url);
|
|
26
|
+
}
|
|
27
|
+
get(url) {
|
|
28
|
+
return this._testRequest("get", url);
|
|
29
|
+
}
|
|
30
|
+
head(url) {
|
|
31
|
+
return this._testRequest("head", url);
|
|
32
|
+
}
|
|
33
|
+
put(url) {
|
|
34
|
+
return this._testRequest("put", url);
|
|
35
|
+
}
|
|
36
|
+
post(url) {
|
|
37
|
+
return this._testRequest("post", url);
|
|
38
|
+
}
|
|
39
|
+
patch(url) {
|
|
40
|
+
return this._testRequest("patch", url);
|
|
41
|
+
}
|
|
42
|
+
options(url) {
|
|
43
|
+
return this._testRequest("options", url);
|
|
44
|
+
}
|
|
45
|
+
trace(url) {
|
|
46
|
+
return this._testRequest("trace", url);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
export { Request };
|
package/dist/test.d.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { AssertError } from "./error/AssertError.js";
|
|
2
|
+
import { Request, Response } from "superagent";
|
|
3
|
+
import { Server } from "node:net";
|
|
4
|
+
|
|
5
|
+
//#region src/test.d.ts
|
|
6
|
+
type TestApplication = Server | string;
|
|
7
|
+
type AssertFunction = (res: Response) => AssertError | void;
|
|
8
|
+
type CallbackFunction = (err: AssertError | Error | null, res: Response) => void;
|
|
9
|
+
type ResponseError = Error & {
|
|
10
|
+
syscall?: string;
|
|
11
|
+
code?: string;
|
|
12
|
+
status?: number;
|
|
13
|
+
};
|
|
14
|
+
interface ExpectHeader {
|
|
15
|
+
name: string;
|
|
16
|
+
value: string | number | RegExp;
|
|
17
|
+
}
|
|
18
|
+
declare class Test extends Request {
|
|
19
|
+
app: TestApplication;
|
|
20
|
+
_server: Server;
|
|
21
|
+
_asserts: AssertFunction[];
|
|
22
|
+
/**
|
|
23
|
+
* Initialize a new `Test` with the given `app`,
|
|
24
|
+
* request `method` and `path`.
|
|
25
|
+
*/
|
|
26
|
+
constructor(app: TestApplication, method: string, path: string);
|
|
27
|
+
/**
|
|
28
|
+
* Returns a URL, extracted from a server.
|
|
29
|
+
*
|
|
30
|
+
* @return {String} URL address
|
|
31
|
+
* @private
|
|
32
|
+
*/
|
|
33
|
+
protected serverAddress(app: Server, path: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Expectations:
|
|
36
|
+
*
|
|
37
|
+
* ```js
|
|
38
|
+
* .expect(200)
|
|
39
|
+
* .expect(200, fn)
|
|
40
|
+
* .expect(200, body)
|
|
41
|
+
* .expect('Some body')
|
|
42
|
+
* .expect('Some body', fn)
|
|
43
|
+
* .expect(['json array body', { key: 'val' }])
|
|
44
|
+
* .expect('Content-Type', 'application/json')
|
|
45
|
+
* .expect('Content-Type', 'application/json', fn)
|
|
46
|
+
* .expect(fn)
|
|
47
|
+
* .expect([200, 404])
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @return {Test} The current Test instance for chaining.
|
|
51
|
+
*/
|
|
52
|
+
expect(a: number | string | RegExp | object | AssertFunction, b?: string | number | RegExp | CallbackFunction, c?: CallbackFunction): Test;
|
|
53
|
+
/**
|
|
54
|
+
* UnExpectations:
|
|
55
|
+
*
|
|
56
|
+
* .unexpectHeader('Content-Type')
|
|
57
|
+
* .unexpectHeader('Content-Type', fn)
|
|
58
|
+
*/
|
|
59
|
+
unexpectHeader(name: string, fn?: CallbackFunction): this;
|
|
60
|
+
/**
|
|
61
|
+
* Expectations:
|
|
62
|
+
*
|
|
63
|
+
* .expectHeader('Content-Type')
|
|
64
|
+
* .expectHeader('Content-Type', fn)
|
|
65
|
+
*/
|
|
66
|
+
expectHeader(name: string, fn?: CallbackFunction): this;
|
|
67
|
+
_unexpectHeader(name: string, res: Response): AssertError | undefined;
|
|
68
|
+
_expectHeader(name: string, res: Response): AssertError | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* Defer invoking superagent's `.end()` until
|
|
71
|
+
* the server is listening.
|
|
72
|
+
*/
|
|
73
|
+
end(fn: CallbackFunction): this;
|
|
74
|
+
/**
|
|
75
|
+
* Perform assertions and invoke `fn(err, res)`.
|
|
76
|
+
*/
|
|
77
|
+
assert(resError: ResponseError | null, res: Response, fn: CallbackFunction): void;
|
|
78
|
+
/**
|
|
79
|
+
* Perform assertions on a response body and return an Error upon failure.
|
|
80
|
+
*/
|
|
81
|
+
_assertBody(body: RegExp | string | number | object | null | undefined, res: Response): AssertError | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* Perform assertions on a response header and return an Error upon failure.
|
|
84
|
+
*/
|
|
85
|
+
_assertHeader(header: ExpectHeader, res: Response): AssertError | undefined;
|
|
86
|
+
/**
|
|
87
|
+
* Perform assertions on the response status and return an Error upon failure.
|
|
88
|
+
*/
|
|
89
|
+
_assertStatus(status: number, res: Response): AssertError | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* Perform assertions on the response status and return an Error upon failure.
|
|
92
|
+
*/
|
|
93
|
+
_assertStatusArray(statusArray: number[], res: Response): AssertError | undefined;
|
|
94
|
+
/**
|
|
95
|
+
* Performs an assertion by calling a function and return an Error upon failure.
|
|
96
|
+
*/
|
|
97
|
+
_assertFunction(fn: AssertFunction, res: Response): Error | undefined;
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
export { AssertFunction, CallbackFunction, ExpectHeader, ResponseError, Test, TestApplication };
|