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
1 change: 0 additions & 1 deletion src/parsers/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export function prepareTextParserOptions(options: Partial<BodyParserRawConfig>):
return {
encoding: options.encoding ?? 'utf8',
limit: options.limit ?? '56kb',
length: 0,
}
}

Expand Down
54 changes: 54 additions & 0 deletions tests/parsers/json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import supertest from 'supertest'
import { test } from '@japa/runner'
import { createServer } from 'node:http'
import { gzipSync } from 'node:zlib'
import { parseJSON, prepareJSONParserOptions } from '../../src/parsers/json.ts'

test.group('JSON parser', () => {
Expand Down Expand Up @@ -53,6 +54,59 @@ test.group('JSON parser', () => {
assert.equal(text, 'Unsupported Content-Encoding: invalid')
})

test('parse valid gzip request body', async ({ assert }) => {
const server = createServer(async (req, res) => {
try {
const body = await parseJSON(req, prepareJSONParserOptions({}))
res.writeHead(200, { 'content-type': 'application/json' })
res.end(JSON.stringify(body))
} catch (error) {
res.writeHead(error.status || 500, { 'content-type': 'application/json' })
res.end(
JSON.stringify({
message: error.message,
status: error.status,
code: error.code,
type: error.type,
})
)
}
})

const payload = gzipSync(Buffer.from(JSON.stringify({ foo: { bar: 'baz' } })))
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))

try {
const address = server.address()
if (!address || typeof address === 'string') {
throw new Error('Failed to resolve test server address')
}

const response = await fetch(`http://127.0.0.1:${address.port}/`, {
method: 'POST',
headers: {
'content-type': 'application/json',
'content-encoding': 'gzip',
},
body: payload,
})

assert.equal(response.status, 200)
assert.deepEqual(await response.json(), {
parsed: { foo: { bar: 'baz' } },
raw: JSON.stringify({ foo: { bar: 'baz' } }),
})
} finally {
await new Promise<void>((resolve, reject) => {
server.close((error) => {
if (error) return reject(error)

resolve()
})
})
}
})

test('return empty string when content-length=0 and strict is false', async ({ assert }) => {
const server = createServer(async (req, res) => {
const body = await parseJSON(
Expand Down
37 changes: 37 additions & 0 deletions tests/parsers/raw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import supertest from 'supertest'
import { test } from '@japa/runner'
import { createServer } from 'node:http'
import { gzipSync } from 'node:zlib'
import { parseText, prepareTextParserOptions } from '../../src/parsers/text.ts'

test.group('Raw parser', () => {
Expand All @@ -24,6 +25,42 @@ test.group('Raw parser', () => {
assert.equal(text, 'Hello World!')
})

test('inflate gzip request body', async ({ assert }) => {
const server = createServer(async (req, res) => {
const body = await parseText(req, prepareTextParserOptions({}))
res.writeHead(200)
res.end(body)
})

await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))

try {
const address = server.address()
if (!address || typeof address === 'string') {
throw new Error('Failed to resolve test server address')
}

const response = await fetch(`http://127.0.0.1:${address.port}/`, {
method: 'POST',
headers: {
'content-encoding': 'gzip',
},
body: gzipSync(Buffer.from('Hello World!')),
})

assert.equal(response.status, 200)
assert.equal(await response.text(), 'Hello World!')
} finally {
await new Promise<void>((resolve, reject) => {
server.close((error) => {
if (error) return reject(error)

resolve()
})
})
}
})

test('fail with 415 when content encoding is invalid', async ({ assert }) => {
const server = createServer(async (req, res) => {
try {
Expand Down
Loading