-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
application.test.js
175 lines (152 loc) 路 4.71 KB
/
application.test.js
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
'use strict';
const assert = require('assert');
const sleep = require('mz-modules/sleep');
const fs = require('fs');
const path = require('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', function* () {
app = utils.cluster('apps/notready');
// it won't be ready, so wait for the timeout
yield 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', function* () {
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', function* () {
let config = yield app.dataService.get('first').getConfig();
assert(config.foo === 'bar');
assert(config.foo1 === 'bar1');
const ds = app.dataService.createInstance({ foo: 'barrr' });
config = yield ds.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', function* () {
yield app.httpRequest()
.get('/app_background')
.expect(200)
.expect('hello app');
yield sleep(5000);
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'))
);
});
});
});