Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/tests/with-contact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,17 @@ describe('withContract', () => {

expect(response.status).toBe(400);
expect(response.body.error).toBe(
'Unsupported content-type. Allowed: application/x-www-form-urlencoded',
'Unsupported content-type. Allowed: application/x-www-form-urlencoded, got: application/json',
);
});

it('should bypass content-type check if request has no body', async () => {
const response = await request(app).post('/custom-content-type'); // No .send() and no Content-Type header

expect(response.status).toBe(400);
// The error should be from Zod validation, not the Content-Type check
expect(response.body.error).toBe('Invalid request data');
});
});

describe('Schema-less Responses', () => {
Expand Down
27 changes: 17 additions & 10 deletions src/validator/with-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,23 @@ export function withContract<

// Check content type if body validation is required
if (config.request?.body) {
const contentType = expressReq.headers['content-type'];
const allowedContentTypes = config.request?.contentType ?? ['application/json'];

if (
!contentType ||
!allowedContentTypes.some((type) => contentType.includes(type))
) {
throw new ValidationError(
`Unsupported content-type. Allowed: ${allowedContentTypes.join(', ')}`,
);
const hasBody =
expressReq.headers['transfer-encoding'] !== undefined ||
(expressReq.headers['content-length'] !== undefined &&
expressReq.headers['content-length'] !== '0');

if (hasBody) {
const contentType = expressReq.headers['content-type'];
const allowedContentTypes = config.request?.contentType ?? ['application/json'];

if (
!contentType ||
!allowedContentTypes.some((type) => contentType.includes(type))
) {
throw new ValidationError(
`Unsupported content-type. Allowed: ${allowedContentTypes.join(', ')}, got: ${contentType}`,
);
}
}
}

Expand Down
Loading