Skip to content

Commit

Permalink
Merge pull request #130 from zevisert/fix/undefined-unparsed-body
Browse files Browse the repository at this point in the history
Fixing empty body breaking includeUnparsed
  • Loading branch information
MarkHerhold committed Jan 29, 2019
2 parents 2f9054b + eb3788a commit 235a084
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -101,9 +101,9 @@ function requestbody(opts) {
ctx.req.body = body.fields;
ctx.req.files = body.files;
} else if (opts.includeUnparsed) {
ctx.req.body = body.parsed;
ctx.req.body = body.parsed || {};
if (! ctx.is('text')) {
ctx.request.body[symbolUnparsed] = body.raw;
ctx.req.body[symbolUnparsed] = body.raw;
}
} else {
ctx.req.body = body;
Expand All @@ -114,7 +114,7 @@ function requestbody(opts) {
ctx.request.body = body.fields;
ctx.request.files = body.files;
} else if (opts.includeUnparsed) {
ctx.request.body = body.parsed;
ctx.request.body = body.parsed || {};
if (! ctx.is('text')) {
ctx.request.body[symbolUnparsed] = body.raw;
}
Expand Down
48 changes: 37 additions & 11 deletions test/index.js
Expand Up @@ -167,7 +167,7 @@ describe('koa-body', () => {
res.body.user.names.should.be.an.Array().and.have.lengthOf(2);
res.body.user.names[0].should.equal('John');
res.body.user.names[1].should.equal('Paul');
res.body._files.firstField.should.be.an.Object;
res.body._files.firstField.should.be.an.Object();
res.body._files.firstField.name.should.equal('package.json');
should(fs.statSync(res.body._files.firstField.path)).be.ok;
fs.unlinkSync(res.body._files.firstField.path);
Expand Down Expand Up @@ -230,9 +230,9 @@ describe('koa-body', () => {
.end( (err, res) => {
if (err) return done(err);

res.body._files.firstField.should.be.an.Object;
res.body._files.firstField.should.be.an.Object();
res.body._files.firstField.name.should.equal('backage.json');
should(fs.statSync(res.body._files.firstField.path)).be.ok;
should(fs.statSync(res.body._files.firstField.path)).be.ok();
fs.unlinkSync(res.body._files.firstField.path);

done();
Expand Down Expand Up @@ -275,7 +275,7 @@ describe('koa-body', () => {
let requestSpy;

beforeEach(() => {
app.use(koaBody({ includeUnparsed: true}));
app.use(koaBody({ includeUnparsed: true }));
app.use(router.routes());
});

Expand All @@ -284,6 +284,32 @@ describe('koa-body', () => {
requestSpy = undefined;
});

it('should not fail when no request body is provided', (done) => {
const echoRouterLayer = router.stack.filter(layer => layer.path === "/echo_body");
requestSpy = sinon.spy(echoRouterLayer[0].stack, '0');

request(http.createServer(app.callback()))
.post('/echo_body')
.type('application/json')
.send(undefined)
.expect(200)
.end((err, res) => {
if (err) return done(err);

assert(requestSpy.calledOnce, 'Spy for /echo_body not called');
const req = requestSpy.firstCall.args[0].request;

req.body.should.not.be.Undefined();
res.body.should.have.properties({});

req.body[unparsed].should.not.be.Undefined();
req.body[unparsed].should.be.a.String();
req.body[unparsed].should.have.length(0);

done();
});
});

it('should recieve `urlencoded` request bodies with the `includeUnparsed` option', (done) => {

const userRouterLayer = router.stack.filter(layer => layer.path === "/users" && layer.methods.includes('POST'));
Expand All @@ -304,8 +330,8 @@ describe('koa-body', () => {

assert(requestSpy.calledOnce, 'Spy for /users not called');
const req = requestSpy.firstCall.args[0].request;
req.body[unparsed].should.not.be.undefined;
req.body[unparsed].should.be.a.String;
req.body[unparsed].should.not.be.Undefined();
req.body[unparsed].should.be.a.String();
req.body[unparsed].should.equal('name=Test&followers=97');

res.body.user.should.have.properties({ name: 'Test', followers: '97' });
Expand All @@ -332,8 +358,8 @@ describe('koa-body', () => {

assert(requestSpy.calledOnce, 'Spy for /echo_body not called');
const req = requestSpy.firstCall.args[0].request;
req.body[unparsed].should.not.be.undefined;
req.body[unparsed].should.be.a.String;
req.body[unparsed].should.not.be.Undefined();
req.body[unparsed].should.be.a.String();
req.body[unparsed].should.equal(JSON.stringify({
hello: 'world',
number: 42
Expand All @@ -360,14 +386,14 @@ describe('koa-body', () => {

assert(requestSpy.calledOnce, 'Spy for /echo_body not called');
const req = requestSpy.firstCall.args[0].request;
req.body.should.equal('plain text content');
should(req.body).equal('plain text content');

// Raw text requests are still just text
assert.equal(req.body[unparsed], undefined);

// Text response is just text
res.body.should.have.properties({});
res.text.should.equal('plain text content');
should(res.body).have.properties({});
should(res.text).equal('plain text content');

done();
});
Expand Down

0 comments on commit 235a084

Please sign in to comment.