Skip to content

Commit

Permalink
fix: delete method parse body and form body (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Mar 8, 2021
1 parent 3ce2b57 commit f5c1e70
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 59 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"description": "a web framework for complex Node.js application",
"version": "1.0.0",
"devDependencies": {
"@midwayjs/cli": "^1.2.36",
"@midwayjs/cli-plugin-build": "^1.2.47",
"@types/jest": "^26.0.10",
"@types/node": "^10.17.21",
"lerna": "^3.20.2",
Expand Down
59 changes: 42 additions & 17 deletions packages-serverless/egg-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,52 @@ module.exports = engine => {
) {
context.headers['X-Forwarded-For'] = context.ip;
}
request(
{
uri: `http://unix:${socketPath}:${context.path}`,
qs: context.query,
method: context.method,
body:
typeof context.request.body === 'string'
? context.request.body
: JSON.stringify(context.request.body),
headers: context.headers,
followRedirect: false,
},
(error, response, body) => {
const requestOption = {
uri: `http://unix:${socketPath}:${context.path}`,
qs: context.query,
method: context.method,
headers: context.headers,
followRedirect: false,
};
if (
(context.headers['content-type'] || '').indexOf('application/json') >=
0
) {
// post json
if (typeof context.request.body !== 'string') {
requestOption.body = JSON.stringify(context.request.body);
} else {
requestOption.body = context.request.body;
}
} else if (
(context.headers['content-type'] || '').indexOf('form-urlencoded') >=
0
) {
// post formdata
requestOption.form = context.request.body;
} else if (context.request.body) {
console.warn(
'[egg-layer] unknown content-type:',
context.headers['content-type']
);
if (typeof context.request.body !== 'string') {
requestOption.form = context.request.body;
} else {
requestOption.body = context.request.body;
}
}
request(requestOption, (error, response, body) => {
context.res = response;
context.status = response.statusCode;
if (error) {
console.error('[egg-layer]' + error);
resolve('Internal Server Error');
} else {
context.res = response;
context.status = response.statusCode;
if (error) {
reject(error);
}
resolve(body);
}
);
});
});
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@ class HomeController extends Controller {
body: ctx.request.body,
};
}
async postFormBodyMethod() {
const { ctx } = this;
ctx.body = {
body: ctx.request.body
};
}
async buffer() {
const { ctx } = this;
ctx.setHeader('x-res', 'buffer');
// assert
ctx.body = Buffer.from('hi, egg');
}

async gotError() {
throw new Error('custom error');
}
}

module.exports = HomeController;
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ module.exports = app => {
router.get('/get/query', controller.home.getQueryMethod);
router.post('/post', controller.home.postMethod);
router.post('/post/body', controller.home.postBodyMethod);
router.post('/post/formBody', controller.home.postFormBodyMethod);
router.get('/buffer', controller.home.buffer);
router.get('/error', controller.home.gotError);
};
20 changes: 20 additions & 0 deletions packages-serverless/egg-layer/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ describe('/test/index.test.ts', () => {
.expect(200, done);
});

it('should test with post form body', (done) => {
request(app)
.post('/post/formBody')
.send('b=1')
.expect('Content-Type', 'application/json; charset=utf-8')
.expect(/{"body":{"b":"1"}}/)
.expect(200, done);
});

it('should test static file', done => {
request(app)
.get('/public/news.css')
Expand All @@ -82,6 +91,17 @@ describe('/test/index.test.ts', () => {
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(301, done);
});

it('should test throw error', done => {
request(app)
.get('/error')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(500, (err, res) => {
expect(res.statusCode).toEqual(500);
expect(res.text).toMatch('custom error');
done();
});
});
});

describe('FC test with api gateway', () => {
Expand Down
56 changes: 39 additions & 17 deletions packages-serverless/express-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,49 @@ module.exports = engine => {
return new Promise((resolve, reject) => {
delete context.headers['content-length'];
delete context.headers['accept-encoding'];
request(
{
uri: `http://unix:${socketPath}:${context.path}`,
qs: context.query,
method: context.method,
body:
typeof context.request.body === 'string'
? context.request.body
: JSON.stringify(context.request.body),
headers: context.headers,
followRedirect: false,
},
(error, response, body) => {

const requestOption = {
uri: `http://unix:${socketPath}:${context.path}`,
qs: context.query,
method: context.method,
headers: context.headers,
followRedirect: false,
};
if (
(context.headers['content-type'] || '').indexOf('application/json') >=
0
) {
// post json
if (typeof context.request.body !== 'string') {
requestOption.body = JSON.stringify(context.request.body);
} else {
requestOption.body = context.request.body;
}
} else if (
(context.headers['content-type'] || '').indexOf('form-urlencoded') >=
0
) {
// post formdata
requestOption.form = context.request.body;
} else if (context.request.body) {
if (typeof context.request.body !== 'string') {
requestOption.form = context.request.body;
} else {
requestOption.body = context.request.body;
}
}
request(requestOption, (error, response, body) => {
context.res = response;
context.status = response.statusCode;
if (error) {
console.error('[express-layer]' + error);
resolve('Internal Server Error');
} else {
context.res = response;
context.status = response.statusCode;
if (error) {
reject(error);
}
resolve(body);
}
);
});
});
},
});
Expand Down
6 changes: 6 additions & 0 deletions packages-serverless/express-layer/test/fixtures/app-fc/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ app.post('/post/body', (req, res) => {
});
});

