@eggjs/mock 7.0.0-beta.34 → 7.0.0-beta.36
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/_virtual/rolldown_runtime.js +7 -0
- package/dist/app/extend/agent.d.ts +38 -34
- package/dist/app/extend/agent.js +48 -46
- package/dist/app/extend/application.d.ts +171 -167
- package/dist/app/extend/application.js +382 -442
- package/dist/app/middleware/cluster_app_mock.d.ts +5 -2
- package/dist/app/middleware/cluster_app_mock.js +95 -100
- package/dist/app.d.ts +9 -5
- package/dist/app.js +17 -18
- package/dist/bootstrap.d.ts +9 -5
- package/dist/bootstrap.js +11 -12
- package/dist/index.d.ts +20 -17
- package/dist/index.js +39 -78
- package/dist/inject_mocha.d.ts +1 -1
- package/dist/inject_mocha.js +27 -28
- package/dist/lib/agent_handler.d.ts +7 -3
- package/dist/lib/agent_handler.js +22 -20
- package/dist/lib/app.d.ts +7 -3
- package/dist/lib/app.js +246 -287
- package/dist/lib/app_handler.d.ts +9 -5
- package/dist/lib/app_handler.js +48 -61
- package/dist/lib/cluster.d.ts +122 -118
- package/dist/lib/cluster.js +278 -335
- package/dist/lib/context.d.ts +4 -1
- package/dist/lib/context.js +10 -11
- package/dist/lib/format_options.d.ts +9 -4
- package/dist/lib/format_options.js +60 -88
- package/dist/lib/inject_context.d.ts +8 -5
- package/dist/lib/inject_context.js +103 -123
- package/dist/lib/mock_agent.d.ts +8 -4
- package/dist/lib/mock_agent.js +42 -42
- package/dist/lib/mock_custom_loader.d.ts +4 -1
- package/dist/lib/mock_custom_loader.js +34 -33
- package/dist/lib/mock_http_server.d.ts +6 -2
- package/dist/lib/mock_http_server.js +16 -17
- package/dist/lib/mock_httpclient.d.ts +38 -34
- package/dist/lib/mock_httpclient.js +106 -142
- package/dist/lib/parallel/agent.d.ts +22 -18
- package/dist/lib/parallel/agent.js +106 -112
- package/dist/lib/parallel/app.d.ts +23 -19
- package/dist/lib/parallel/app.js +93 -106
- package/dist/lib/parallel/util.d.ts +5 -2
- package/dist/lib/parallel/util.js +56 -63
- package/dist/lib/prerequire.d.ts +1 -1
- package/dist/lib/prerequire.js +1 -25
- package/dist/lib/request_call_function.d.ts +1 -1
- package/dist/lib/request_call_function.js +34 -47
- package/dist/lib/restore.d.ts +4 -1
- package/dist/lib/restore.js +14 -11
- package/dist/lib/start-cluster.d.ts +1 -2
- package/dist/lib/start-cluster.js +15 -14
- package/dist/lib/supertest.d.ts +14 -9
- package/dist/lib/supertest.js +32 -36
- package/dist/lib/tmp/empty.d.ts +1 -1
- package/dist/lib/tmp/empty.js +1 -2
- package/dist/lib/types.d.ts +71 -67
- package/dist/lib/types.js +1 -2
- package/dist/lib/utils.d.ts +11 -8
- package/dist/lib/utils.js +34 -63
- package/dist/register.d.ts +11 -7
- package/dist/register.js +36 -40
- package/dist/typings/index.d.ts +1 -0
- package/package.json +36 -39
|
@@ -1,34 +1,35 @@
|
|
|
1
|
-
import { debuglog } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
1
|
+
import { debuglog } from "node:util";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/mock_custom_loader.ts
|
|
4
|
+
const debug = debuglog("egg/mock/lib/mock_custom_loader");
|
|
5
|
+
function setCustomLoader(app) {
|
|
6
|
+
const customLoader = app.config.customLoader;
|
|
7
|
+
if (!customLoader) return;
|
|
8
|
+
for (const field of Object.keys(customLoader)) {
|
|
9
|
+
const loaderConfig = Object.assign({}, customLoader[field]);
|
|
10
|
+
loaderConfig.field = field;
|
|
11
|
+
addMethod(loaderConfig);
|
|
12
|
+
}
|
|
13
|
+
function addMethod(loaderConfig) {
|
|
14
|
+
const field = loaderConfig.field;
|
|
15
|
+
const appMethodName = "mock" + field.replace(/^[a-z]/i, (s) => s.toUpperCase());
|
|
16
|
+
if (app[appMethodName]) {
|
|
17
|
+
app.coreLogger.warn("Can't override app.%s", appMethodName);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
debug("[addMethod] %s => %j", appMethodName, loaderConfig);
|
|
21
|
+
app[appMethodName] = function(service, methodName, fn) {
|
|
22
|
+
if (typeof service === "string") {
|
|
23
|
+
const arr = service.split(".");
|
|
24
|
+
service = loaderConfig.inject === "ctx" ? this[field + "Classes"] : this[field];
|
|
25
|
+
for (const key of arr) service = service[key];
|
|
26
|
+
service = service.prototype || service;
|
|
27
|
+
}
|
|
28
|
+
this._mockFn(service, methodName, fn);
|
|
29
|
+
return this;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
33
32
|
}
|
|
34
|
-
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { setCustomLoader };
|
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
import { Server } from
|
|
2
|
-
|
|
1
|
+
import { Server } from "node:http";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/mock_http_server.d.ts
|
|
4
|
+
declare function createServer(app: any): Server;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { createServer };
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import http, { Server } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
app.emit('server', server);
|
|
15
|
-
}
|
|
16
|
-
return server;
|
|
1
|
+
import http, { Server } from "node:http";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/mock_http_server.ts
|
|
4
|
+
const SERVER = Symbol("http_server");
|
|
5
|
+
function createServer(app) {
|
|
6
|
+
let server = app[SERVER] || app.callback();
|
|
7
|
+
if (typeof server === "function") {
|
|
8
|
+
server = http.createServer(server);
|
|
9
|
+
app[SERVER] = server;
|
|
10
|
+
if (!app.server) app.server = server;
|
|
11
|
+
app.emit("server", server);
|
|
12
|
+
}
|
|
13
|
+
return server;
|
|
17
14
|
}
|
|
18
|
-
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { createServer };
|
|
@@ -1,36 +1,40 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
import { BodyInit, Dispatcher, Headers } from "urllib";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/mock_httpclient.d.ts
|
|
4
|
+
interface MockResultOptions {
|
|
5
|
+
data: string | Buffer | Record<string, any>;
|
|
6
|
+
/**
|
|
7
|
+
* http status
|
|
8
|
+
*/
|
|
9
|
+
status?: number;
|
|
10
|
+
/**
|
|
11
|
+
* response header
|
|
12
|
+
*/
|
|
13
|
+
headers?: Record<string, string>;
|
|
14
|
+
/**
|
|
15
|
+
* delay the associated reply by a set amount in ms
|
|
16
|
+
*/
|
|
17
|
+
delay?: number;
|
|
18
|
+
/**
|
|
19
|
+
* any matching request will always reply with the defined response indefinitely
|
|
20
|
+
*/
|
|
21
|
+
persist?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* any matching request will reply with the defined response a fixed amount of times
|
|
24
|
+
*/
|
|
25
|
+
repeats?: number;
|
|
24
26
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
type ResultObject = MockResultOptions;
|
|
28
|
+
interface MockResponseCallbackOptions {
|
|
29
|
+
path: string;
|
|
30
|
+
method: string;
|
|
31
|
+
headers?: Headers | Record<string, string>;
|
|
32
|
+
origin?: string;
|
|
33
|
+
body?: BodyInit | Dispatcher.DispatchOptions["body"] | null;
|
|
34
|
+
maxRedirections?: number;
|
|
33
35
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
type MockResultFunction = (url: string, options: MockResponseCallbackOptions) => MockResultOptions | string;
|
|
37
|
+
type MockHttpClientMethod = (mockUrl: string | RegExp, mockMethod: string | string[] | MockResultOptions | MockResultFunction, mockResult?: MockResultOptions | MockResultFunction | string) => void;
|
|
38
|
+
declare function createMockHttpClient(app: any): MockHttpClientMethod;
|
|
39
|
+
//#endregion
|
|
40
|
+
export { MockHttpClientMethod, MockResponseCallbackOptions, MockResultFunction, MockResultOptions, ResultObject, createMockHttpClient };
|
|
@@ -1,146 +1,110 @@
|
|
|
1
|
-
import { mm } from 'mm';
|
|
2
|
-
import { extend } from '@eggjs/extend2';
|
|
3
1
|
import { getMockAgent } from "./mock_agent.js";
|
|
2
|
+
import { mm } from "mm";
|
|
3
|
+
import { extend } from "@eggjs/extend2";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/mock_httpclient.ts
|
|
4
6
|
function normalizeResult(result) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// do nothing
|
|
14
|
-
}
|
|
15
|
-
else if (typeof result.data === 'object') {
|
|
16
|
-
// json
|
|
17
|
-
result.data = Buffer.from(JSON.stringify(result.data));
|
|
18
|
-
}
|
|
19
|
-
else if (typeof result.data === 'string') {
|
|
20
|
-
// string
|
|
21
|
-
result.data = Buffer.from(result.data);
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
throw new Error('`mockResult.data` must be buffer, string or json');
|
|
25
|
-
}
|
|
26
|
-
result.headers = result.headers ?? {};
|
|
27
|
-
return result;
|
|
7
|
+
if (typeof result === "string") result = { data: result };
|
|
8
|
+
if (!result.status) result.status = 200;
|
|
9
|
+
result.data = result.data || "";
|
|
10
|
+
if (Buffer.isBuffer(result.data)) {} else if (typeof result.data === "object") result.data = Buffer.from(JSON.stringify(result.data));
|
|
11
|
+
else if (typeof result.data === "string") result.data = Buffer.from(result.data);
|
|
12
|
+
else throw new Error("`mockResult.data` must be buffer, string or json");
|
|
13
|
+
result.headers = result.headers ?? {};
|
|
14
|
+
return result;
|
|
28
15
|
}
|
|
29
|
-
const MOCK_CONFIGS = Symbol(
|
|
30
|
-
const MOCK_CONFIG_INDEX = Symbol(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
else {
|
|
121
|
-
mockRequestResult = typeof mockResult === 'function' ? mockResult(requestUrl, options) : mockResult;
|
|
122
|
-
}
|
|
123
|
-
const result = extend(true, {}, normalizeResult(mockRequestResult));
|
|
124
|
-
return {
|
|
125
|
-
statusCode: result.status,
|
|
126
|
-
data: result.data,
|
|
127
|
-
responseOptions: {
|
|
128
|
-
headers: result.headers,
|
|
129
|
-
},
|
|
130
|
-
};
|
|
131
|
-
});
|
|
132
|
-
if (typeof mockResult === 'object') {
|
|
133
|
-
if (mockResult.delay && mockResult.delay > 0) {
|
|
134
|
-
mockScope.delay(mockResult.delay);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
if (persist) {
|
|
138
|
-
mockScope.persist();
|
|
139
|
-
}
|
|
140
|
-
else if (typeof mockResult === 'object' && mockResult.repeats && mockResult.repeats > 0) {
|
|
141
|
-
mockScope.times(mockResult.repeats);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
};
|
|
16
|
+
const MOCK_CONFIGS = Symbol("MOCK_CONFIGS");
|
|
17
|
+
const MOCK_CONFIG_INDEX = Symbol("MOCK_CONFIG_INDEX");
|
|
18
|
+
function createMockHttpClient(app) {
|
|
19
|
+
/**
|
|
20
|
+
* mock httpclient
|
|
21
|
+
* @function mockHttpclient
|
|
22
|
+
* @param {String} mockUrl - url
|
|
23
|
+
* @param {String|Array} mockMethod - http method, default is '*'
|
|
24
|
+
* @param {Object|Function} mockResult - you data
|
|
25
|
+
* - data - buffer / string / json
|
|
26
|
+
* - status - http status
|
|
27
|
+
* - headers - response header
|
|
28
|
+
* - delay - delay the associated reply by a set amount in ms.
|
|
29
|
+
* - persist - any matching request will always reply with the defined response indefinitely, default is true
|
|
30
|
+
* - repeats - number, any matching request will reply with the defined response a fixed amount of times
|
|
31
|
+
*/
|
|
32
|
+
return function mockHttpClient(mockUrl, mockMethod, mockResult) {
|
|
33
|
+
let mockMethods = mockMethod;
|
|
34
|
+
if (!mockResult) {
|
|
35
|
+
mockResult = mockMethod;
|
|
36
|
+
mockMethods = ["*"];
|
|
37
|
+
}
|
|
38
|
+
if (!Array.isArray(mockMethods)) mockMethods = [mockMethods];
|
|
39
|
+
mockMethods = mockMethods.map((method) => (method || "GET").toUpperCase());
|
|
40
|
+
let mockConfigs = app[MOCK_CONFIGS];
|
|
41
|
+
if (!mockConfigs) {
|
|
42
|
+
mockConfigs = [];
|
|
43
|
+
mm(app, MOCK_CONFIGS, mockConfigs);
|
|
44
|
+
}
|
|
45
|
+
let mockConfigIndex = -1;
|
|
46
|
+
let origin = mockUrl;
|
|
47
|
+
let originMethod;
|
|
48
|
+
const pathname = mockUrl;
|
|
49
|
+
let pathMethod;
|
|
50
|
+
if (typeof mockUrl === "string") {
|
|
51
|
+
const urlObject = new URL(mockUrl);
|
|
52
|
+
origin = urlObject.origin;
|
|
53
|
+
const originalPathname = urlObject.pathname;
|
|
54
|
+
pathMethod = (path) => {
|
|
55
|
+
if (path === originalPathname) return true;
|
|
56
|
+
if (path.includes("?")) return path.startsWith(originalPathname);
|
|
57
|
+
return false;
|
|
58
|
+
};
|
|
59
|
+
} else if (mockUrl instanceof RegExp) {
|
|
60
|
+
let requestOrigin = "";
|
|
61
|
+
originMethod = (value) => {
|
|
62
|
+
requestOrigin = value;
|
|
63
|
+
return true;
|
|
64
|
+
};
|
|
65
|
+
pathMethod = (path) => {
|
|
66
|
+
for (const config of mockConfigs) if (config.mockUrl.test(`${requestOrigin}${path}`)) {
|
|
67
|
+
mm(app, MOCK_CONFIG_INDEX, config.mockConfigIndex);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
};
|
|
72
|
+
mockConfigIndex = mockConfigs.length;
|
|
73
|
+
mockConfigs.push({
|
|
74
|
+
mockUrl,
|
|
75
|
+
mockResult,
|
|
76
|
+
mockConfigIndex
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
const mockPool = originMethod ? getMockAgent(app).get(originMethod) : getMockAgent(app).get(originMethod ?? origin);
|
|
80
|
+
let persist = true;
|
|
81
|
+
if (typeof mockResult === "object" && typeof mockResult.persist === "boolean") persist = mockResult.persist;
|
|
82
|
+
mockMethods.forEach(function(method) {
|
|
83
|
+
const mockScope = mockPool.intercept({
|
|
84
|
+
path: pathMethod ?? pathname,
|
|
85
|
+
method: method === "*" ? () => true : method
|
|
86
|
+
}).reply((options) => {
|
|
87
|
+
const requestUrl = `${options.origin}${options.path}`;
|
|
88
|
+
let mockRequestResult;
|
|
89
|
+
if (mockConfigIndex >= 0) {
|
|
90
|
+
mockResult = mockConfigs[app[MOCK_CONFIG_INDEX]].mockResult;
|
|
91
|
+
mockRequestResult = typeof mockResult === "function" ? mockResult(requestUrl, options) : mockResult;
|
|
92
|
+
} else mockRequestResult = typeof mockResult === "function" ? mockResult(requestUrl, options) : mockResult;
|
|
93
|
+
const result = extend(true, {}, normalizeResult(mockRequestResult));
|
|
94
|
+
return {
|
|
95
|
+
statusCode: result.status,
|
|
96
|
+
data: result.data,
|
|
97
|
+
responseOptions: { headers: result.headers }
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
if (typeof mockResult === "object") {
|
|
101
|
+
if (mockResult.delay && mockResult.delay > 0) mockScope.delay(mockResult.delay);
|
|
102
|
+
}
|
|
103
|
+
if (persist) mockScope.persist();
|
|
104
|
+
else if (typeof mockResult === "object" && mockResult.repeats && mockResult.repeats > 0) mockScope.times(mockResult.repeats);
|
|
105
|
+
});
|
|
106
|
+
};
|
|
145
107
|
}
|
|
146
|
-
|
|
108
|
+
|
|
109
|
+
//#endregion
|
|
110
|
+
export { createMockHttpClient };
|
|
@@ -1,19 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import { MockApplicationOptions, MockOptions } from "../types.js";
|
|
2
|
+
import { Agent } from "egg";
|
|
3
|
+
import { Base } from "sdk-base";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/parallel/agent.d.ts
|
|
6
|
+
declare class MockAgent extends Base {
|
|
7
|
+
#private;
|
|
8
|
+
options: MockApplicationOptions;
|
|
9
|
+
baseDir: string;
|
|
10
|
+
__APP_INIT__: boolean;
|
|
11
|
+
_instance: Agent;
|
|
12
|
+
constructor(options: MockApplicationOptions);
|
|
13
|
+
_init(): Promise<void>;
|
|
14
|
+
on(...args: any[]): this;
|
|
15
|
+
once(...args: any[]): this;
|
|
16
|
+
/**
|
|
17
|
+
* close agent
|
|
18
|
+
*/
|
|
19
|
+
_close(): Promise<void>;
|
|
18
20
|
}
|
|
19
|
-
|
|
21
|
+
declare function createAgent(options: MockOptions): MockAgent;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { MockAgent, createAgent };
|