Skip to content

Commit

Permalink
fix: http parser get body (#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar authored Feb 2, 2021
1 parent 0d76f21 commit 9afdbbb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
6 changes: 5 additions & 1 deletion packages-serverless/serverless-http-parser/src/http/req.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class HTTPRequest {

get url() {
if (!this[EVENT].url) {
const querystirng = qs.stringify(this.query || {});
const querystirng = (qs as any).stringify(this.query || {});
this[EVENT].url = this.path + (querystirng ? '?' + querystirng : '');
}
return this[EVENT].url;
Expand Down Expand Up @@ -91,6 +91,10 @@ export class HTTPRequest {
}

get body() {
const method = this.method.toLowerCase();
if (['get', 'head', 'delete', 'options'].includes(method)) {
return undefined;
}
if (this.bodyParsed) {
return this[BODY];
}
Expand Down
4 changes: 4 additions & 0 deletions packages-serverless/serverless-http-parser/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export const request = {
},

get body() {
const method = this.method.toLowerCase();
if (['get', 'head', 'delete', 'options'].includes(method)) {
return undefined;
}
if (this[BODY]) {
return this[BODY];
}
Expand Down
15 changes: 12 additions & 3 deletions packages-serverless/serverless-http-parser/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ describe('test http parser', () => {
// assert(context.res.headers['set-cookie'].length === 2);
});

it('body should undefined when method not post', () => {
const app = new Application();
const req = require('./resource/fc_http.json');
req.headers['Content-Type'] = 'application/json';
const res = new HTTPResponse();
const context = app.createContext(req, res);
assert(!context.request.body);
});

it('should parser fc http event', () => {
const app = new Application();
const req = Object.assign(require('./resource/fc_http.json'), {
Expand Down Expand Up @@ -158,7 +167,7 @@ describe('test http parser', () => {
a: '1',
});

assert.deepStrictEqual(context.request.body, '{"a":"1"}');
assert.deepStrictEqual(context.request.body, undefined);

assert(context.cookies.get('_ga') === 'GA1.2.690852134.1546410522');

Expand Down Expand Up @@ -207,8 +216,8 @@ describe('test http parser', () => {
assert(context.headers === context.req.headers);

// request
assert(context.method === 'GET');
assert(context.request.method === 'GET');
assert(context.method === 'POST');
assert(context.request.method === 'POST');
assert(context.path === '/api');
assert(context.request.path === '/api');
assert(context.url === '/api?name=test');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"path":"/api",
"httpMethod":"GET",
"httpMethod":"POST",
"headers":{
"Accept-Language": "en-US,en,cn",
"Accept": "text/html,application/xml,application/json",
Expand Down
4 changes: 2 additions & 2 deletions packages-serverless/serverless-scf-starter/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ describe('/test/index.test.ts', () => {
const runtime = await start();
const handle = asyncWrapper(async (...args) => {
return runtime.asyncEvent(async ctx => {
return 'hello world ' + ctx.req.body.user;
return 'hello world';
})(...args);
});
const event = {
Expand All @@ -387,7 +387,7 @@ describe('/test/index.test.ts', () => {
body: 'user=harry',
};
const data = await test(handle).runHttp(event, {});
assert(data.body === 'hello world harry');
assert(data.body === 'hello world');
});

it('should ok with asyncWrap use appoint args in event', async () => {
Expand Down

0 comments on commit 9afdbbb

Please sign in to comment.