app.post('/post/formBody', (req, res) => {
res.send({
body: req.body,
});
});

// app.listen(3000);

module.exports = async () => {
Expand Down
9 changes: 9 additions & 0 deletions packages-serverless/express-layer/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ describe('/test/index.test.ts', () => {
.expect(/{"body":{"b":1}}/)
.expect(200, done);
});

it('should test with post form body', (done) => {
request(app)
.post('/post/formBody')
.send('b=1')
.expect('Content-Type', 'application/json; charset=utf-8')
.expect(/{"body":{"b":"1"}}/)
.expect(200, done);
});
});

describe('FC test with api gateway', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages-serverless/faas-typings/typings/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ interface ContextDelegatedRequest {
* Get parsed query-string.
*/
query: { [key: string]: any };

/**
* Get parsed params
*/
params: { [key: string]: string };
}

export interface FaaSHTTPRequest extends ContextDelegatedRequest {
Expand Down
55 changes: 38 additions & 17 deletions packages-serverless/koa-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,48 @@ module.exports = engine => {
async defaultInvokeHandler(context) {
return new Promise((resolve, reject) => {
delete context.headers['content-length'];
request(
{
uri: `http://unix:${socketPath}:${context.path}`,
qs: context.query,
method: context.method,
body:
typeof context.request.body === 'string'
? context.request.body
: JSON.stringify(context.request.body),
headers: context.headers,
followRedirect: false,
},
(error, response, body) => {
const requestOption = {
uri: `http://unix:${socketPath}:${context.path}`,
qs: context.query,
method: context.method,
headers: context.headers,
followRedirect: false,
};
if (
(context.headers['content-type'] || '').indexOf('application/json') >=
0
) {
// post json
if (typeof context.request.body !== 'string') {
requestOption.body = JSON.stringify(context.request.body);
} else {
requestOption.body = context.request.body;
}
} else if (
(context.headers['content-type'] || '').indexOf('form-urlencoded') >=
0
) {
// post formdata
requestOption.form = context.request.body;
} else if (context.request.body) {
if (typeof context.request.body !== 'string') {
requestOption.form = context.request.body;
} else {
requestOption.body = context.request.body;
}
}
request(requestOption, (error, response, body) => {
context.res = response;
context.status = response.statusCode;
if (error) {
console.error('[koa-layer]' + error);
resolve('Internal Server Error');
} else {
context.res = response;
context.status = response.statusCode;
if (error) {
reject(error);
}
resolve(body);
}
);
});
});
},
});
Expand Down
4 changes: 2 additions & 2 deletions packages-serverless/koa-layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "2.8.8",
"main": "index.js",
"scripts": {
"test": "midway-bin test --forceExit",
"cov": "midway-bin cov --forceExit"
"test": "midway-bin test",
"cov": "midway-bin cov"
},
"dependencies": {
"request": "^2.88.2"
Expand Down
6 changes: 6 additions & 0 deletions packages-serverless/koa-layer/test/fixtures/app-fc/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ router.post('/post/body', (ctx, next) => {
};
});

router.post('/post/formBody', (ctx, next) => {
ctx.body = {
body: ctx.request.body,
};
});

app.use(bodyParser());
app.use(router.routes()).use(router.allowedMethods());

Expand Down
9 changes: 9 additions & 0 deletions packages-serverless/koa-layer/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ describe('/test/index.test.ts', () => {
.expect(/{"body":{"b":1}}/)
.expect(200, done);
});

it('should test with post form body', (done) => {
request(app)
.post('/post/formBody')
.send('b=1')
.expect('Content-Type', 'application/json; charset=utf-8')
.expect(/{"body":{"b":"1"}}/)
.expect(200, done);
});
});

describe('FC test with api gateway', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages-serverless/serverless-http-parser/src/http/req.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class HTTPRequest {

get body() {
const method = this.method.toLowerCase();
if (['get', 'head', 'delete', 'options'].includes(method)) {
if (['get', 'head', 'options'].includes(method)) {
return undefined;
}
if (this.bodyParsed) {
Expand Down
2 changes: 1 addition & 1 deletion packages-serverless/serverless-http-parser/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const request = {

get body() {
const method = this.method.toLowerCase();
if (['get', 'head', 'delete', 'options'].includes(method)) {
if (['get', 'head', 'options'].includes(method)) {
return undefined;
}
if (this[BODY]) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/util/pathFileUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const PathFileUtil = {
return one.replace(ext, '') === two;
},

getFileContentSync(filePath: any, encoding?) {
getFileContentSync(filePath: any, encoding?: string) {
return typeof filePath === 'string'
? readFileSync(filePath, encoding)
: filePath;
Expand Down
1 change: 0 additions & 1 deletion scripts/benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ npm install @midwayjs/cli autocannon
cp ./scripts/start.js ./midway_benchmark_app/start.js
cp ./scripts/benchmark.js ./midway_benchmark_app/benchmark.js
cd midway_benchmark_app
npm install autocannon
npm run build
npm link @midwayjs/web @midwayjs/core @midwayjs/decorator midway @midwayjs/mock @midwayjs/bootstrap
node benchmark.js

0 comments on commit f5c1e70

Please sign in to comment.