Commit a09167cb authored by Jiachi Liu's avatar Jiachi Liu

update test

parent 5ee6ebb0
/** /**
* App Route handler that: * App Route handler that reads request headers (tests NextRequest header conversion)
* 1. Reads request headers (tests NextRequest header conversion)
* 2. Sets response headers with non-ASCII values (tests outgoing header handling)
*/ */
export async function GET(request) { export async function GET(request) {
// Read incoming request headers // Read incoming request headers
...@@ -12,15 +10,9 @@ export async function GET(request) { ...@@ -12,15 +10,9 @@ export async function GET(request) {
} }
} }
// Create response with non-ASCII header values return new Response(JSON.stringify(requestHeaders), {
const response = new Response(JSON.stringify(requestHeaders), {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
// Set non-ASCII response headers to verify they work
'x-response-city': 'Montréal',
'x-response-country': 'Österreich',
}, },
}) })
return response
} }
...@@ -269,15 +269,13 @@ describe('Middleware Non-ASCII Headers', () => { ...@@ -269,15 +269,13 @@ describe('Middleware Non-ASCII Headers', () => {
// ============================================================ // ============================================================
// App Route Handler Tests // App Route Handler Tests
// These test fromNodeOutgoingHttpHeaders in NextRequest adapter // Test that App Route handlers can read headers correctly
// ============================================================ // ============================================================
describe('App Route Handler', () => { describe('App Route Handler', () => {
// App Route handlers should see properly decoded headers (same as middleware)
// fromNodeOutgoingHttpHeaders applies mojibake recovery without encoding
it('should read non-ASCII request headers correctly in route handler', async () => { it('should read non-ASCII request headers correctly in route handler', async () => {
// Test that App Route handlers can read mojibake headers correctly // Test that App Route handlers can read mojibake headers correctly
// fromNodeOutgoingHttpHeaders decodes mojibake but doesn't encode by default // The middleware encodes non-ASCII, server decodes them before route handler
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
next.url, next.url,
'/api/route-headers', '/api/route-headers',
...@@ -297,26 +295,7 @@ describe('Middleware Non-ASCII Headers', () => { ...@@ -297,26 +295,7 @@ describe('Middleware Non-ASCII Headers', () => {
expect(body['x-country']).toBe('Österreich') expect(body['x-country']).toBe('Österreich')
}) })
// Response headers with non-ASCII values arrive as mojibake
// (UTF-8 bytes interpreted as Latin-1 by HTTP transport)
it('should show that response headers with non-ASCII arrive as mojibake', async () => {
// Test that route handlers setting non-ASCII response headers
// have those headers arrive as mojibake to the client
const res = await fetchViaHTTP(next.url, '/api/route-headers', {}, {})
expect(res.status).toBe(200)
// Response headers with non-ASCII values get mangled during HTTP transport
// UTF-8 bytes are interpreted as Latin-1 (mojibake)
const cityHeader = res.headers.get('x-response-city')
const countryHeader = res.headers.get('x-response-country')
// These arrive as mojibake: "Montréal" → "Montréal", "Österreich" → "Ãsterreich"
expect(cityHeader).toBe(toMojibake('Montréal'))
expect(countryHeader).toBe(toMojibake('Österreich'))
})
it('should preserve intentional percent-encoding in route handler', async () => { it('should preserve intentional percent-encoding in route handler', async () => {
// Test that intentional percent-encoding is not double-encoded or decoded
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
next.url, next.url,
'/api/route-headers', '/api/route-headers',
...@@ -331,7 +310,6 @@ describe('Middleware Non-ASCII Headers', () => { ...@@ -331,7 +310,6 @@ describe('Middleware Non-ASCII Headers', () => {
expect(res.status).toBe(200) expect(res.status).toBe(200)
const body = await res.json() const body = await res.json()
// Intentional percent-encoding should be preserved
expect(body['x-path']).toBe('/hello%2Fworld') expect(body['x-path']).toBe('/hello%2Fworld')
expect(body['x-query']).toBe('foo%20bar') expect(body['x-query']).toBe('foo%20bar')
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment