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
4 changes: 4 additions & 0 deletions src/runtime/internal/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ async function fetchContent<T>(
): Promise<T> {
const headers = event ? getRequestHeaders(event) : {}
headers['accept-encoding'] = undefined // prevent unsupported enconding issue (https://github.com/nuxt/content/pull/3701)
// The internal request has its own body, so never inherit the incoming request's body framing headers
for (const name of ['content-length', 'transfer-encoding', 'content-encoding', 'expect']) {
headers[name] = undefined
}

const url = `/__nuxt_content/${collection}/${path}`
const fetchOptions = {
Expand Down
55 changes: 55 additions & 0 deletions test/unit/fetchContent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { H3Event } from 'h3'
import { fetchDatabase, fetchQuery } from '../../src/runtime/internal/api'

vi.mock('h3', async importOriginal => ({
...(await importOriginal<typeof import('h3')>()),
getRequestHeaders: () => ({
'content-length': '700000',
'transfer-encoding': 'chunked',
'content-encoding': 'gzip',
'expect': '100-continue',
'cookie': 'a=b',
'authorization': 'Bearer token',
}),
}))

const $fetch = vi.fn().mockResolvedValue([])
const event = { $fetch } as unknown as H3Event

describe('fetchContent', () => {
beforeEach(() => {
$fetch.mockClear()
})

const expectForwardedHeaders = (headers: Record<string, string | undefined>) => {
expect(headers['content-length']).toBeUndefined()
expect(headers['transfer-encoding']).toBeUndefined()
expect(headers['content-encoding']).toBeUndefined()
expect(headers.expect).toBeUndefined()
expect(headers.cookie).toBe('a=b')
expect(headers.authorization).toBe('Bearer token')
}

it('does not forward body headers of the incoming request to the query endpoint', async () => {
await fetchQuery(event, 'test', 'SELECT 1')

expect($fetch).toHaveBeenCalledTimes(1)
const [url, options] = $fetch.mock.calls[0]!
expect(url).toBe('/__nuxt_content/test/query')
expect(options.method).toBe('POST')
expect(options.body).toEqual({ sql: 'SELECT 1' })
expectForwardedHeaders(options.headers)
expect(options.headers['content-type']).toBe('application/json')
})

it('does not forward body headers of the incoming request to the dump endpoint', async () => {
await fetchDatabase(event, 'test')

expect($fetch).toHaveBeenCalledTimes(1)
const [url, options] = $fetch.mock.calls[0]!
expect(url).toBe('/__nuxt_content/test/sql_dump.txt')
expectForwardedHeaders(options.headers)
expect(options.headers['content-type']).toBe('text/plain')
})
})
Loading