Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: warn when json validator does not find a content type in the request #3707

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
31 changes: 27 additions & 4 deletions src/validator/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,25 @@ describe('JSON', () => {
expect(data).toEqual({ foo: 'bar' })
})

it('Should not validate if Content-Type is not set', async () => {
it('Should return 415 response if Content-Type is not set', async () => {
const res = await app.request('http://localhost/post', {
method: 'POST',
body: JSON.stringify({ foo: 'bar' }),
})
expect(res.status).toBe(200)
expect(res.status).toBe(415)
const data = await res.json()
expect(data.foo).toBeUndefined()
})

it('Should not validate if Content-Type is wrong', async () => {
it('Should return 415 response if Content-Type is wrong', async () => {
const res = await app.request('http://localhost/post', {
method: 'POST',
headers: {
'Content-Type': 'text/plain;charset=utf-8',
},
body: JSON.stringify({ foo: 'bar' }),
})
expect(res.status).toBe(200)
expect(res.status).toBe(415)
})

it('Should validate if Content-Type is a application/json with a charset', async () => {
Expand Down Expand Up @@ -231,6 +231,29 @@ describe('FormData', () => {
expect(await res.json()).toEqual({ message: 'hi' })
})

it('Should return 415 response if Content-Type is not set', async () => {
const formData = new FormData()
formData.append('message', 'hi')
const res = await app.request('http://localhost/post', {
method: 'POST',
body: formData,
})
expect(res.status).toBe(415)
})

it('Should return 415 response if Content-Type is wrong', async () => {
const formData = new FormData()
formData.append('message', 'hi')
const res = await app.request('http://localhost/post', {
method: 'POST',
headers: {
'Content-Type': 'text/plain;charset=utf-8',
},
body: formData,
})
expect(res.status).toBe(415)
})

it('Should validate a URL Encoded Data', async () => {
const params = new URLSearchParams()
params.append('foo', 'bar')
Expand Down
5 changes: 5 additions & 0 deletions src/validator/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const jsonRegex = /^application\/([a-z-\.]+\+)?json(;\s*[a-zA-Z0-9\-]+\=([^;]+))
const multipartRegex = /^multipart\/form-data(;\s?boundary=[a-zA-Z0-9'"()+_,\-./:=?]+)?$/
const urlencodedRegex = /^application\/x-www-form-urlencoded(;\s*[a-zA-Z0-9\-]+\=([^;]+))*$/

const ERROR_MESSAGE_JSON = 'missing Content-Type header "application/json"'
const ERROR_MESSAGE_FORM = 'missing Content-Type header "multipart/form-data" or "application/x-www-form-urlencoded"'
Soviut marked this conversation as resolved.
Show resolved Hide resolved

export const validator = <
InputType,
P extends string,
Expand Down Expand Up @@ -72,6 +75,7 @@ export const validator = <
switch (target) {
case 'json':
if (!contentType || !jsonRegex.test(contentType)) {
throw new HTTPException(415, { message: ERROR_MESSAGE_JSON })
break
}
try {
Expand All @@ -86,6 +90,7 @@ export const validator = <
!contentType ||
!(multipartRegex.test(contentType) || urlencodedRegex.test(contentType))
) {
throw new HTTPException(415, { message: ERROR_MESSAGE_FORM })
break
}

Expand Down