mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-30 22:34:10 -05:00
fix(middleware): allow content-type with charset (#4990)
* fix(middleware): allow content-type with charset * Add test
This commit is contained in:
parent
23f893453c
commit
899504143a
3 changed files with 39 additions and 1 deletions
5
.changeset/fifty-falcons-design.md
Normal file
5
.changeset/fifty-falcons-design.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@verdaccio/middleware': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(middleware): allow content-type with charset
|
|
@ -4,7 +4,18 @@ import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
|
||||||
|
|
||||||
export function media(expect: string | null): any {
|
export function media(expect: string | null): any {
|
||||||
return function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
|
return function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
|
||||||
if (req.headers[HEADER_TYPE.CONTENT_TYPE] !== expect) {
|
const header = req.headers[HEADER_TYPE.CONTENT_TYPE];
|
||||||
|
if (!header) {
|
||||||
|
next(
|
||||||
|
errorUtils.getCode(
|
||||||
|
HTTP_STATUS.UNSUPPORTED_MEDIA,
|
||||||
|
'content-type is missing, expect: ' + expect
|
||||||
|
)
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof header !== 'string' || header.split(';')[0].trim() !== expect) {
|
||||||
next(
|
next(
|
||||||
errorUtils.getCode(
|
errorUtils.getCode(
|
||||||
HTTP_STATUS.UNSUPPORTED_MEDIA,
|
HTTP_STATUS.UNSUPPORTED_MEDIA,
|
||||||
|
|
|
@ -20,6 +20,19 @@ test('media is json', async () => {
|
||||||
.expect(200);
|
.expect(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('media is json with charset', async () => {
|
||||||
|
const app = getApp([]);
|
||||||
|
app.get('/json', media(mime.getType('json')), (req, res) => {
|
||||||
|
res.status(200).json();
|
||||||
|
});
|
||||||
|
|
||||||
|
return request(app)
|
||||||
|
.get('/json')
|
||||||
|
.set(HEADERS.CONTENT_TYPE, 'application/json; charset=utf-8')
|
||||||
|
.expect('Content-Type', /json/)
|
||||||
|
.expect(200);
|
||||||
|
});
|
||||||
|
|
||||||
test('media is not json', async () => {
|
test('media is not json', async () => {
|
||||||
const app = getApp([]);
|
const app = getApp([]);
|
||||||
app.get('/json', media(mime.getType('json')), (req, res) => {
|
app.get('/json', media(mime.getType('json')), (req, res) => {
|
||||||
|
@ -32,3 +45,12 @@ test('media is not json', async () => {
|
||||||
.expect('Content-Type', /html/)
|
.expect('Content-Type', /html/)
|
||||||
.expect(HTTP_STATUS.UNSUPPORTED_MEDIA);
|
.expect(HTTP_STATUS.UNSUPPORTED_MEDIA);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('missing content-type', async () => {
|
||||||
|
const app = getApp([]);
|
||||||
|
app.get('/json', media(mime.getType('json')), (req, res) => {
|
||||||
|
res.status(HTTP_STATUS.OK).json({});
|
||||||
|
});
|
||||||
|
|
||||||
|
return request(app).get('/json').expect(HTTP_STATUS.UNSUPPORTED_MEDIA);
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue