Skip to content

Commit

Permalink
fix: encode request path in serverless (#1230)
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer2q committed Aug 16, 2021
1 parent 67bb7f5 commit c826e68
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
4 changes: 3 additions & 1 deletion packages-serverless/egg-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ module.exports = engine => {
if (eggApp.config.proxy && !context.headers['X-Forwarded-For']) {
context.headers['X-Forwarded-For'] = context.ip;
}
// fix path for special characters, eg chinese.
const path = encodeURI(context.path);
const requestOption = {
uri: `http://unix:${socketPath}:${context.path}`,
uri: `http://unix:${socketPath}:${path}`,
qs: context.query,
method: context.method,
headers: context.headers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {
Post,
Provide,
Inject,
Body, ContentType,
Body,
ContentType,
Param,
Get,
} from '@midwayjs/decorator';
import { UserService } from '../service/user';

Expand All @@ -22,4 +25,9 @@ export class APIController {
return 'data' + bbbb;
}

@Get('/echo/:param')
async echoParam(@Param() param: string) {
console.log('[echo param]', param);
return param;
}
}
26 changes: 18 additions & 8 deletions packages-serverless/egg-layer/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const { createRuntime } = require('@midwayjs/runtime-mock');
const { HTTPTrigger } = require('@midwayjs/serverless-fc-trigger');
const FCApiGatewayTrigger = require('@midwayjs/serverless-fc-trigger')
.ApiGatewayTrigger;
const FCApiGatewayTrigger =
require('@midwayjs/serverless-fc-trigger').ApiGatewayTrigger;
const { ApiGatewayTrigger } = require('@midwayjs/serverless-scf-trigger');
const { join } = require('path');
const request = require('supertest');

describe('/test/index.test.ts', () => {

describe('FC test with http trigger', () => {
let runtime;
let app;
Expand Down Expand Up @@ -69,7 +68,7 @@ describe('/test/index.test.ts', () => {
.expect(200, done);
});

it('should test with post form body', (done) => {
it('should test with post form body', done => {
request(app)
.post('/post/formBody')
.send('b=1')
Expand Down Expand Up @@ -279,14 +278,17 @@ describe('/test/index.test.ts', () => {
});
});


describe('FC test with midway app use http trigger', () => {
let runtime;
let app;
jest.setTimeout(100 * 60 * 1000);

beforeAll(async () => {
// set midway framework dir
process.env.EGG_FRAMEWORK_DIR = join(__dirname, '../node_modules/@midwayjs/web');
process.env.EGG_FRAMEWORK_DIR = join(
__dirname,
'../node_modules/@midwayjs/web'
);
const entryDir = join(__dirname, './fixtures/midway-fc');
process.env.ENTRY_DIR = entryDir;
runtime = createRuntime({
Expand All @@ -303,15 +305,23 @@ describe('/test/index.test.ts', () => {
process.env.ENTRY_DIR = '';
});

it('should test with get', done => {
it('should test payload with post', done => {
request(app)
.post('/api')
.send({
bbbbb: 'ccc'
bbbbb: 'ccc',
})
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(/dataccc/)
.expect(200, done);
});

it('should test param with get', done => {
request(app)
.get('/echo/%E6%B5%8B%E8%AF%95')
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('娴嬭瘯')
.expect(200, done);
});
});
});

0 comments on commit c826e68

Please sign in to comment.