|
| 1 | +import Koa from 'koa'; |
| 2 | +import KoaRouter from 'koa-router'; // or import Router from 'koa-router'; |
| 3 | +import bodyParser from 'koa-bodyparser'; // Add thisimport lib from "./lib"; |
| 4 | +// import { koaBody } from "koa-body" |
| 5 | +import { TYPES } from './helper'; |
| 6 | +import lib from "./lib"; |
| 7 | + |
| 8 | +// Helper to set up ERest instance for forceGroup: true |
| 9 | +const setupERestWithGroup = () => { |
| 10 | + return lib({ |
| 11 | + forceGroup: true, |
| 12 | + groups: { |
| 13 | + v1: { name: 'Version 1', prefix: '/v1' }, // Explicit prefix |
| 14 | + user: { name: 'User Group' } // Default prefix will be 'user' by camelCase2underscore |
| 15 | + }, |
| 16 | + }); |
| 17 | +}; |
| 18 | + |
| 19 | +function returnJson(ctx: Koa.Context, data: any) { |
| 20 | + ctx.type = 'application/json'; |
| 21 | + ctx.body = JSON.stringify(data); |
| 22 | +} |
| 23 | + |
| 24 | +describe('ERest Koa Integration', () => { |
| 25 | + let server: any; |
| 26 | + afterAll(() => { |
| 27 | + server.close(); |
| 28 | + }); |
| 29 | + describe('forceGroup: false', () => { |
| 30 | + const app = new Koa(); |
| 31 | + app.use(bodyParser()) // Use bodyParser for all non-group tests |
| 32 | + app.use(async (ctx, next) => { |
| 33 | + try { |
| 34 | + await next(); |
| 35 | + } catch (err: any) { |
| 36 | + ctx.status = err.status || 500; |
| 37 | + ctx.body = JSON.stringify({ |
| 38 | + message: err.message, |
| 39 | + stack: process.env.NODE_ENV === 'development' ? err.stack : undefined, |
| 40 | + }); |
| 41 | + } |
| 42 | + }); |
| 43 | + const apiService = lib(); |
| 44 | + const router = new KoaRouter(); |
| 45 | + server = app.listen() |
| 46 | + apiService.initTest(server); |
| 47 | + const { api } = apiService; |
| 48 | + // Test 1.1 |
| 49 | + api.get('/test-koa').group('Index').register(async (ctx: Koa.Context) => { |
| 50 | + returnJson(ctx, { data: 'koa works' }); |
| 51 | + }); |
| 52 | + |
| 53 | + // Test 1.2 |
| 54 | + api.get('/query-test') |
| 55 | + .group('Index') |
| 56 | + .query({ name: { type: TYPES.String, required: true } }) |
| 57 | + .register(async (ctx: Koa.Context) => { returnJson(ctx, { name: ctx.$params.name }); }); |
| 58 | + |
| 59 | + // Test 1.3 |
| 60 | + api.post('/body-test') |
| 61 | + .group('Index') |
| 62 | + .body({ id: { type: TYPES.Integer, required: true } }) |
| 63 | + .register(async (ctx: Koa.Context) => { |
| 64 | + returnJson(ctx, { id: ctx.$params.id }); |
| 65 | + }); |
| 66 | + |
| 67 | + // After all API definitions for this block |
| 68 | + apiService.bindRouter(router, apiService.checkerKoa); |
| 69 | + app.use(router.routes()).use(router.allowedMethods()); |
| 70 | + |
| 71 | + // Now run the actual test executions that were commented out above |
| 72 | + it('should handle basic GET request (execution)', async () => { |
| 73 | + const ret = await apiService.test.get('/test-koa').success(); |
| 74 | + expect(ret).toStrictEqual({ data: 'koa works' }); |
| 75 | + }); |
| 76 | + it('should validate query parameters (execution)', async () => { |
| 77 | + const ret = await apiService.test.get('/query-test').query({ name: "tester" }).success(); |
| 78 | + expect(ret).toStrictEqual({ name: 'tester' }); |
| 79 | + }); |
| 80 | + it('should validate POST body parameters (success)', async () => { |
| 81 | + const ret = await apiService.test.post('/body-test').input({ id: 'abc' }).error(); |
| 82 | + expect(ret).toStrictEqual(new Error('POST_/body-test 期望API输出失败结果,但实际输出成功结果:{}')); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should validate POST body parameters (error)', async () => { |
| 86 | + const ret1 = await apiService.test.post('/body-test').input({ id: 123 }).success(); |
| 87 | + expect(ret1).toStrictEqual({ id: 123 }); |
| 88 | + }); |
| 89 | + |
| 90 | + |
| 91 | + }); |
| 92 | + |
| 93 | + describe('forceGroup: true', () => { |
| 94 | + const appGroup = new Koa(); |
| 95 | + appGroup.use(bodyParser()); |
| 96 | + const erestGroup = setupERestWithGroup(); |
| 97 | + server = appGroup.listen(); |
| 98 | + erestGroup.initTest(server); |
| 99 | + // Test 2.1 |
| 100 | + erestGroup.group('v1').get('/grouped-test').register(async (ctx: Koa.Context) => { |
| 101 | + returnJson(ctx, { group: 'v1 works' }); |
| 102 | + }); |
| 103 | + |
| 104 | + erestGroup.group('user').get('/info').register(async (ctx: Koa.Context) => { |
| 105 | + returnJson(ctx, { group: 'user info' }); |
| 106 | + }); |
| 107 | + |
| 108 | + // After all API definitions for this block |
| 109 | + erestGroup.bindKoaRouterToApp(appGroup, KoaRouter, erestGroup.checkerKoa); |
| 110 | + |
| 111 | + // Now run the actual test executions |
| 112 | + it('should handle basic GET request in a group with explicit prefix (execution)', async () => { |
| 113 | + const ret = await erestGroup.test.get('/v1/grouped-test').success(); |
| 114 | + expect(ret).toStrictEqual({ group: 'v1 works' }); |
| 115 | + }); |
| 116 | + it('should handle GET request in a group with default prefix (execution)', async () => { |
| 117 | + // For a group key 'user' with no explicit prefix, camelCase2underscore will make it 'user' |
| 118 | + const ret = await erestGroup.test.get('/user/info').success() |
| 119 | + expect(ret).toStrictEqual({ group: 'user info' }); |
| 120 | + }); |
| 121 | + |
| 122 | + }); |
| 123 | +}); |
0 commit comments