-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
44 lines (39 loc) · 964 Bytes
/
utils.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
import Koa from 'koa';
import getPort from 'get-port';
import fetch from 'node-fetch';
import { sessionKey, appId } from './fixtures';
let server;
let urlRoot;
const fakeWechatLoginMiddleware = async (ctx, next) => {
if (ctx.request.path === '/wechat') {
ctx.body = {
session_key: sessionKey,
openid: 'FAKE_OPEN_ID',
};
return;
}
await next();
};
export async function startServer(setup) {
const port = await getPort();
urlRoot = `http://127.0.0.1:${port}`;
const defaults = {
wechatLoginURL: `${urlRoot}/wechat`,
appId,
appSecret: 'fake',
};
const app = new Koa().use(fakeWechatLoginMiddleware);
setup(app, defaults);
server = app.use((ctx) => (ctx.body = { ok: false })).listen(port);
}
export async function stopServer() {
if (server) {
return new Promise((resolve) => {
server.close(resolve);
server = null;
});
}
}
export async function request(path = '', options) {
return fetch(`${urlRoot}${path}`, options);
}