Skip to content

Commit

Permalink
fix #124 support 'charset' in Content-Type header
Browse files Browse the repository at this point in the history
  • Loading branch information
kikyomits committed Nov 16, 2019
1 parent d8caea5 commit 0e20680
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion src/middlewares/util.ts
Expand Up @@ -5,15 +5,30 @@ import { ValidationError } from '../framework/types';

export function extractContentType(req: Request): string | null {
let contentType = req.headers['content-type'];
if (!contentType) {
return null;
}
if (contentType.match(/charset/)) {
// if contentType has charset such as 'application.json; charset=utf-8',
// will use raw value of contentType
return contentType;
} else {
// if doesn't, will use media-type only in contentType by dropping any characters after semicolon.
return extractMediaType(contentType)
}
}

function extractMediaType(contentType: string | null): string | null {
if (!contentType) {
return null;
}
let end = contentType.indexOf(';');
end = end === -1 ? contentType.length : end;
if (contentType) {
return contentType.substring(0, end);
} else {
return contentType
}
return contentType;
}

const _validationError = (
Expand Down
32 changes: 21 additions & 11 deletions test/multipart.spec.ts
Expand Up @@ -5,18 +5,15 @@ import { createApp } from './common/app';
import * as packageJson from '../package.json';

describe(packageJson.name, () => {
let app = null;
before(async () => {
const apiSpec = path.join('test', 'resources', 'openapi.yaml');
app = await createApp({ apiSpec }, 3003);
});
after(() => {
(<any>app).server.close();
});
describe(`GET .../pets/:id/photos`, () => {
let app = null;

before(async () => {
const apiSpec = path.join('test', 'resources', 'openapi.yaml');
app = await createApp({ apiSpec }, 3003);
});

after(() => {
(<any>app).server.close();
});

it('should throw 400 when required multipart file field', async () =>
request(app)
.post(`${app.basePath}/pets/10/photos`)
Expand Down Expand Up @@ -85,4 +82,17 @@ describe(packageJson.name, () => {
.equal('unsupported media type application/json');
}));
});
describe(`POST .../pets`, () => {
it('should find appropriate request body in spec by contentType with charset', 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));
})

});
24 changes: 24 additions & 0 deletions test/resources/openapi.yaml
Expand Up @@ -313,6 +313,30 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pets_charset:
post:
description: Creates a new pet in the store. Duplicates are allowed
operationId: addPet
requestBody:
description: Pet to add to the store
required: true
content:
application/json; charset=utf-8:
schema:
$ref: '#/components/schemas/NewPet'
responses:
'200':
description: pet response
content:
application/json; charset=utf-8:
schema:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json; charset=utf-8:
schema:
$ref: '#/components/schemas/Error'

components:
parameters:
Expand Down

0 comments on commit 0e20680

Please sign in to comment.