Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Dec 28, 2020
2 parents 7357083 + 20aa8f1 commit f781400
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
5 changes: 0 additions & 5 deletions src/middlewares/parsers/body.parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Ajv } from 'ajv';
import { ContentType } from '../util';

import {
Expand All @@ -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() {
}
Expand Down
8 changes: 4 additions & 4 deletions src/middlewares/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/content.type.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findResponseContent, ContentType } from '../src/middlewares/util';
import { findResponseContent } from '../src/middlewares/util';
import { expect } from 'chai';

describe('contentType', () => {
Expand Down
11 changes: 11 additions & 0 deletions test/headers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
9 changes: 9 additions & 0 deletions test/response.validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down

0 comments on commit f781400

Please sign in to comment.