Skip to content

Commit

Permalink
fix(body): set request.body public body
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 2, 2017
1 parent ef476e6 commit 62ff44b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/BodyParser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ class BodyParser {
* @return {void}
*/
async handle ({ request }, next) {
request._body = {}
request._raw = {}
request._files = {}
request.body = {}
request._raw = {}

/**
* Don't bother when request does not have body
Expand Down Expand Up @@ -259,7 +259,7 @@ class BodyParser {

await request.multipart.process()
request._files = this.files.get()
request._body = this.fields.get()
request.body = this.fields.get()

await next()
return
Expand All @@ -272,7 +272,7 @@ class BodyParser {
*/
if (this._isType(request, this.jsonTypes)) {
debug('detected json body')
request._body = await this._parseJSON(request.request)
request.body = await this._parseJSON(request.request)
await next()
return
}
Expand All @@ -282,7 +282,7 @@ class BodyParser {
*/
if (this._isType(request, this.formTypes)) {
debug('detected form body')
request._body = await this._parseForm(request.request)
request.body = await this._parseForm(request.request)
await next()
return
}
Expand Down
28 changes: 14 additions & 14 deletions test/unit/body-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test.group('Body Parser', (group) => {
const parser = new BodyParser(new Config())
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify(request._body))
res.write(JSON.stringify(request.body))
res.end()
}

Expand Down Expand Up @@ -87,7 +87,7 @@ test.group('Body Parser', (group) => {
const parser = new BodyParser(new Config())
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify(request._body))
res.write(JSON.stringify(request.body))
res.end()
}

Expand Down Expand Up @@ -130,7 +130,7 @@ test.group('Body Parser', (group) => {
const parser = new BodyParser(new Config())
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify(request._body))
res.write(JSON.stringify(request.body))
res.end()
}

Expand All @@ -150,7 +150,7 @@ test.group('Body Parser', (group) => {
const parser = new BodyParser(new Config())
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify(request._body))
res.write(JSON.stringify(request.body))
res.end()
}
const { body } = await supertest(app.server).post('/').send({ name: 'virk', isJSON: true })
Expand All @@ -162,7 +162,7 @@ test.group('Body Parser', (group) => {
const parser = new BodyParser(new Config())
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify(request._body))
res.write(JSON.stringify(request.body))
res.end()
}
const { body } = await supertest(app.server).post('/').send({ names: ['virk', 'foo@bar.com'], isJSON: true })
Expand All @@ -176,7 +176,7 @@ test.group('Body Parser', (group) => {
const parser = new BodyParser(config)
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify(request._body))
res.write(JSON.stringify(request.body))
res.end()
}
const { body } = await supertest(app.server).post('/').send({ name: 'virk', isJSON: true })
Expand Down Expand Up @@ -208,7 +208,7 @@ test.group('Body Parser', (group) => {
const parser = new BodyParser(config)
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify(request._body))
res.write(JSON.stringify(request.body))
res.end()
}

Expand Down Expand Up @@ -359,7 +359,7 @@ test.group('Body Parser', (group) => {
const parser = new BodyParser(new Config())
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify(request._body))
res.write(JSON.stringify(request.body))
res.end()
}

Expand All @@ -379,7 +379,7 @@ test.group('Body Parser', (group) => {
const parser = new BodyParser(new Config())
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify(request._body))
res.write(JSON.stringify(request.body))
res.end()
}

Expand All @@ -399,7 +399,7 @@ test.group('Body Parser', (group) => {
const parser = new BodyParser(new Config())
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify({ fields: request._body, files: request._files.package }))
res.write(JSON.stringify({ fields: request.body, files: request._files.package }))
res.end()
}

Expand All @@ -426,7 +426,7 @@ test.group('Body Parser', (group) => {
await parser.handle({ request }, function () {})
setTimeout(() => {
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify({ fields: request._body, files: request._files }))
res.write(JSON.stringify({ fields: request.body, files: request._files }))
res.end()
})
}
Expand All @@ -453,7 +453,7 @@ test.group('Body Parser', (group) => {
await parser.handle({ request }, function () {})
setTimeout(() => {
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify({ fields: request._body, files: request._files }))
res.write(JSON.stringify({ fields: request.body, files: request._files }))
res.end()
})
}
Expand All @@ -478,7 +478,7 @@ test.group('Body Parser', (group) => {
const parser = new BodyParser(config)
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify({ fields: request._body, files: request._files.package }))
res.write(JSON.stringify({ fields: request.body, files: request._files.package }))
res.end()
}

Expand All @@ -501,7 +501,7 @@ test.group('Body Parser', (group) => {
const parser = new BodyParser(new Config())
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify({ fields: request._body, files: request._files, raw: request._raw }))
res.write(JSON.stringify({ fields: request.body, files: request._files, raw: request._raw }))
res.end()
}

Expand Down

0 comments on commit 62ff44b

Please sign in to comment.