From 0cd894a2d41287a62d05dc203b8d29eda2e0a6a1 Mon Sep 17 00:00:00 2001 From: Carmine DiMascio Date: Sun, 27 Dec 2020 20:47:14 -0500 Subject: [PATCH] fix: case-insenstive charset --- src/middlewares/parsers/body.parse.ts | 5 ----- src/middlewares/util.ts | 8 ++++---- test/content.type.spec.ts | 2 +- test/headers.spec.ts | 11 +++++++++++ test/response.validation.spec.ts | 9 +++++++++ 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/middlewares/parsers/body.parse.ts b/src/middlewares/parsers/body.parse.ts index 3a9e90f0..fdbddf17 100644 --- a/src/middlewares/parsers/body.parse.ts +++ b/src/middlewares/parsers/body.parse.ts @@ -1,4 +1,3 @@ -import { Ajv } from 'ajv'; import { ContentType } from '../util'; import { @@ -7,10 +6,6 @@ import { UnsupportedMediaType, } from '../../framework/types'; -type SchemaObject = OpenAPIV3.SchemaObject; -type ReferenceObject = OpenAPIV3.ReferenceObject; -type Schema = ReferenceObject | SchemaObject; - export class BodySchemaParser { constructor() { } diff --git a/src/middlewares/util.ts b/src/middlewares/util.ts index f654b88b..1d747bc5 100644 --- a/src/middlewares/util.ts +++ b/src/middlewares/util.ts @@ -11,12 +11,12 @@ export class ContentType { private constructor(contentType: string | null) { this.contentType = contentType; if (contentType) { - this.withoutBoundary = contentType.replace(/;\s{0,}boundary.*/, ''); - this.mediaType = this.withoutBoundary.split(';')[0].trim(); - this.charSet = this.withoutBoundary.split(';')[1]; + this.withoutBoundary = contentType.replace(/;\s{0,}boundary.*/, '').toLowerCase(); + this.mediaType = this.withoutBoundary.split(';')[0].toLowerCase().trim(); + this.charSet = this.withoutBoundary.split(';')[1]?.toLowerCase(); this.isWildCard = RegExp(/^[a-z]+\/\*$/).test(this.contentType); if (this.charSet) { - this.charSet = this.charSet.trim(); + this.charSet = this.charSet.toLowerCase().trim(); } } } diff --git a/test/content.type.spec.ts b/test/content.type.spec.ts index 210f3ce5..9db4fbbf 100644 --- a/test/content.type.spec.ts +++ b/test/content.type.spec.ts @@ -1,4 +1,4 @@ -import { findResponseContent, ContentType } from '../src/middlewares/util'; +import { findResponseContent } from '../src/middlewares/util'; import { expect } from 'chai'; describe('contentType', () => { diff --git a/test/headers.spec.ts b/test/headers.spec.ts index 3529c23b..25aa4dca 100644 --- a/test/headers.spec.ts +++ b/test/headers.spec.ts @@ -59,5 +59,16 @@ describe(packageJson.name, () => { tag: 'cat', }) .expect(200)); + + it('should match mediatype when charset case does not match the case defined in the spec', async () => + request(app) + .post(`${app.basePath}/pets_charset`) + .set('Content-Type', 'application/json; charset=UTF-8') + .set('Accept', 'application/json; charset=UTF-8') + .send({ + name: 'myPet', + tag: 'cat', + }) + .expect(200)); }); }); diff --git a/test/response.validation.spec.ts b/test/response.validation.spec.ts index 74e3a1c6..f3e2647d 100644 --- a/test/response.validation.spec.ts +++ b/test/response.validation.spec.ts @@ -91,6 +91,15 @@ describe(packageJson.name, () => { expect(r.body.id).to.be.a('number').that.equals(213); })); + it('should return 200 on valid responses 200 $ref', async () => + request(app) + .get(`${app.basePath}/ref_response_body`) + .set('Accept', 'APPLICATION/JSON') + .expect(200) + .then((r: any) => { + expect(r.body.id).to.be.a('number').that.equals(213); + })); + it('should fail if response field has a value of incorrect type', async () => request(app) .get(`${app.basePath}/pets?mode=bad_type`)