@modern-js/plugin-koa 1.4.5 → 1.4.8-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/.wireit/6275696c64/stdout +9 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/cli/index.js +60 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/context.js +6 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/index.js +3 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/plugin.js +129 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/registerRoutes.js +82 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/runtime.js +1 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/cli/index.js +76 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/context.js +15 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/index.js +28 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/plugin.js +151 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/registerRoutes.js +97 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/runtime.js +13 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/cli/index.js +60 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/context.js +6 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/index.js +3 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/plugin.js +129 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/registerRoutes.js +82 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/runtime.js +1 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/cli/index.js +76 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/context.js +15 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/index.js +28 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/plugin.js +151 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/registerRoutes.js +97 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/runtime.js +13 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/cli/index.js +60 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/context.js +6 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/index.js +3 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/plugin.js +129 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/registerRoutes.js +82 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/runtime.js +1 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/cli/index.js +76 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/context.js +15 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/index.js +28 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/plugin.js +151 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/registerRoutes.js +97 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/runtime.js +13 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/.wireit/6275696c64/stdout +9 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/cli/index.js +62 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/context.js +6 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/index.js +3 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/plugin.js +129 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/registerRoutes.js +82 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/runtime.js +1 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/cli/index.js +78 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/context.js +15 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/index.js +28 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/plugin.js +151 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/registerRoutes.js +97 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/runtime.js +13 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/cli/index.js +60 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/context.js +6 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/index.js +3 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/plugin.js +129 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/registerRoutes.js +82 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/runtime.js +1 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/cli/index.js +76 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/context.js +15 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/index.js +28 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/plugin.js +151 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/registerRoutes.js +97 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/runtime.js +13 -0
- package/{tests/fixtures/function-mode/api/_app.ts → .wireit/6275696c64/lock} +0 -0
- package/.wireit/6275696c64/state +1 -0
- package/.wireit/6275696c64/stdout +9 -0
- package/CHANGELOG.md +28 -0
- package/dist/js/modern/cli/index.js +6 -9
- package/dist/js/modern/registerRoutes.js +3 -3
- package/dist/js/node/cli/index.js +6 -8
- package/dist/js/node/registerRoutes.js +3 -3
- package/package.json +34 -10
- package/.eslintrc.js +0 -7
- package/jest.config.js +0 -7
- package/modern.config.js +0 -2
- package/tests/.eslintrc.js +0 -6
- package/tests/common.ts +0 -9
- package/tests/fixtures/assets/index.html +0 -12
- package/tests/fixtures/function-mode/api/hello.ts +0 -1
- package/tests/fixtures/function-mode/api/nest/user.ts +0 -33
- package/tests/fixtures/function-mode/api/upload.ts +0 -4
- package/tests/fixtures/lambda-mode/api/app.ts +0 -1
- package/tests/fixtures/lambda-mode/api/lambda/hello.ts +0 -1
- package/tests/fixtures/lambda-mode/api/lambda/nest/[id].ts +0 -1
- package/tests/fixtures/lambda-mode/api/lambda/nest/user.ts +0 -33
- package/tests/fixtures/lambda-mode/api/lambda/upload.ts +0 -4
- package/tests/functionMode.test.ts +0 -69
- package/tests/helpers.ts +0 -14
- package/tests/lambdaMode.test.ts +0 -318
- package/tests/tsconfig.json +0 -12
- package/tests/webServer.test.ts +0 -81
- package/tsconfig.json +0 -10
package/tests/lambdaMode.test.ts
DELETED
|
@@ -1,318 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import request from 'supertest';
|
|
3
|
-
import Koa from 'koa';
|
|
4
|
-
import { serverManager } from '@modern-js/server-core';
|
|
5
|
-
import Router from 'koa-router';
|
|
6
|
-
import koaBody from 'koa-body';
|
|
7
|
-
import plugin from '../src/plugin';
|
|
8
|
-
import { APIPlugin } from './helpers';
|
|
9
|
-
import { INTROSPECTION_ROUTE_PATH } from './common';
|
|
10
|
-
|
|
11
|
-
const pwd = path.join(__dirname, './fixtures/lambda-mode');
|
|
12
|
-
const API_DIR = './api';
|
|
13
|
-
const MockKoa = Koa;
|
|
14
|
-
const MockRouter = Router;
|
|
15
|
-
const MockKoaBody = koaBody;
|
|
16
|
-
|
|
17
|
-
describe('lambda-mode', () => {
|
|
18
|
-
const id = '666';
|
|
19
|
-
const name = 'foo';
|
|
20
|
-
const foo = { id, name };
|
|
21
|
-
const prefix = '/api';
|
|
22
|
-
let apiHandler: any;
|
|
23
|
-
|
|
24
|
-
beforeAll(async () => {
|
|
25
|
-
const runner = await serverManager
|
|
26
|
-
.clone()
|
|
27
|
-
.usePlugin(APIPlugin, plugin)
|
|
28
|
-
.init();
|
|
29
|
-
apiHandler = await runner.prepareApiServer({
|
|
30
|
-
pwd,
|
|
31
|
-
mode: 'framework',
|
|
32
|
-
prefix,
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test('should works', async () => {
|
|
37
|
-
const res = await request(apiHandler).get(`${prefix}/hello`);
|
|
38
|
-
expect(res.status).toBe(200);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test('should works with query', async () => {
|
|
42
|
-
const res = await request(apiHandler).get(`${prefix}/nest/user?id=${id}`);
|
|
43
|
-
expect(res.status).toBe(200);
|
|
44
|
-
expect(res.body.query.id).toBe(id);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test('should works with body', async () => {
|
|
48
|
-
const res = await request(apiHandler).post(`${prefix}/nest/user`).send(foo);
|
|
49
|
-
expect(res.status).toBe(200);
|
|
50
|
-
expect(res.body.data).toEqual(foo);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test('should works with dynamic route ', async () => {
|
|
54
|
-
const res = await request(apiHandler).post(`${prefix}/nest/${id}`);
|
|
55
|
-
expect(res.status).toBe(200);
|
|
56
|
-
expect(res.body).toEqual({ id });
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
test('should works with context', async () => {
|
|
60
|
-
const res = await request(apiHandler)
|
|
61
|
-
.post(`${prefix}/nest/user?id=${id}`)
|
|
62
|
-
.send(foo);
|
|
63
|
-
expect(res.status).toBe(200);
|
|
64
|
-
expect(res.body.data).toEqual(foo);
|
|
65
|
-
expect(res.body.query.id).toBe(id);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
test('should support cookies', async () => {
|
|
69
|
-
const res = await request(apiHandler)
|
|
70
|
-
.post(`${prefix}/nest/user?id=${id}`)
|
|
71
|
-
.set('Cookie', [`id=${id};name=${name}`]);
|
|
72
|
-
expect(res.status).toBe(200);
|
|
73
|
-
expect(res.body.cookies.id).toBe(id);
|
|
74
|
-
expect(res.body.cookies.name).toBe(name);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test('should works with schema', async () => {
|
|
78
|
-
const res = await request(apiHandler).patch(`${prefix}/nest/user`).send({
|
|
79
|
-
id: 777,
|
|
80
|
-
name: 'xxx',
|
|
81
|
-
});
|
|
82
|
-
expect(res.status).toBe(200);
|
|
83
|
-
|
|
84
|
-
const res2 = await request(apiHandler).patch(`${prefix}/nest/user`).send({
|
|
85
|
-
id: 'aaa',
|
|
86
|
-
name: 'xxx',
|
|
87
|
-
});
|
|
88
|
-
expect(res2.status).toBe(400);
|
|
89
|
-
|
|
90
|
-
const res3 = await request(apiHandler).patch(`${prefix}/nest/user`).send({
|
|
91
|
-
id: '777',
|
|
92
|
-
name: 'xxx',
|
|
93
|
-
});
|
|
94
|
-
expect(res3.status).toBe(500);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test('introspection', async () => {
|
|
98
|
-
const res = await request(apiHandler).get(
|
|
99
|
-
`${prefix}${INTROSPECTION_ROUTE_PATH}`,
|
|
100
|
-
);
|
|
101
|
-
expect(res.status).toBe(200);
|
|
102
|
-
expect(res.body.protocol).toBe('Farrow-API');
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test('should support upload file', done => {
|
|
106
|
-
request(apiHandler)
|
|
107
|
-
.post(`${prefix}/upload`)
|
|
108
|
-
.field('my_field', 'value')
|
|
109
|
-
.attach('file', path.join(__dirname, './fixtures/assets/index.html'))
|
|
110
|
-
.end(async (err, res) => {
|
|
111
|
-
if (err) {
|
|
112
|
-
throw err;
|
|
113
|
-
}
|
|
114
|
-
expect(res.statusCode).toBe(200);
|
|
115
|
-
expect(res.body.message).toBe('success');
|
|
116
|
-
expect(res.body.formData).not.toBeUndefined();
|
|
117
|
-
done();
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
describe('add middlewares', () => {
|
|
123
|
-
let runner: any;
|
|
124
|
-
|
|
125
|
-
beforeAll(async () => {
|
|
126
|
-
runner = await serverManager.clone().usePlugin(APIPlugin, plugin).init();
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
test('should works', async () => {
|
|
130
|
-
const foo = 'foo';
|
|
131
|
-
const fakeMiddleware = jest.fn(async (ctx: any, next: any) => {
|
|
132
|
-
await next();
|
|
133
|
-
ctx.body = foo;
|
|
134
|
-
});
|
|
135
|
-
const fakeMiddleware2 = jest.fn(async (ctx: any, next: any) => {
|
|
136
|
-
await next();
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
140
|
-
const [mock_fakeMiddleware, mock_fakeMiddleware2] = [
|
|
141
|
-
fakeMiddleware,
|
|
142
|
-
fakeMiddleware2,
|
|
143
|
-
];
|
|
144
|
-
const apiHandler = await runner.prepareApiServer({
|
|
145
|
-
pwd,
|
|
146
|
-
mode: 'framework',
|
|
147
|
-
config: {
|
|
148
|
-
middleware: [
|
|
149
|
-
mock_fakeMiddleware,
|
|
150
|
-
mock_fakeMiddleware2,
|
|
151
|
-
mock_fakeMiddleware2,
|
|
152
|
-
],
|
|
153
|
-
},
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
const res = await request(apiHandler).get('/user');
|
|
157
|
-
expect(fakeMiddleware.mock.calls.length).toBe(1);
|
|
158
|
-
expect(fakeMiddleware2.mock.calls.length).toBe(2);
|
|
159
|
-
expect(res.status).toBe(200);
|
|
160
|
-
expect(res.text).toEqual(foo);
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
describe('support use koaBody in app.ts', () => {
|
|
165
|
-
const id = '666';
|
|
166
|
-
const name = 'modernjs';
|
|
167
|
-
const foo = { id, name };
|
|
168
|
-
|
|
169
|
-
let runner: any;
|
|
170
|
-
|
|
171
|
-
beforeAll(async () => {
|
|
172
|
-
runner = await serverManager.clone().usePlugin(APIPlugin, plugin).init();
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
test('support use koaBody', async () => {
|
|
176
|
-
const mock_foo = foo;
|
|
177
|
-
jest.mock(
|
|
178
|
-
path.join(pwd, API_DIR, 'app.ts'),
|
|
179
|
-
() => {
|
|
180
|
-
const app = new MockKoa();
|
|
181
|
-
// test app takes effect
|
|
182
|
-
app.use(async (ctx, next) => {
|
|
183
|
-
ctx.request.query = mock_foo;
|
|
184
|
-
await next();
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
app.use(MockKoaBody());
|
|
188
|
-
|
|
189
|
-
return {
|
|
190
|
-
__esModule: true,
|
|
191
|
-
default: app,
|
|
192
|
-
};
|
|
193
|
-
},
|
|
194
|
-
{ virtual: true },
|
|
195
|
-
);
|
|
196
|
-
|
|
197
|
-
const apiHandler = await runner.prepareApiServer({
|
|
198
|
-
pwd,
|
|
199
|
-
mode: 'framework',
|
|
200
|
-
});
|
|
201
|
-
const res = await request(apiHandler).post('/nest/user').send(foo);
|
|
202
|
-
expect(res.status).toBe(200);
|
|
203
|
-
expect(res.body.query).toEqual(foo);
|
|
204
|
-
expect(res.body.data).toEqual(foo);
|
|
205
|
-
});
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
describe('support app.ts in lambda mode', () => {
|
|
209
|
-
const name = 'modernjs';
|
|
210
|
-
|
|
211
|
-
let runner: any;
|
|
212
|
-
|
|
213
|
-
beforeAll(async () => {
|
|
214
|
-
runner = await serverManager.clone().usePlugin(APIPlugin, plugin).init();
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
beforeEach(() => {
|
|
218
|
-
jest.resetModules();
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
test('support es module', async () => {
|
|
222
|
-
jest.mock(
|
|
223
|
-
path.join(pwd, API_DIR, 'app.ts'),
|
|
224
|
-
() => {
|
|
225
|
-
const app = new MockKoa();
|
|
226
|
-
return {
|
|
227
|
-
__esModule: true,
|
|
228
|
-
default: app,
|
|
229
|
-
};
|
|
230
|
-
},
|
|
231
|
-
{ virtual: true },
|
|
232
|
-
);
|
|
233
|
-
|
|
234
|
-
const apiHandler = await runner.prepareApiServer({
|
|
235
|
-
pwd,
|
|
236
|
-
mode: 'framework',
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
const res = await request(apiHandler).get(`/nest/user?name=${name}`);
|
|
240
|
-
expect(res.status).toBe(200);
|
|
241
|
-
expect(res.body.query.name).toBe(name);
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
test('support commonjs module', async () => {
|
|
245
|
-
jest.mock(
|
|
246
|
-
path.join(pwd, API_DIR, 'app.ts'),
|
|
247
|
-
() => {
|
|
248
|
-
const app = new MockKoa();
|
|
249
|
-
return app;
|
|
250
|
-
},
|
|
251
|
-
{ virtual: true },
|
|
252
|
-
);
|
|
253
|
-
|
|
254
|
-
const apiHandler = await runner.prepareApiServer({
|
|
255
|
-
pwd,
|
|
256
|
-
mode: 'framework',
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
const res = await request(apiHandler).get(`/nest/user?name=${name}`);
|
|
260
|
-
expect(res.status).toBe(200);
|
|
261
|
-
expect(res.body.query.name).toBe(name);
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
test('support use middleware', async () => {
|
|
265
|
-
const mock_name = name;
|
|
266
|
-
jest.mock(
|
|
267
|
-
path.join(pwd, API_DIR, 'app.ts'),
|
|
268
|
-
() => {
|
|
269
|
-
const app = new MockKoa();
|
|
270
|
-
app.use(async (ctx, next) => {
|
|
271
|
-
ctx.query.name = mock_name;
|
|
272
|
-
await next();
|
|
273
|
-
});
|
|
274
|
-
return app;
|
|
275
|
-
},
|
|
276
|
-
{ virtual: true },
|
|
277
|
-
);
|
|
278
|
-
|
|
279
|
-
const apiHandler = await runner.prepareApiServer({
|
|
280
|
-
pwd,
|
|
281
|
-
mode: 'framework',
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
const res = await request(apiHandler).get(`/nest/user`);
|
|
285
|
-
expect(res.status).toBe(200);
|
|
286
|
-
expect(res.body.query.name).toBe(name);
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
test('support use router', async () => {
|
|
290
|
-
jest.mock(
|
|
291
|
-
path.join(pwd, API_DIR, 'app.ts'),
|
|
292
|
-
() => {
|
|
293
|
-
const app = new MockKoa();
|
|
294
|
-
const router = new MockRouter();
|
|
295
|
-
router.get('/hello', ctx => {
|
|
296
|
-
ctx.body = `foo`;
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
app.use(router.routes());
|
|
300
|
-
return app;
|
|
301
|
-
},
|
|
302
|
-
{ virtual: true },
|
|
303
|
-
);
|
|
304
|
-
|
|
305
|
-
const apiHandler = await runner.prepareApiServer({
|
|
306
|
-
pwd,
|
|
307
|
-
mode: 'framework',
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
const res1 = await request(apiHandler).get(`/hello`);
|
|
311
|
-
expect(res1.status).toBe(200);
|
|
312
|
-
expect(res1.text).toBe('foo');
|
|
313
|
-
|
|
314
|
-
const res2 = await request(apiHandler).get(`/nest/user?name=${name}`);
|
|
315
|
-
expect(res2.status).toBe(200);
|
|
316
|
-
expect(res2.body.query.name).toBe(name);
|
|
317
|
-
});
|
|
318
|
-
});
|
package/tests/tsconfig.json
DELETED
package/tests/webServer.test.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import request from 'supertest';
|
|
4
|
-
import { serverManager } from '@modern-js/server-core';
|
|
5
|
-
import plugin from '../src/plugin';
|
|
6
|
-
import { APIPlugin } from './helpers';
|
|
7
|
-
import './common';
|
|
8
|
-
|
|
9
|
-
const pwd = path.join(__dirname, './fixtures/function-mode');
|
|
10
|
-
|
|
11
|
-
describe('webServer', () => {
|
|
12
|
-
const id = '666';
|
|
13
|
-
const name = 'foo';
|
|
14
|
-
const foo = { id, name };
|
|
15
|
-
let webHandler: any;
|
|
16
|
-
let runner: any;
|
|
17
|
-
|
|
18
|
-
beforeAll(async () => {
|
|
19
|
-
runner = await serverManager.clone().usePlugin(APIPlugin, plugin).init();
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test('support json', async () => {
|
|
23
|
-
const middleware = (ctx: any) => {
|
|
24
|
-
// ctx.req.url = `${ctx.req.url}?id=${id}`;
|
|
25
|
-
ctx.body = foo;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
webHandler = await runner.prepareWebServer({
|
|
29
|
-
pwd,
|
|
30
|
-
config: { middleware: [middleware] },
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
const res = await request(webHandler).get('/');
|
|
34
|
-
expect(res.status).toBe(200);
|
|
35
|
-
expect(res.get('content-type')).toBe('application/json; charset=utf-8');
|
|
36
|
-
expect(res.body).toEqual(foo);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('support stream', async () => {
|
|
40
|
-
const middleware = (ctx: any) => {
|
|
41
|
-
ctx.response.set('content-type', 'text/html');
|
|
42
|
-
ctx.body = fs.createReadStream(
|
|
43
|
-
path.resolve(__dirname, './fixtures/assets/index.html'),
|
|
44
|
-
);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
webHandler = await runner.prepareWebServer({
|
|
48
|
-
pwd,
|
|
49
|
-
config: { middleware: [middleware] },
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
const res = await request(webHandler).get('/');
|
|
53
|
-
expect(res.status).toBe(200);
|
|
54
|
-
expect(res.get('content-type')).toBe('text/html');
|
|
55
|
-
expect(res.text).toContain(`hello koa plugin`);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test('should support Multiple middleware', async () => {
|
|
59
|
-
const middleware1 = async (ctx: any, next: any) => {
|
|
60
|
-
await next();
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const middleware2 = async (ctx: any, next: any) => {
|
|
64
|
-
ctx.response.set('content-type', 'text/html');
|
|
65
|
-
ctx.body = fs.createReadStream(
|
|
66
|
-
path.resolve(__dirname, './fixtures/assets/index.html'),
|
|
67
|
-
);
|
|
68
|
-
await next();
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
webHandler = await runner.prepareWebServer({
|
|
72
|
-
pwd,
|
|
73
|
-
config: { middleware: [middleware1, middleware2] },
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const res = await request(webHandler).get('/');
|
|
77
|
-
expect(res.status).toBe(200);
|
|
78
|
-
expect(res.get('content-type')).toBe('text/html');
|
|
79
|
-
expect(res.text).toContain(`hello koa plugin`);
|
|
80
|
-
});
|
|
81
|
-
});
|