@eggjs/supertest 8.2.0 → 8.3.0-beta.11

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.
Files changed (46) hide show
  1. package/README.md +51 -63
  2. package/dist/agent.d.ts +33 -0
  3. package/dist/agent.js +74 -0
  4. package/dist/error/AssertError.d.ts +8 -0
  5. package/dist/error/AssertError.js +15 -0
  6. package/dist/index.d.ts +17 -0
  7. package/dist/index.js +24 -0
  8. package/dist/request.d.ts +25 -0
  9. package/dist/request.js +51 -0
  10. package/dist/test.d.ts +100 -0
  11. package/dist/test.js +237 -0
  12. package/dist/types.d.ts +14 -0
  13. package/package.json +28 -54
  14. package/dist/commonjs/agent.d.ts +0 -28
  15. package/dist/commonjs/agent.js +0 -94
  16. package/dist/commonjs/error/AssertError.d.ts +0 -5
  17. package/dist/commonjs/error/AssertError.js +0 -16
  18. package/dist/commonjs/index.d.ts +0 -14
  19. package/dist/commonjs/index.js +0 -47
  20. package/dist/commonjs/package.json +0 -3
  21. package/dist/commonjs/request.d.ts +0 -21
  22. package/dist/commonjs/request.js +0 -63
  23. package/dist/commonjs/test.d.ts +0 -96
  24. package/dist/commonjs/test.js +0 -326
  25. package/dist/commonjs/types.d.ts +0 -10
  26. package/dist/commonjs/types.js +0 -3
  27. package/dist/esm/agent.d.ts +0 -28
  28. package/dist/esm/agent.js +0 -87
  29. package/dist/esm/error/AssertError.d.ts +0 -5
  30. package/dist/esm/error/AssertError.js +0 -12
  31. package/dist/esm/index.d.ts +0 -14
  32. package/dist/esm/index.js +0 -30
  33. package/dist/esm/package.json +0 -3
  34. package/dist/esm/request.d.ts +0 -21
  35. package/dist/esm/request.js +0 -56
  36. package/dist/esm/test.d.ts +0 -96
  37. package/dist/esm/test.js +0 -322
  38. package/dist/esm/types.d.ts +0 -10
  39. package/dist/esm/types.js +0 -2
  40. package/dist/package.json +0 -4
  41. package/src/agent.ts +0 -96
  42. package/src/error/AssertError.ts +0 -12
  43. package/src/index.ts +0 -39
  44. package/src/request.ts +0 -62
  45. package/src/test.ts +0 -372
  46. package/src/types.ts +0 -17
