Skip to content

Commit

Permalink
fix: compatible extra semicolon on content-type header (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Jun 22, 2023
1 parent c16f056 commit 5a551b1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/nodejs.yml
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'
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

27 changes: 12 additions & 15 deletions index.js
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 <dead_horse@qq.com> (http://deadhorse.me)
* fengmk2 <m@fengmk2.com> (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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"lint:fix": "xo --fix",
"test": "mocha --require should test/*.spec.js --exit",
"coverage": "nyc npm run test --reporter=lcov",
"test-ci": "npm run lint && npm run coverage"
"ci": "npm run lint && npm run coverage"
},
"repository": {
"type": "git",
Expand All @@ -34,15 +34,16 @@
"eslint-config-xo-lass": "^1.0.3",
"husky": "^4.2.5",
"koa": "^2",
"mocha": "^7.1.1",
"mocha": "^10.2.0",
"nyc": "^15.0.1",
"should": "^13.2.3",
"supertest": "^4.0.2",
"xo": "0.25.4"
},
"dependencies": {
"co-body": "^6.0.0",
"copy-to": "^2.0.1"
"copy-to": "^2.0.1",
"type-is": "^1.6.18"
},
"xo": {
"prettier": true,
Expand Down
32 changes: 15 additions & 17 deletions test/middleware.spec.js
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 <dead_horse@qq.com> (http://deadhorse.me)
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/

'use strict';

/**
* Module dependencies.
*/

const path = require('path');
const request = require('supertest');
const Koa = require('koa');
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit 5a551b1

Please sign in to comment.