-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update middleware to be koa v2 Promises
- Loading branch information
Showing
5 changed files
with
87 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const body = require('co-body'); | ||
|
||
module.exports = function(opts) { | ||
opts = opts || { strict: true }; | ||
|
||
return (ctx, next) => { | ||
if (ctx.method === 'GET' || ctx.method === 'DELETE') { | ||
return next(); | ||
} | ||
|
||
return body.json(ctx.req, opts).then((body) => { | ||
ctx.request.body = body; | ||
return next(); | ||
}, (err) => { | ||
return next(err); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
const body = require('../'); | ||
const Koa = require('koa'); | ||
const request = require('supertest'); | ||
|
||
describe('integration', function() { | ||
let app; | ||
|
||
before(function() { | ||
app = new Koa(); | ||
app.use(body()); | ||
app.use( ctx => { | ||
ctx.body = ctx.request.body; | ||
}); | ||
}); | ||
|
||
it('does nothing on a GET request', function(done) { | ||
request(app.listen()) | ||
.get('/') | ||
.expect(204, done); | ||
}); | ||
|
||
it('does nothing on a DELETE request', function(done) { | ||
request(app.listen()) | ||
.delete('/') | ||
.expect(204, done); | ||
}); | ||
|
||
it('parses JSON and assigns it to ctx.request.body', function(done) { | ||
request(app.listen()) | ||
.post('/') | ||
.send({ | ||
something: 'awesome' | ||
}) | ||
.expect(200, { something: 'awesome' }, done); | ||
}); | ||
|
||
it('silently handles invalid JSON', function(done) { | ||
request(app.listen()) | ||
.post('/') | ||
.set('Content-Type', 'application/json') | ||
.set('Accept', 'application/json') | ||
.send('{ "not": "json"') | ||
.expect(204, done); | ||
}); | ||
}); |