package/src/index.ts DELETED
@@ -1,39 +0,0 @@
1
- import { Request, RequestOptions } from './request.js';
2
- import { TestAgent, proxyAgent } from './agent.js';
3
- import type { App, AgentOptions } from './types.js';
4
-
5
- /**
6
- * Test against the given `app`,
7
- * returning a new `Test`.
8
- */
9
- export function request(app: App, options: RequestOptions = {}) {
10
- return new Request(app, options);
11
- }
12
-
13
- export {
14
- Request, RequestOptions,
15
- TestAgent,
16
- // import { agent } from '@eggjs/supertest';
17
- // agent()
18
- proxyAgent as agent,
19
- };
20
-
21
- export * from './test.js';
22
-
23
- // import request from '@eggjs/supertest';
24
- // request()
25
- export default new Proxy(request, {
26
- apply(target, _, argumentsList) {
27
- return target(argumentsList[0], argumentsList[1]);
28
- },
29
- get(target, property, receiver) {
30
- // import request from '@eggjs/supertest';
31
- // request.agent()
32
- if (property === 'agent') {
33
- return proxyAgent;
34
- }
35
- return Reflect.get(target, property, receiver);
36
- },
37
- }) as unknown as ((app: App, options?: RequestOptions) => Request) & {
38
- agent: (app: App, options?: AgentOptions) => TestAgent;
39
- };
package/src/request.ts DELETED
@@ -1,62 +0,0 @@
1
- import http from 'node:http';
2
- import http2 from 'node:http2';
3
- import type { Server } from 'node:net';
4
- import type { H1RequestListener, H2RequestListener, App } from './types.js';
5
- import { Test } from './test.js';
6
-
7
- export interface RequestOptions {
8
- http2?: boolean;
9
- }
10
-
11
- export class Request {
12
- app: string | Server;
13
- #http2 = false;
14
-
15
- constructor(appOrListener: App, options: RequestOptions = {}) {
16
- if (typeof appOrListener === 'function') {
17
- if (options.http2) {
18
- this.#http2 = true;
19
- this.app = http2.createServer(appOrListener as H2RequestListener); // eslint-disable-line no-param-reassign
20
- } else {
21
- this.app = http.createServer(appOrListener as H1RequestListener); // eslint-disable-line no-param-reassign
22
- }
23
- } else {
24
- this.app = appOrListener;
25
- }
26
- }
27
-
28
- protected _testRequest(method: string, url: string) {
29
- const req = new Test(this.app, method.toUpperCase(), url);
30
- if (this.#http2) {
31
- req.http2();
32
- }
33
- return req;
34
- }
35
- delete(url: string) {
36
- return this._testRequest('delete', url);
37
- }
38
- del(url: string) {
39
- return this._testRequest('delete', url);
40
- }
41
- get(url: string) {
42
- return this._testRequest('get', url);
43
- }
44
- head(url: string) {
45
- return this._testRequest('head', url);
46
- }
47
- put(url: string) {
48
- return this._testRequest('put', url);
49
- }
50
- post(url: string) {
51
- return this._testRequest('post', url);
52
- }
53
- patch(url: string) {
54
- return this._testRequest('patch', url);
55
- }
56
- options(url: string) {
57
- return this._testRequest('options', url);
58
- }
59
- trace(url: string) {
60
- return this._testRequest('trace', url);
61
- }
62
- }
package/src/test.ts DELETED
@@ -1,372 +0,0 @@
1
- import { inspect } from 'node:util';
2
- import { STATUS_CODES } from 'node:http';
3
- import { Server as HttpsServer } from 'node:tls';
4
- import type { Server, AddressInfo } from 'node:net';
5
- import { deepStrictEqual } from 'node:assert';
6
- import { Request, type Response } from 'superagent';
7
- import { AssertError } from './error/AssertError.js';
8
-
9
- export type TestApplication = Server | string;
10
-
11
- export type AssertFunction = (res: Response) => AssertError | void;
12
- export type CallbackFunction = (err: AssertError | Error | null, res: Response) => void;
13
- export type ResponseError = Error & { syscall?: string; code?: string; status?: number };
14
- export interface ExpectHeader {
15
- name: string;
16
- value: string | number | RegExp;
17
- }
18
-
19
- export class Test extends Request {
20
- app: TestApplication;
21
- _server: Server;
22
- _asserts: AssertFunction[] = [];
23
-
24
- /**
25
- * Initialize a new `Test` with the given `app`,
26
- * request `method` and `path`.
27
- */
28
- constructor(app: TestApplication, method: string, path: string) {
29
- super(method.toUpperCase(), path);
30
-
31
- this.redirects(0);
32
- this.buffer();
33
- this.app = app;
34
- this.url = typeof app === 'string'
35
- ? app + path
36
- : this.serverAddress(app, path);
37
- }
38
-
39
- /**
40
- * Returns a URL, extracted from a server.
41
- *
42
- * @return {String} URL address
43
- * @private
44
- */
45
- protected serverAddress(app: Server, path: string): string {
46
- const addr = app.address();
47
- if (!addr) {
48
- this._server = app.listen(0);
49
- }
50
- const port = (app.address() as AddressInfo).port;
51
- const protocol = (app instanceof HttpsServer || this._server instanceof HttpsServer) ? 'https' : 'http';
52
- return `${protocol}://127.0.0.1:${port}${path}`;
53
- }
54
-
55
- /**
56
- * Expectations:
57
- *
58
- * ```js
59
- * .expect(200)
60
- * .expect(200, fn)
61
- * .expect(200, body)
62
- * .expect('Some body')
63
- * .expect('Some body', fn)
64
- * .expect(['json array body', { key: 'val' }])
65
- * .expect('Content-Type', 'application/json')
66
- * .expect('Content-Type', 'application/json', fn)
67
- * .expect(fn)
68
- * .expect([200, 404])
69
- * ```
70
- *
71
- * @return {Test} The current Test instance for chaining.
72
- */
73
- expect(a: number | string | RegExp | object | AssertFunction, b?: string | number | RegExp | CallbackFunction, c?: CallbackFunction): Test {
74
- // callback
75
- if (typeof a === 'function') {
76
- // .expect(fn)
77
- this._asserts.push(wrapAssertFn(a as AssertFunction));
78
- return this;
79
- }
80
- if (typeof b === 'function') {
81
- // .expect('Some body', fn)
82
- this.end(b);
83
- }
84
- if (typeof c === 'function') {
85
- // .expect('Content-Type', 'application/json', fn)
86
- this.end(c);
87
- }
88
-
89
- // status
90
- if (typeof a === 'number') {
91
- this._asserts.push(wrapAssertFn(this._assertStatus.bind(this, a)));
92
- // body
93
- if (typeof b !== 'function' && arguments.length > 1) {
94
- // .expect(200, 'body')
95
- // .expect(200, null)
96
- // .expect(200, 9999999)
97
- this._asserts.push(wrapAssertFn(this._assertBody.bind(this, b)));
98
- }
99
- return this;
100
- }
101
-
102
- // multiple statuses
103
- if (Array.isArray(a) && a.length > 0 && a.every(val => typeof val === 'number')) {
104
- // .expect([200, 300])
105
- this._asserts.push(wrapAssertFn(this._assertStatusArray.bind(this, a)));
106
- return this;
107
- }
108
-
109
- // header field
110
- if (typeof b === 'string' || typeof b === 'number' || b instanceof RegExp) {
111
- // .expect('Content-Type', 'application/json')
112
- // .expect('Content-Type', /json/)
113
- this._asserts.push(wrapAssertFn(this._assertHeader.bind(this, { name: String(a), value: b })));
114
- return this;
115
- }
116
-
117
- // body
118
- // .expect('body')
119
- // .expect(['json array body', { key: 'val' }])
120
- // .expect(/foo/)
121
- this._asserts.push(wrapAssertFn(this._assertBody.bind(this, a)));
122
-
123
- return this;
124
- }
125
-
126
- /**
127
- * UnExpectations:
128
- *
129
- * .unexpectHeader('Content-Type')
130
- * .unexpectHeader('Content-Type', fn)
131
- */
132
- unexpectHeader(name: string, fn?: CallbackFunction) {
133
- if (typeof fn === 'function') {
134
- this.end(fn);
135
- }
136
-
137
- // header
138
- if (typeof name === 'string') {
139
- this._asserts.push(this._unexpectHeader.bind(this, name));
140
- }
141
- return this;
142
- }
143
-
144
- /**
145
- * Expectations:
146
- *
147
- * .expectHeader('Content-Type')
148
- * .expectHeader('Content-Type', fn)
149
- */
150
- expectHeader(name: string, fn?: CallbackFunction) {
151
- if (typeof fn === 'function') {
152
- this.end(fn);
153
- }
154
-
155
- // header
156
- if (typeof name === 'string') {
157
- this._asserts.push(this._expectHeader.bind(this, name));
158
- }
159
- return this;
160
- }
161
-
162
- _unexpectHeader(name: string, res: Response) {
163
- const actual = res.headers[name.toLowerCase()];
164
- if (actual) {
165
- return new AssertError('unexpected "' + name + '" header field, got "' + actual + '"',
166
- name, actual,
167
- );
168
- }
169
- }
170
-
171
- _expectHeader(name: string, res: Response) {
172
- const actual = res.headers[name.toLowerCase()];
173
- if (!actual) {
174
- return new AssertError('expected "' + name + '" header field', name, actual);
175
- }
176
- }
177
-
178
- /**
179
- * Defer invoking superagent's `.end()` until
180
- * the server is listening.
181
- */
182
- end(fn: CallbackFunction) {
183
- const server = this._server;
184
-
185
- super.end((err, res) => {
186
- const localAssert = () => {
187
- this.assert(err, res, fn);
188
- };
189
-
190
- if (server && '_handle' in server && server._handle) {
191
- return server.close(localAssert);
192
- }
193
-
194
- localAssert();
195
- });
196
-
197
- return this;
198
- }
199
-
200
- /**
201
- * Perform assertions and invoke `fn(err, res)`.
202
- */
203
- assert(resError: ResponseError | null, res: Response, fn: CallbackFunction) {
204
- let errorObj: Error | undefined;
205
-
206
- // check for unexpected network errors or server not running/reachable errors
207
- // when there is no response and superagent sends back a System Error
208
- // do not check further for other asserts, if any, in such case
209
- // https://nodejs.org/api/errors.html#errors_common_system_errors
210
- const sysErrors: Record<string, string> = {
211
- ECONNREFUSED: 'Connection refused',
212
- ECONNRESET: 'Connection reset by peer',
213
- EPIPE: 'Broken pipe',
214
- ETIMEDOUT: 'Operation timed out',
215
- };
216
-
217
- if (!res && resError) {
218
- if (resError instanceof Error
219
- && resError.syscall === 'connect'
220
- && resError.code
221
- && sysErrors[resError.code]) {
222
- errorObj = new Error(resError.code + ': ' + sysErrors[resError.code]);
223
- } else {
224
- errorObj = resError;
225
- }
226
- }
227
-
228
- // asserts
229
- for (let i = 0; i < this._asserts.length && !errorObj; i += 1) {
230
- errorObj = this._assertFunction(this._asserts[i], res);
231
- }
232
-
233
- // set unexpected superagent error if no other error has occurred.
234
- if (!errorObj && resError instanceof Error && (!res || resError.status !== res.status)) {
235
- errorObj = resError;
236
- }
237
-
238
- fn.call(this, errorObj || null, res);
239
- }
240
-
241
- /**
242
- * Perform assertions on a response body and return an Error upon failure.
243
- */
244
- _assertBody(body: RegExp | string | number | object | null | undefined, res: Response) {
245
- const isRegexp = body instanceof RegExp;
246
-
247
- // parsed
248
- if (typeof body === 'object' && !isRegexp) {
249
- try {
250
- deepStrictEqual(body, res.body);
251
- } catch (err) {
252
- const a = inspect(body);
253
- const b = inspect(res.body);
254
- return new AssertError('expected ' + a + ' response body, got ' + b, body, res.body, { cause: err });
255
- }
256
- } else if (body !== res.text) {
257
- // string
258
- const a = inspect(body);
259
- const b = inspect(res.text);
260
-
261
- // regexp
262
- if (isRegexp) {
263
- if (!body.test(res.text)) {
264
- return new AssertError('expected body ' + b + ' to match ' + body, body, res.body);
265
- }
266
- } else {
267
- return new AssertError('expected ' + a + ' response body, got ' + b, body, res.body);
268
- }
269
- }
270
- }
271
-
272
- /**
273
- * Perform assertions on a response header and return an Error upon failure.
274
- */
275
- _assertHeader(header: ExpectHeader, res: Response) {
276
- const field = header.name;
277
- const actual = res.header[field.toLowerCase()];
278
- const fieldExpected = header.value;
279
-
280
- if (typeof actual === 'undefined') {
281
- return new AssertError('expected "' + field + '" header field', header, actual);
282
- }
283
- // This check handles header values that may be a String or single element Array
284
- if ((Array.isArray(actual) && actual.toString() === fieldExpected)
285
- || fieldExpected === actual) {
286
- return;
287
- }
288
- if (fieldExpected instanceof RegExp) {
289
- if (!fieldExpected.test(actual)) {
290
- return new AssertError('expected "' + field + '" matching '
291
- + fieldExpected + ', got "' + actual + '"', header, actual);
292
- }
293
- } else {
294
- return new AssertError('expected "' + field + '" of "' + fieldExpected + '", got "' + actual + '"',
295
- header, actual,
296
- );
297
- }
298
- }
299
-
300
- /**
301
- * Perform assertions on the response status and return an Error upon failure.
302
- */
303
- _assertStatus(status: number, res: Response) {
304
- if (res.status !== status) {
305
- const a = STATUS_CODES[status];
306
- const b = STATUS_CODES[res.status];
307
- return new AssertError('expected ' + status + ' "' + a + '", got ' + res.status + ' "' + b + '"',
308
- status, res.status,
309
- );
310
- }
311
- }
312
-
313
- /**
314
- * Perform assertions on the response status and return an Error upon failure.
315
- */
316
- _assertStatusArray(statusArray: number[], res: Response) {
317
- if (!statusArray.includes(res.status)) {
318
- const b = STATUS_CODES[res.status];
319
- const expectedList = statusArray.join(', ');
320
- return new AssertError(
321
- 'expected one of "' + expectedList + '", got ' + res.status + ' "' + b + '"',
322
- statusArray, res.status,
323
- );
324
- }
325
- }
326
-
327
- /**
328
- * Performs an assertion by calling a function and return an Error upon failure.
329
- */
330
- _assertFunction(fn: AssertFunction, res: Response) {
331
- let err;
332
- try {
333
- err = fn(res);
334
- } catch (e) {
335
- err = e;
336
- }
337
- if (err instanceof Error) {
338
- return err;
339
- }
340
- }
341
- }
342
-
343
- /**
344
- * Wraps an assert function into another.
345
- * The wrapper function edit the stack trace of any assertion error, prepending a more useful stack to it.
346
- *
347
- * @param {Function} assertFn
348
- * @return {Function} wrapped assert function
349
- */
350
-
351
- function wrapAssertFn(assertFn: AssertFunction) {
352
- const savedStack = new Error().stack!.split('\n').slice(3);
353
-
354
- return (res: Response) => {
355
- let badStack;
356
- let err;
357
- try {
358
- err = assertFn(res);
359
- } catch (e: any) {
360
- err = e;
361
- }
362
- if (err instanceof Error && err.stack) {
363
- badStack = err.stack.replace(err.message, '').split('\n').slice(1);
364
- err.stack = [ err.toString() ]
365
- .concat(savedStack)
366
- .concat('----')
367
- .concat(badStack)
368
- .join('\n');
369
- }
370
- return err;
371
- };
372
- }
package/src/types.ts DELETED
@@ -1,17 +0,0 @@
1
- import type { RequestListener } from 'node:http';
2
- import type { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
3
- import type { Server } from 'node:net';
4
- import type { AgentOptions as SAgentOptions } from 'superagent';
5
-
6
- export type H2RequestListener = (request: Http2ServerRequest, response: Http2ServerResponse) => void;
7
- export type H1RequestListener = RequestListener;
8
-
9
- export type App =
10
- | Server
11
- | H1RequestListener
12
- | H2RequestListener
13
- | string;
14
-
15
- export interface AgentOptions extends SAgentOptions {
16
- http2?: boolean;
17
- }