-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
121 lines (106 loc) · 2.87 KB
/
Copy pathutils.js
File metadata and controls
121 lines (106 loc) · 2.87 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
'use strict';
const fs = require('fs');
const path = require('path');
const mm = require('egg-mock');
const sleep = require('mz-modules/sleep');
const Koa = require('koa');
const http = require('http');
const fixtures = path.join(__dirname, 'fixtures');
const eggPath = path.join(__dirname, '..');
const egg = require('..');
const request = require('supertest');
exports.app = (name, options) => {
options = formatOptions(name, options);
const app = mm.app(options);
return app;
};
/**
* start app with single process mode
*
* @param {String} baseDir - base dir.
* @param {Object} [options] - optional
* @return {App} app - Application object.
*/
exports.singleProcessApp = async (baseDir, options = {}) => {
if (!baseDir.startsWith('/')) baseDir = path.join(__dirname, 'fixtures', baseDir);
options.env = options.env || 'unittest';
options.baseDir = baseDir;
const app = await egg.start(options);
app.httpRequest = () => request(app.callback());
return app;
};
/**
* start app with cluster mode
*
* @param {String} name - cluster name.
* @param {Object} [options] - optional
* @return {App} app - Application object.
*/
exports.cluster = (name, options) => {
options = formatOptions(name, options);
return mm.cluster(options);
};
let localServer;
exports.startLocalServer = () => {
return new Promise((resolve, reject) => {
if (localServer) {
return resolve('http://127.0.0.1:' + localServer.address().port);
}
let retry = false;
const app = new Koa();
app.use(async ctx => {
if (ctx.path === '/get_headers') {
ctx.body = ctx.request.headers;
return;
}
if (ctx.path === '/timeout') {
await sleep(10000);
ctx.body = `${ctx.method} ${ctx.path}`;
return;
}
if (ctx.path === '/error') {
ctx.status = 500;
ctx.body = 'this is an error';
return;
}
if (ctx.path === '/retry') {
if (!retry) {
retry = true;
ctx.status = 500;
} else {
ctx.set('x-retry', '1');
ctx.body = 'retry suc';
retry = false;
}
return;
}
ctx.body = `${ctx.method} ${ctx.path}`;
});
localServer = http.createServer(app.callback());
localServer.listen(0, err => {
if (err) return reject(err);
return resolve('http://127.0.0.1:' + localServer.address().port);
});
});
};
process.once('exit', () => localServer && localServer.close());
exports.getFilepath = name => {
return path.join(fixtures, name);
};
exports.getJSON = name => {
return JSON.parse(fs.readFileSync(exports.getFilepath(name)));
};
function formatOptions(name, options) {
let baseDir;
if (typeof name === 'string') {
baseDir = name;
} else {
// name is options
options = name;
}
return Object.assign({}, {
baseDir,
customEgg: eggPath,
cache: false,
}, options);
}