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