-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathapplication.test.js
More file actions
259 lines (225 loc) · 8.11 KB
/
application.test.js
File metadata and controls
259 lines (225 loc) · 8.11 KB
1
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const assert = require('node:assert');
const fs = require('node:fs');
const path = require('node:path');
const utils = require('../../utils');
describe('test/app/extend/application.test.js', () => {
describe('app.logger', () => {
let app;
before(() => {
app = utils.app('apps/demo');
return app.ready();
});
after(() => app.close());
it('should alias app.logger => app.loggers.logger', () => {
assert(app.logger === app.loggers.logger);
});
it('should alias app.coreLooger => app.loggers.coreLooger', () => {
assert(app.coreLogger === app.loggers.coreLogger);
});
it('should alias app.getLogger(\'coreLogger\') => app.loggers.coreLooger', () => {
assert(app.getLogger('coreLogger') === app.loggers.coreLogger);
});
it('should alias app.getLogger(\'noexist\') => null', () => {
assert(app.getLogger('noexist') === null);
});
});
describe('app.inspect()', () => {
let app;
before(() => {
app = utils.app('apps/demo');
return app.ready();
});
after(() => app.close());
it('should inspect app properties', () => {
assert([
'name', 'baseDir',
'env', 'subdomainOffset',
'controller', 'middlewares', 'serviceClasses',
'config', 'httpclient', 'loggers',
].every(p => Object.prototype.hasOwnProperty.call(app.inspect(), p)));
assert(app.inspect().name === 'demo');
});
});
describe('app.readyCallback()', () => {
let app;
after(() => app.close());
it('should log info when plugin is not ready', async () => {
app = utils.cluster('apps/notready');
// it won't be ready, so wait for the timeout
await utils.sleep(11000);
app.expect('stderr', /\[egg:core:ready_timeout] 10 seconds later a was still unable to finish./);
});
});
describe('app.locals', () => {
let app;
before(() => {
app = utils.app('apps/locals');
return app.ready();
});
after(() => app.close());
it('should app.locals is same ref', () => {
return app.httpRequest()
.get('/app_same_ref')
.expect('true');
});
it('should app.locals not OOM', () => {
return app.httpRequest()
.get('/app_locals_oom')
.expect('ok');
});
});
describe('app.locals.foo = bar', () => {
let app;
before(() => {
app = utils.app('apps/app-locals-getter');
return app.ready();
});
after(() => app.close());
it('should work', () => {
return app.httpRequest()
.get('/test')
.expect({
locals: {
foo: 'bar',
abc: '123',
},
});
});
});
describe('app.createAnonymousContext()', () => {
let app;
before(() => {
app = utils.app('apps/demo');
return app.ready();
});
after(() => app.close());
it('should get anonymous context object', async () => {
const ctx = app.createAnonymousContext({
socket: {
remoteAddress: '10.0.0.1',
},
headers: {
'x-forwarded-for': '10.0.0.1',
},
url: '/foobar?ok=1',
});
assert(ctx.ip === '10.0.0.1');
assert(ctx.url === '/foobar?ok=1');
assert(ctx.socket.remoteAddress === '10.0.0.1');
assert(ctx.socket.remotePort === 7001);
});
});
describe('app.addSingleton()', () => {
let app;
before(() => {
app = utils.app('apps/singleton-demo');
return app.ready();
});
after(() => app.close());
it('should add singleton success', async () => {
let config = await app.dataService.get('first').getConfig();
assert(config.foo === 'bar');
assert(config.foo1 === 'bar1');
const ds = app.dataService.createInstance({ foo: 'barrr' });
config = await ds.getConfig();
assert(config.foo === 'barrr');
const ds2 = await app.dataService.createInstanceAsync({ foo: 'barrr' });
config = await ds2.getConfig();
assert(config.foo === 'barrr');
config = await app.dataServiceAsync.get('first').getConfig();
assert(config.foo === 'bar');
assert(config.foo1 === 'bar1');
try {
app.dataServiceAsync.createInstance({ foo: 'barrr' });
throw new Error('should not execute');
} catch (err) {
assert(err.message === 'egg:singleton dataServiceAsync only support create asynchronous, please use createInstanceAsync');
}
const ds4 = await app.dataServiceAsync.createInstanceAsync({ foo: 'barrr' });
config = await ds4.getConfig();
assert(config.foo === 'barrr');
});
});
describe('app.runInBackground(scope)', () => {
let app;
before(() => {
app = utils.app('apps/ctx-background');
return app.ready();
});
after(() => app.close());
it('should run background task success', async () => {
await app.httpRequest()
.get('/app_background')
.expect(200)
.expect('hello app');
await utils.sleep(2100);
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'ctx-background-web.log'), 'utf8');
assert(/mock background run at app result file size: \d+/.test(log));
assert(/mock background run at app anonymous result file size: \d+/.test(log));
assert(
/\[egg:background] task:.*?app[\/\\]controller[\/\\]app\.js:\d+:\d+ success \([\d\.]+ms\)/.test(fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8'))
);
});
});
describe('app.runInAnonymousContextScope(scope)', () => {
it('should run task in anonymous context scope success', async () => {
const app = utils.app('apps/app-runInAnonymousContextScope');
await app.ready();
await app.close();
await utils.sleep(2100);
const logdir = app.config.logger.dir;
const logs = fs.readFileSync(path.join(logdir, 'app-runInAnonymousContextScope-web.log'), 'utf8').split('\n');
// console.log(logs);
// 2022-12-15 23:00:08,551 INFO 86728 [-/127.0.0.1/-/1ms GET /] before close on ctx logger
// 2022-12-15 23:00:08,551 INFO 86728 [-/127.0.0.1/-/1ms GET /] before close on app logger
// 2022-12-15 23:03:16,086 INFO 89216 outside before close on app logger
assert.match(logs[0], / INFO \d+ \[-\/127.0.0.1\/-\/\d+ms GET \/] inside before close on ctx logger/);
assert.match(logs[1], / INFO \d+ \[-\/127.0.0.1\/-\/\d+ms GET \/] inside before close on app logger/);
assert.match(logs[2], / INFO \d+ outside before close on app logger/);
});
});
describe('app.runInAnonymousContextScope(scope,request)', () => {
it('should run task in anonymous context scope with req success', async () => {
const app = utils.app('apps/app-runInAnonymousContextScope-withRequest');
await app.ready();
await app.close();
await utils.sleep(2100);
const logdir = app.config.logger.dir;
const logs = fs.readFileSync(path.join(logdir, 'app-runInAnonymousContextScope-withRequest-web.log'), { encoding: 'utf8' }).split('\n');
assert.match(logs[0], / INFO \d+ \[-\/127.0.0.2\/-\/\d+ms GET \/] inside before close on ctx logger/);
assert.match(logs[1], / INFO \d+ \[-\/127.0.0.2\/-\/\d+ms GET \/] inside before close on app logger/);
assert.match(logs[2], / INFO \d+ outside before close on app logger/);
});
});
describe('app.handleRequest(ctx, fnMiddleware)', () => {
let app;
before(() => {
app = utils.app('apps/demo');
return app.ready();
});
after(() => app.close());
it('should wait for middleware resolution', async () => {
const ctx = app.createAnonymousContext();
await app.handleRequest(ctx, async ctx => {
await utils.sleep(100);
ctx.body = 'middleware resolution';
});
assert(ctx.body === 'middleware resolution');
});
});
describe('app.keys', () => {
let app;
before(() => {
app = utils.app('apps/demo');
return app.ready();
});
after(() => app.close());
it('should work for app.keys and app.keys=', async () => {
assert.deepEqual(app.keys, [ 'foo' ]);
// `app.keys=` will be ignored
app.keys = undefined;
assert.deepEqual(app.keys, [ 'foo' ]);
});
});
});