-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: compatible extra semicolon on content-type header (#153)
- Loading branch information
Showing
5 changed files
with
46 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
|
||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
Job: | ||
name: Node.js | ||
uses: node-modules/github-actions/.github/workflows/node-test.yml@master | ||
with: | ||
version: '12, 14, 16, 18, 20' |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,21 +1,8 @@ | ||
/** ! | ||
* koa-body-parser - index.js | ||
* Copyright(c) 2014 | ||
* MIT Licensed | ||
* | ||
* Authors: | ||
* dead_horse <[email protected]> (http://deadhorse.me) | ||
* fengmk2 <[email protected]> (http://fengmk2.com) | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const parse = require('co-body'); | ||
const copy = require('copy-to'); | ||
const typeis = require('type-is'); | ||
|
||
/** | ||
* @param [Object] opts | ||
|
@@ -94,7 +81,8 @@ module.exports = function(opts) { | |
async function parseBody(ctx) { | ||
if ( | ||
enableJson && | ||
((detectJSON && detectJSON(ctx)) || ctx.request.is(jsonTypes)) | ||
((detectJSON && detectJSON(ctx)) || | ||
isTypes(ctx.request.get('content-type'), jsonTypes)) | ||
) { | ||
return await parse.json(ctx, jsonOpts); // eslint-disable-line no-return-await | ||
} | ||
|
@@ -137,3 +125,12 @@ function extendType(original, extend) { | |
function checkEnable(types, type) { | ||
return types.includes(type); | ||
} | ||
|
||
function isTypes(contentTypeValue, types) { | ||
if (typeof contentTypeValue === 'string') { | ||
// trim extra semicolon | ||
contentTypeValue = contentTypeValue.replace(/;$/, ''); | ||
} | ||
|
||
return typeis.is(contentTypeValue, types); | ||
} |
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 |
---|---|---|
@@ -1,20 +1,3 @@ | ||
/** ! | ||
* koa-body-parser - test/middleware.test.js | ||
* | ||
* Copyright(c) 2014 | ||
* MIT Licensed | ||
* | ||
* Authors: | ||
* dead_horse <[email protected]> (http://deadhorse.me) | ||
* fengmk2 <[email protected]> (http://fengmk2.com) | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const path = require('path'); | ||
const request = require('supertest'); | ||
const Koa = require('koa'); | ||
|
@@ -61,6 +44,21 @@ describe('test/middleware.test.js', function() { | |
.expect({foo: 'bar'}, done); | ||
}); | ||
|
||
it('should parse json body with `content-type: application/json;charset=utf-8;` headers ok', async () => { | ||
app.use(bodyParser()); | ||
|
||
app.use(async ctx => { | ||
ctx.request.body.should.eql({foo: 'bar'}); | ||
ctx.request.rawBody.should.equal('{"foo": "bar"}'); | ||
ctx.body = ctx.request.body; | ||
}); | ||
await request(app.listen()) | ||
.post('/') | ||
.set('Content-type', 'application/json;charset=utf-8;') | ||
.send('{"foo": "bar"}') | ||
.expect({foo: 'bar'}); | ||
}); | ||
|
||
it('should parse json patch', function(done) { | ||
const app = App(); | ||
app.use(async ctx => { | ||
|