-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.test.js
204 lines (174 loc) · 5.92 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
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
'use strict';
const assert = require('assert');
const mm = require('egg-mock');
const sleep = require('mz-modules/sleep');
const fs = require('fs');
const path = require('path');
const Application = require('../../lib/application');
const utils = require('../utils');
describe('test/lib/application.test.js', () => {
let app;
afterEach(mm.restore);
describe('create application', () => {
it('should throw options.baseDir required', () => {
assert.throws(() => {
new Application({
baseDir: 1,
});
}, /options.baseDir required, and must be a string/);
});
it('should throw options.baseDir not exist', () => {
assert.throws(() => {
new Application({
baseDir: 'not-exist',
});
}, /Directory not-exist not exists/);
});
it('should throw options.baseDir is not a directory', () => {
assert.throws(() => {
new Application({
baseDir: __filename,
});
}, /is not a directory/);
});
});
describe('app start timeout', function() {
afterEach(() => app.close());
it('should emit `startTimeout` event', function(done) {
app = utils.app('apps/app-start-timeout');
app.once('startTimeout', done);
});
});
describe('app.keys', () => {
it('should throw when config.keys missing on non-local and non-unittest env', function* () {
mm.env('test');
app = utils.app('apps/keys-missing');
yield app.ready();
mm(app.config, 'keys', null);
try {
app.keys;
throw new Error('should not run this');
} catch (err) {
assert(err.message === 'Please set config.keys first');
}
// make sure app close
yield app.close();
});
it('should throw when config.keys missing on unittest env', function* () {
mm.env('unittest');
app = utils.app('apps/keys-missing');
yield app.ready();
mm(app.config, 'keys', null);
try {
app.keys;
throw new Error('should not run this');
} catch (err) {
assert(err.message === 'Please set config.keys first');
}
// make sure app close
yield app.close();
});
it('should throw when config.keys missing on local env', function* () {
mm.env('local');
app = utils.app('apps/keys-missing');
yield app.ready();
mm(app.config, 'keys', null);
try {
app.keys;
throw new Error('should not run this');
} catch (err) {
assert(err.message === 'Please set config.keys first');
}
// make sure app close
yield app.close();
});
it('should use exists keys', function* () {
mm.env('unittest');
app = utils.app('apps/keys-exists');
yield app.ready();
assert(app.keys);
assert(app.keys);
assert(app.config.keys === 'my keys');
yield app.close();
});
});
describe('handle uncaughtException', () => {
let app;
before(() => {
app = utils.cluster('apps/app-throw');
return app.ready();
});
after(() => app.close());
it('should handle uncaughtException and log it', function* () {
yield app.httpRequest()
.get('/throw')
.expect('foo')
.expect(200);
yield sleep(1100);
const logfile = path.join(utils.getFilepath('apps/app-throw'), 'logs/app-throw/common-error.log');
const body = fs.readFileSync(logfile, 'utf8');
assert(body.includes('ReferenceError: a is not defined (uncaughtException throw'));
});
});
describe('warn confused configurations', () => {
it('should warn if confused configurations exist', function* () {
const app = utils.app('apps/confused-configuration');
yield app.ready();
yield sleep(1000);
const logs = fs.readFileSync(utils.getFilepath('apps/confused-configuration/logs/confused-configuration/confused-configuration-web.log'), 'utf8');
assert(logs.match(/Unexpected config key `bodyparser` exists, Please use `bodyParser` instead\./));
assert(logs.match(/Unexpected config key `notFound` exists, Please use `notfound` instead\./));
assert(logs.match(/Unexpected config key `sitefile` exists, Please use `siteFile` instead\./));
assert(logs.match(/Unexpected config key `middlewares` exists, Please use `middleware` instead\./));
assert(logs.match(/Unexpected config key `httpClient` exists, Please use `httpclient` instead\./));
});
});
describe('test on apps/demo', () => {
let app;
before(() => {
app = utils.app('apps/demo');
return app.ready();
});
after(() => app.close());
describe('application.deprecate', () => {
it('should get deprecate with namespace egg', function* () {
const deprecate = app.deprecate;
assert(deprecate._namespace === 'egg');
assert(deprecate === app.deprecate);
});
});
describe('curl()', () => {
it('should curl success', function* () {
const localServer = yield utils.startLocalServer();
const res = yield app.curl(`${localServer}/foo/app`);
assert(res.status === 200);
});
});
describe('env', () => {
it('should return app.config.env', function* () {
assert(app.env === app.config.env);
});
});
describe('proxy', () => {
it('should delegate app.config.proxy', function* () {
assert(app.proxy === app.config.proxy);
});
});
describe('inspect && toJSON', () => {
it('should override koa method', function() {
const inspectResult = app.inspect();
const jsonResult = app.toJSON();
assert.deepEqual(inspectResult, jsonResult);
assert(inspectResult.env === app.config.env);
});
});
describe('class style controller', () => {
it('should work with class style controller', () => {
return app.httpRequest()
.get('/class-controller')
.expect('this is bar!')
.expect(200);
});
});
});
});