@eggjs/supertest 9.0.0-beta.18 → 9.0.0-beta.20

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