@eggjs/supertest 8.0.0

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/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2014 TJ Holowaychuk <tj@vision-media.ca>
4
+ Copyright (c) 2024-present eggjs and the contributors.
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ 'Software'), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,347 @@
1
+ # @eggjs/supertest
2
+
3
+ [![NPM version][npm-image]][npm-url]
4
+ [![code coverage][coverage-badge]][coverage]
5
+ [![Node.js CI](https://github.com/eggjs/supertest/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/supertest/actions/workflows/nodejs.yml)
6
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
7
+ [![MIT License][license-badge]][license]
8
+ [![npm download][download-image]][download-url]
9
+ [![Node.js Version](https://img.shields.io/node/v/@eggjs/mock.svg?style=flat)](https://nodejs.org/en/download/)
10
+
11
+ [npm-image]: https://img.shields.io/npm/v/@eggjs/supertest.svg?style=flat-square
12
+ [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
+ [license-badge]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square
16
+ [license]: https://github.com/eggjs/supertest/blob/master/LICENSE
17
+ [download-image]: https://img.shields.io/npm/dm/@eggjs/supertest.svg?style=flat-square
18
+ [download-url]: https://npmjs.org/package/@eggjs/supertest
19
+
20
+ > 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
+ > Forked for TypeScript friendly
22
+
23
+ Document see [SuperTest](https://ladjs.github.io/superagent/)
24
+
25
+ ## About
26
+
27
+ The motivation with this module is to provide a high-level abstraction for testing
28
+ HTTP, while still allowing you to drop down to the [lower-level API](https://ladjs.github.io/superagent/) provided by superagent.
29
+
30
+ ## Getting Started
31
+
32
+ Install SuperTest as an npm module and save it to your package.json file as a development dependency:
33
+
34
+ ```bash
35
+ npm install @eggjs/supertest --save-dev
36
+ ```
37
+
38
+ Once installed it can now be referenced by simply calling ```require('supertest');```
39
+
40
+ ## Example
41
+
42
+ You may pass an `http.Server`, or a `Function` to `request()` - if the server is not
43
+ already listening for connections then it is bound to an ephemeral port for you so
44
+ there is no need to keep track of ports.
45
+
46
+ SuperTest works with any test framework, here is an example without using any
47
+ test framework at all:
48
+
49
+ ```js
50
+ const { request } = require('@eggjs/supertest');
51
+ const express = require('express');
52
+
53
+ const app = express();
54
+
55
+ app.get('/user', function(req, res) {
56
+ res.status(200).json({ name: 'john' });
57
+ });
58
+
59
+ request(app)
60
+ .get('/user')
61
+ .expect('Content-Type', /json/)
62
+ .expect('Content-Length', '15')
63
+ .expect(200)
64
+ .end(function(err, res) {
65
+ if (err) throw err;
66
+ });
67
+ ```
68
+
69
+ To enable http2 protocol, simply append an options to `request` or `request.agent`:
70
+
71
+ ```js
72
+ const { request } = require('@eggjs/supertest');
73
+ const express = require('express');
74
+
75
+ const app = express();
76
+
77
+ app.get('/user', function(req, res) {
78
+ res.status(200).json({ name: 'john' });
79
+ });
80
+
81
+ request(app, { http2: true })
82
+ .get('/user')
83
+ .expect('Content-Type', /json/)
84
+ .expect('Content-Length', '15')
85
+ .expect(200)
86
+ .end(function(err, res) {
87
+ if (err) throw err;
88
+ });
89
+
90
+ request.agent(app, { http2: true })
91
+ .get('/user')
92
+ .expect('Content-Type', /json/)
93
+ .expect('Content-Length', '15')
94
+ .expect(200)
95
+ .end(function(err, res) {
96
+ if (err) throw err;
97
+ });
98
+ ```
99
+
100
+ Here's an example with mocha, note how you can pass `done` straight to any of the `.expect()` calls:
101
+
102
+ ```js
103
+ describe('GET /user', function() {
104
+ it('responds with json', function(done) {
105
+ request(app)
106
+ .get('/user')
107
+ .set('Accept', 'application/json')
108
+ .expect('Content-Type', /json/)
109
+ .expect(200, done);
110
+ });
111
+ });
112
+ ```
113
+
114
+ 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
+
116
+ ```js
117
+ describe('GET /user', function() {
118
+ it('responds with json', function(done) {
119
+ request(app)
120
+ .get('/user')
121
+ .auth('username', 'password')
122
+ .set('Accept', 'application/json')
123
+ .expect('Content-Type', /json/)
124
+ .expect(200, done);
125
+ });
126
+ });
127
+ ```
128
+
129
+ One thing to note with the above statement is that superagent now sends any HTTP
130
+ error (anything other than a 2XX response code) to the callback as the first argument if
131
+ you do not add a status code expect (i.e. `.expect(302)`).
132
+
133
+ If you are using the `.end()` method `.expect()` assertions that fail will
134
+ not throw - they will return the assertion as an error to the `.end()` callback. In
135
+ order to fail the test case, you will need to rethrow or pass `err` to `done()`, as follows:
136
+
137
+ ```js
138
+ describe('POST /users', function() {
139
+ it('responds with json', function(done) {
140
+ request(app)
141
+ .post('/users')
142
+ .send({name: 'john'})
143
+ .set('Accept', 'application/json')
144
+ .expect('Content-Type', /json/)
145
+ .expect(200)
146
+ .end(function(err, res) {
147
+ if (err) return done(err);
148
+ return done();
149
+ });
150
+ });
151
+ });
152
+ ```
153
+
154
+ You can also use promises:
155
+
156
+ ```js
157
+ describe('GET /users', function() {
158
+ it('responds with json', function() {
159
+ return request(app)
160
+ .get('/users')
161
+ .set('Accept', 'application/json')
162
+ .expect('Content-Type', /json/)
163
+ .expect(200)
164
+ .then(response => {
165
+ expect(response.body.email).toEqual('foo@bar.com');
166
+ })
167
+ });
168
+ });
169
+ ```
170
+
171
+ Or async/await syntax:
172
+
173
+ ```js
174
+ describe('GET /users', function() {
175
+ it('responds with json', async function() {
176
+ const response = await request(app)
177
+ .get('/users')
178
+ .set('Accept', 'application/json')
179
+ expect(response.headers["Content-Type"]).toMatch(/json/);
180
+ expect(response.status).toEqual(200);
181
+ expect(response.body.email).toEqual('foo@bar.com');
182
+ });
183
+ });
184
+ ```
185
+
186
+ Expectations are run in the order of definition. This characteristic can be used
187
+ to modify the response body or headers before executing an assertion.
188
+
189
+ ```js
190
+ describe('POST /user', function() {
191
+ it('user.name should be an case-insensitive match for "john"', function(done) {
192
+ request(app)
193
+ .post('/user')
194
+ .send('name=john') // x-www-form-urlencoded upload
195
+ .set('Accept', 'application/json')
196
+ .expect(function(res) {
197
+ res.body.id = 'some fixed id';
198
+ res.body.name = res.body.name.toLowerCase();
199
+ })
200
+ .expect(200, {
201
+ id: 'some fixed id',
202
+ name: 'john'
203
+ }, done);
204
+ });
205
+ });
206
+ ```
207
+
208
+ Anything you can do with superagent, you can do with supertest - for example multipart file uploads!
209
+
210
+ ```js
211
+ request(app)
212
+ .post('/')
213
+ .field('name', 'my awesome avatar')
214
+ .field('complex_object', '{"attribute": "value"}', {contentType: 'application/json'})
215
+ .attach('avatar', 'test/fixtures/avatar.jpg')
216
+ ...
217
+ ```
218
+
219
+ Passing the app or url each time is not necessary, if you're testing
220
+ the same host you may simply re-assign the request variable with the
221
+ initialization app or url, a new `Test` is created per `request.VERB()` call.
222
+
223
+ ```js
224
+ t = request('http://localhost:5555');
225
+
226
+ t.get('/').expect(200, function(err){
227
+ console.log(err);
228
+ });
229
+
230
+ t.get('/').expect('heya', function(err){
231
+ console.log(err);
232
+ });
233
+ ```
234
+
235
+ Here's an example with mocha that shows how to persist a request and its cookies:
236
+
237
+ ```js
238
+ const { agent } = require('@eggjs/supertest');
239
+ const should = require('should');
240
+ const express = require('express');
241
+ const cookieParser = require('cookie-parser');
242
+
243
+ describe('request.agent(app)', function() {
244
+ const app = express();
245
+ app.use(cookieParser());
246
+
247
+ app.get('/', function(req, res) {
248
+ res.cookie('cookie', 'hey');
249
+ res.send();
250
+ });
251
+
252
+ app.get('/return', function(req, res) {
253
+ if (req.cookies.cookie) res.send(req.cookies.cookie);
254
+ else res.send(':(')
255
+ });
256
+
257
+ const testAgent = agent(app);
258
+
259
+ it('should save cookies', function(done) {
260
+ testAgent
261
+ .get('/')
262
+ .expect('set-cookie', 'cookie=hey; Path=/', done);
263
+ });
264
+
265
+ it('should send cookies', function(done) {
266
+ testAgent
267
+ .get('/return')
268
+ .expect('hey', done);
269
+ });
270
+ });
271
+ ```
272
+
273
+ There is another example that is introduced by the file [agency.js](https://github.com/ladjs/superagent/blob/master/test/node/agency.js)
274
+
275
+ Here is an example where 2 cookies are set on the request.
276
+
277
+ ```js
278
+ agent(app)
279
+ .get('/api/content')
280
+ .set('Cookie', ['nameOne=valueOne;nameTwo=valueTwo'])
281
+ .send()
282
+ .expect(200)
283
+ .end((err, res) => {
284
+ if (err) {
285
+ return done(err);
286
+ }
287
+ expect(res.text).to.be.equal('hey');
288
+ return done();
289
+ });
290
+ ```
291
+
292
+ ## API
293
+
294
+ You may use any [superagent](http://github.com/ladjs/superagent) methods,
295
+ including `.write()`, `.pipe()` etc and perform assertions in the `.end()` callback
296
+ for lower-level needs.
297
+
298
+ ### .expect(status[, fn])
299
+
300
+ Assert response `status` code.
301
+
302
+ ### .expect(status, body[, fn])
303
+
304
+ Assert response `status` code and `body`.
305
+
306
+ ### .expect(body[, fn])
307
+
308
+ Assert response `body` text with a string, regular expression, or
309
+ parsed body object.
310
+
311
+ ### .expect(field, value[, fn])
312
+
313
+ Assert header `field` `value` with a string or regular expression.
314
+
315
+ ### .expect(function(res) {})
316
+
317
+ Pass a custom assertion function. It'll be given the response object to check. If the check fails, throw an error.
318
+
319
+ ```js
320
+ request(app)
321
+ .get('/')
322
+ .expect(hasPreviousAndNextKeys)
323
+ .end(done);
324
+
325
+ function hasPreviousAndNextKeys(res) {
326
+ if (!('next' in res.body)) throw new Error("missing next key");
327
+ if (!('prev' in res.body)) throw new Error("missing prev key");
328
+ }
329
+ ```
330
+
331
+ ### .end(fn)
332
+
333
+ Perform the request and invoke `fn(err, res)`.
334
+
335
+ ## Notes
336
+
337
+ Inspired by [api-easy](https://github.com/flatiron/api-easy) minus vows coupling.
338
+
339
+ ## License
340
+
341
+ [MIT](LICENSE)
342
+
343
+ ## Contributors
344
+
345
+ [![Contributors](https://contrib.rocks/image?repo=eggjs/supertest)](https://github.com/eggjs/supertest/graphs/contributors)
346
+
347
+ Made with [contributors-img](https://contrib.rocks).
@@ -0,0 +1,27 @@
1
+ import type { Server } from 'node:net';
2
+ import { agent as Agent } from 'superagent';
3
+ import { Test } from './test.js';
4
+ import type { AgentOptions, App } from './types.js';
5
+ /**
6
+ * Initialize a new `TestAgent`.
7
+ *
8
+ * @param {Function|Server} app
9
+ * @param {Object} options
10
+ */
11
+ export declare class TestAgent extends Agent {
12
+ #private;
13
+ app: Server | string;
14
+ _host: string;
15
+ constructor(appOrListener: App, options?: AgentOptions);
16
+ host(host: string): this;
17
+ protected _testRequest(method: string, url: string): Test;
18
+ delete(url: string): Test;
19
+ del(url: string): Test;
20
+ get(url: string): Test;
21
+ head(url: string): Test;
22
+ put(url: string): Test;
23
+ post(url: string): Test;
24
+ patch(url: string): Test;
25
+ options(url: string): Test;
26
+ }
27
+ export declare const proxyAgent: typeof TestAgent;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.proxyAgent = exports.TestAgent = void 0;
7
+ const node_http_1 = __importDefault(require("node:http"));
8
+ const node_http2_1 = __importDefault(require("node:http2"));
9
+ const superagent_1 = require("superagent");
10
+ const test_js_1 = require("./test.js");
11
+ /**
12
+ * Initialize a new `TestAgent`.
13
+ *
14
+ * @param {Function|Server} app
15
+ * @param {Object} options
16
+ */
17
+ class TestAgent extends superagent_1.agent {
18
+ app;
19
+ _host;
20
+ #http2 = false;
21
+ constructor(appOrListener, options = {}) {
22
+ super(options);
23
+ if (typeof appOrListener === 'function') {
24
+ if (options.http2) {
25
+ this.#http2 = true;
26
+ this.app = node_http2_1.default.createServer(appOrListener); // eslint-disable-line no-param-reassign
27
+ }
28
+ else {
29
+ this.app = node_http_1.default.createServer(appOrListener); // eslint-disable-line no-param-reassign
30
+ }
31
+ }
32
+ else {
33
+ this.app = appOrListener;
34
+ }
35
+ }
36
+ // set a host name
37
+ host(host) {
38
+ this._host = host;
39
+ return this;
40
+ }
41
+ // TestAgent.prototype.del = TestAgent.prototype.delete;
42
+ _testRequest(method, url) {
43
+ const req = new test_js_1.Test(this.app, method.toUpperCase(), url);
44
+ if (this.#http2) {
45
+ req.http2();
46
+ }
47
+ if (this._host) {
48
+ req.set('host', this._host);
49
+ }
50
+ const that = this;
51
+ // access not internal methods
52
+ req.on('response', that._saveCookies.bind(this));
53
+ req.on('redirect', that._saveCookies.bind(this));
54
+ req.on('redirect', that._attachCookies.bind(this, req));
55
+ that._setDefaults(req);
56
+ that._attachCookies(req);
57
+ return req;
58
+ }
59
+ delete(url) {
60
+ return this._testRequest('delete', url);
61
+ }
62
+ del(url) {
63
+ return this._testRequest('delete', url);
64
+ }
65
+ get(url) {
66
+ return this._testRequest('get', url);
67
+ }
68
+ head(url) {
69
+ return this._testRequest('head', url);
70
+ }
71
+ put(url) {
72
+ return this._testRequest('put', url);
73
+ }
74
+ post(url) {
75
+ return this._testRequest('post', url);
76
+ }
77
+ patch(url) {
78
+ return this._testRequest('patch', url);
79
+ }
80
+ options(url) {
81
+ return this._testRequest('options', url);
82
+ }
83
+ }
84
+ exports.TestAgent = TestAgent;
85
+ // allow keep use by `agent()`
86
+ exports.proxyAgent = new Proxy(TestAgent, {
87
+ apply(target, _, argumentsList) {
88
+ return new target(argumentsList[0], argumentsList[1]);
89
+ },
90
+ });
91
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWdlbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvYWdlbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBQUEsMERBQTZCO0FBQzdCLDREQUErQjtBQUUvQiwyQ0FBNEM7QUFDNUMsdUNBQWlDO0FBR2pDOzs7OztHQUtHO0FBRUgsTUFBYSxTQUFVLFNBQVEsa0JBQUs7SUFDbEMsR0FBRyxDQUFrQjtJQUNyQixLQUFLLENBQVM7SUFDZCxNQUFNLEdBQUcsS0FBSyxDQUFDO0lBRWYsWUFBWSxhQUFrQixFQUFFLFVBQXdCLEVBQUU7UUFDeEQsS0FBSyxDQUFDLE9BQU8sQ0FBQyxDQUFDO1FBQ2YsSUFBSSxPQUFPLGFBQWEsS0FBSyxVQUFVLEVBQUUsQ0FBQztZQUN4QyxJQUFJLE9BQU8sQ0FBQyxLQUFLLEVBQUUsQ0FBQztnQkFDbEIsSUFBSSxDQUFDLE1BQU0sR0FBRyxJQUFJLENBQUM7Z0JBQ25CLElBQUksQ0FBQyxHQUFHLEdBQUcsb0JBQUssQ0FBQyxZQUFZLENBQU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BQztJQUNELEtBQUssQ0FBQyxHQUFXO1FBQ2YsT0FBTyxJQUFJLENBQUMsWUFBWSxDQUFDLE9BQU8sRUFBRSxHQUFHLENBQUMsQ0FBQztJQUN6QyxDQUFDO0lBQ0QsT0FBTyxDQUFDLEdBQVc7UUFDakIsT0FBTyxJQUFJLENBQUMsWUFBWSxDQUFDLFNBQVMsRUFBRSxHQUFHLENBQUMsQ0FBQztJQUMzQyxDQUFDO0NBQ0Y7QUF2RUQsOEJBdUVDO0FBRUQsOEJBQThCO0FBQ2pCLFFBQUEsVUFBVSxHQUFHLElBQUksS0FBSyxDQUFDLFNBQVMsRUFBRTtJQUM3QyxLQUFLLENBQUMsTUFBTSxFQUFFLENBQUMsRUFBRSxhQUFhO1FBQzVCLE9BQU8sSUFBSSxNQUFNLENBQUMsYUFBYSxDQUFDLENBQUMsQ0FBQyxFQUFFLGFBQWEsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDO0lBQ3hELENBQUM7Q0FDRixDQUFDLENBQUMifQ==
@@ -0,0 +1,5 @@
1
+ export declare class AssertError extends Error {
2
+ expected: any;
3
+ actual: any;
4
+ constructor(message: string, expected: any, actual: any, options?: ErrorOptions);
5
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AssertError = void 0;
4
+ class AssertError extends Error {
5
+ expected;
6
+ actual;
7
+ constructor(message, expected, actual, options) {
8
+ super(message, options);
9
+ this.name = this.constructor.name;
10
+ this.expected = expected;
11
+ this.actual = actual;
12
+ Error.captureStackTrace(this, this.constructor);
13
+ }
14
+ }
15
+ exports.AssertError = AssertError;
16
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQXNzZXJ0RXJyb3IuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvZXJyb3IvQXNzZXJ0RXJyb3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUEsTUFBYSxXQUFZLFNBQVEsS0FBSztJQUNwQyxRQUFRLENBQU07SUFDZCxNQUFNLENBQU07SUFFWixZQUFZLE9BQWUsRUFBRSxRQUFhLEVBQUUsTUFBVyxFQUFFLE9BQXNCO1FBQzdFLEtBQUssQ0FBQyxPQUFPLEVBQUUsT0FBTyxDQUFDLENBQUM7UUFDeEIsSUFBSSxDQUFDLElBQUksR0FBRyxJQUFJLENBQUMsV0FBVyxDQUFDLElBQUksQ0FBQztRQUNsQyxJQUFJLENBQUMsUUFBUSxHQUFHLFFBQVEsQ0FBQztRQUN6QixJQUFJLENBQUMsTUFBTSxHQUFHLE1BQU0sQ0FBQztRQUNyQixLQUFLLENBQUMsaUJBQWlCLENBQUMsSUFBSSxFQUFFLElBQUksQ0FBQyxXQUFXLENBQUMsQ0FBQztJQUNsRCxDQUFDO0NBQ0Y7QUFYRCxrQ0FXQyJ9
@@ -0,0 +1,14 @@
1
+ import { Request, RequestOptions } from './request.js';
2
+ import { TestAgent, proxyAgent } from './agent.js';
3
+ import type { App, AgentOptions } from './types.js';
4
+ /**
5
+ * Test against the given `app`,
6
+ * returning a new `Test`.
7
+ */
8
+ export declare function request(app: App, options?: RequestOptions): Request;
9
+ export { Request, RequestOptions, TestAgent, proxyAgent as agent, };
10
+ export * from './test.js';
11
+ declare const _default: ((app: App, options?: RequestOptions) => Request) & {
12
+ agent: (app: App, options?: AgentOptions) => TestAgent;
13
+ };
14
+ export default _default;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.agent = exports.TestAgent = exports.Request = void 0;
18
+ exports.request = request;
19
+ const request_js_1 = require("./request.js");
20
+ Object.defineProperty(exports, "Request", { enumerable: true, get: function () { return request_js_1.Request; } });
21
+ const agent_js_1 = require("./agent.js");
22
+ Object.defineProperty(exports, "TestAgent", { enumerable: true, get: function () { return agent_js_1.TestAgent; } });
23
+ Object.defineProperty(exports, "agent", { enumerable: true, get: function () { return agent_js_1.proxyAgent; } });
24
+ /**
25
+ * Test against the given `app`,
26
+ * returning a new `Test`.
27
+ */
28
+ function request(app, options = {}) {
29
+ return new request_js_1.Request(app, options);
30
+ }
31
+ __exportStar(require("./test.js"), exports);
32
+ // import request from '@eggjs/supertest';
33
+ // request()
34
+ exports.default = new Proxy(request, {
35
+ apply(target, _, argumentsList) {
36
+ return target(argumentsList[0], argumentsList[1]);
37
+ },
38
+ get(target, property, receiver) {
39
+ // import request from '@eggjs/supertest';
40
+ // request.agent()
41
+ if (property === 'agent') {
42
+ return agent_js_1.proxyAgent;
43
+ }
44
+ return Reflect.get(target, property, receiver);
45
+ },
46
+ });
47
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFRQSwwQkFFQztBQVZELDZDQUF1RDtBQWFyRCx3RkFiTyxvQkFBTyxPQWFQO0FBWlQseUNBQW1EO0FBYWpELDBGQWJPLG9CQUFTLE9BYVA7QUFHSyxzRkFoQkkscUJBQVUsT0FnQlQ7QUFickI7OztHQUdHO0FBQ0gsU0FBZ0IsT0FBTyxDQUFDLEdBQVEsRUFBRSxVQUEwQixFQUFFO0lBQzVELE9BQU8sSUFBSSxvQkFBTyxDQUFDLEdBQUcsRUFBRSxPQUFPLENBQUMsQ0FBQztBQUNuQyxDQUFDO0FBVUQsNENBQTBCO0FBRTFCLDBDQUEwQztBQUMxQyxZQUFZO0FBQ1osa0JBQWUsSUFBSSxLQUFLLENBQUMsT0FBTyxFQUFFO0lBQ2hDLEtBQUssQ0FBQyxNQUFNLEVBQUUsQ0FBQyxFQUFFLGFBQWE7UUFDNUIsT0FBTyxNQUFNLENBQUMsYUFBYSxDQUFDLENBQUMsQ0FBQyxFQUFFLGFBQWEsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDO0lBQ3BELENBQUM7SUFDRCxHQUFHLENBQUMsTUFBTSxFQUFFLFFBQVEsRUFBRSxRQUFRO1FBQzVCLDBDQUEwQztRQUMxQyxrQkFBa0I7UUFDbEIsSUFBSSxRQUFRLEtBQUssT0FBTyxFQUFFLENBQUM7WUFDekIsT0FBTyxxQkFBVSxDQUFDO1FBQ3BCLENBQUM7UUFDRCxPQUFPLE9BQU8sQ0FBQyxHQUFHLENBQUMsTUFBTSxFQUFFLFFBQVEsRUFBRSxRQUFRLENBQUMsQ0FBQztJQUNqRCxDQUFDO0NBQ0YsQ0FFQSxDQUFDIn0=
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,20 @@
1
+ import type { Server } from 'node:net';
2
+ import type { App } from './types.js';
3
+ import { Test } from './test.js';
4
+ export interface RequestOptions {
5
+ http2?: boolean;
6
+ }
7
+ export declare class Request {
8
+ #private;
9
+ app: string | Server;
10
+ constructor(appOrListener: App, options?: RequestOptions);
11
+ protected _testRequest(method: string, url: string): Test;
12
+ delete(url: string): Test;
13
+ del(url: string): Test;
14
+ get(url: string): Test;
15
+ head(url: string): Test;
16
+ put(url: string): Test;
17
+ post(url: string): Test;
18
+ patch(url: string): Test;
19
+ options(url: string): Test;
20
+ }
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Request = void 0;
7
+ const node_http_1 = __importDefault(require("node:http"));
8
+ const node_http2_1 = __importDefault(require("node:http2"));
9
+ const test_js_1 = require("./test.js");
10
+ class Request {
11
+ app;
12
+ #http2 = false;
13
+ constructor(appOrListener, options = {}) {
14
+ if (typeof appOrListener === 'function') {
15
+ if (options.http2) {
16
+ this.#http2 = true;
17
+ this.app = node_http2_1.default.createServer(appOrListener); // eslint-disable-line no-param-reassign
18
+ }
19
+ else {
20
+ this.app = node_http_1.default.createServer(appOrListener); // eslint-disable-line no-param-reassign
21
+ }
22
+ }
23
+ else {
24
+ this.app = appOrListener;
25
+ }
26
+ }
27
+ _testRequest(method, url) {
28
+ const req = new test_js_1.Test(this.app, method.toUpperCase(), url);
29
+ if (this.#http2) {
30
+ req.http2();
31
+ }
32
+ return req;
33
+ }
34
+ delete(url) {
35
+ return this._testRequest('delete', url);
36
+ }
37
+ del(url) {
38
+ return this._testRequest('delete', url);
39
+ }
40
+ get(url) {
41
+ return this._testRequest('get', url);
42
+ }
43
+ head(url) {
44
+ return this._testRequest('head', url);
45
+ }
46
+ put(url) {
47
+ return this._testRequest('put', url);
48
+ }
49
+ post(url) {
50
+ return this._testRequest('post', url);
51
+ }
52
+ patch(url) {
53
+ return this._testRequest('patch', url);
54
+ }
55
+ options(url) {
56
+ return this._testRequest('options', url);
57
+ }
58
+ }
59
+ exports.Request = Request;
60
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVxdWVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9yZXF1ZXN0LnRzIl0sIm5hb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