import s from "apigo.cc/gojs/service" import http from "apigo.cc/gojs/http" import u from "apigo.cc/gojs/util" import co from "apigo.cc/gojs/console" let h2c = http let urlPrefix function main() { s.config({ app: 'user', accessTokens: { 'testToken1122': 2 }, calls: { 'user': 'testToken1122' }, cpuMonitor: true, memoryMonitor: true, sessionKey: 'SessionID', userIdKey: 'id', limitedMessage: { code: 429, message: "访问过于频繁" }, authFieldMessage: { code: 403, message: "身份验证失败 [{{USER_AUTHLEVEL}}/{{TARGET_AUTHLEVEL}}]" }, verifyFieldMessage: { code: 400, message: "参数 [{{FAILED_FIELDS}} 验证失败" }, limiters: { ip1s: { from: 'ip', time: 100, times: 10 } }, static: { '/': 'api/', }, rewrite: { '/echo2.js': '/echo.js' } }) s.register({ path: '/echo', noLog200: true }, ({ args, response }) => { // setTimeout(() => { // response.end(args.name) // }, 1) u.sleep(1) return args.name }) s.load('api/echo.js', { min: 20, max: 1000, idle: 100 }) s.load('api/user.js') let host = s.start() h2c = http.newH2C({ baseURL: 'http://' + host }) return host } function test(name) { let r = h2c.get('/echo?name=' + name) return r.string() } function testStatic() { let r = h2c.get('/echo2.js') if (r.string().indexOf('/echo2') === -1) return r.string() return true } function test2(name) { let r = h2c.get('/echo2?name=' + name) return r.string() } function testUser() { let r = h2c.get('/userInfo') if (r.statusCode !== 403 || r.object().code !== 403) return r r = h2c.post('/login', { id: 'a1', name: 'Tom' }) if (r.statusCode !== 400 || r.object().code !== 400) return r r = h2c.post('/login', { id: 123, name: 'Tom\t' }) if (r.statusCode !== 400 || r.object().code !== 400) return r r = h2c.post('/login', { id: 123, name: 'Tom' }).object() if (r.code === 1) { // 测试限流器允许的 10 次请求 for (let i = 0; i < 10; i++) { r = h2c.get('/userInfo').object() if (r.data.id !== 123 | r.data.name !== 'Tom') { return r.data } } // 测试限流器拒绝的 1 次请求 r = h2c.get('/userInfo') if (r.statusCode != 429) { return r } u.sleep(100) // 测试限流器过期后允许的 1 次请求 r = h2c.get('/userInfo').object() if (r.data.id !== 123 | r.data.name !== 'Tom') { return r.data } return true } return r }