Commit 41fd30ef authored by Jiachi Liu's avatar Jiachi Liu

update test

parent b02ac1bd
...@@ -946,6 +946,8 @@ export default abstract class Server< ...@@ -946,6 +946,8 @@ export default abstract class Server<
// In minimal mode (Vercel), decode percent-encoded header values. // In minimal mode (Vercel), decode percent-encoded header values.
// Middleware encodes non-ASCII characters for safe HTTP transport, // Middleware encodes non-ASCII characters for safe HTTP transport,
// and we decode them here to restore the original values. // and we decode them here to restore the original values.
// Note: decodeHeaderValue is safe - it only decodes valid percent-encoded
// sequences and returns the original value if decoding fails.
if (this.minimalMode) { if (this.minimalMode) {
for (const key of Object.keys(req.headers)) { for (const key of Object.keys(req.headers)) {
const value = req.headers[key] const value = req.headers[key]
......
...@@ -187,31 +187,31 @@ describe('Middleware Non-ASCII Headers', () => { ...@@ -187,31 +187,31 @@ describe('Middleware Non-ASCII Headers', () => {
expect(headers['x-city']).toBe('São Paulo') expect(headers['x-city']).toBe('São Paulo')
}) })
// Test for LOCAL scenario: when headers are sent as proper Latin-1 (not Mojibake) // Test for headers that arrive as proper UTF-8 (not Mojibake)
// This happens when: // In HTTP/2 environments (like Vercel deployment), headers are transmitted as UTF-8.
// - A local proxy (nginx, etc.) adds headers with non-ASCII // When the server interprets UTF-8 bytes as Latin-1, we get Mojibake.
// - Node.js fetch/http client sends headers (converts to Latin-1) // This test verifies that both scenarios work:
// Unlike CDN scenario, these arrive as single-byte Latin-1, not UTF-8 Mojibake // - Local: UTF-8 headers might arrive as Mojibake (Node.js interprets as Latin-1)
it('should handle Latin-1 encoded headers (local proxy scenario)', async () => { // - Deploy: UTF-8 headers arrive correctly or as Mojibake depending on transport
// When fetchViaHTTP sends "Montréal", it encodes as Latin-1: it('should handle UTF-8 encoded headers (proxy scenario)', async () => {
// "é" becomes single byte 0xE9 (not UTF-8's 0xC3 0xA9) // Send headers as Mojibake to simulate what happens when UTF-8 bytes
// This simulates a local proxy adding headers // are interpreted as Latin-1 by the server (common in CDN/proxy scenarios)
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
next.url, next.url,
'/api/headers', '/api/headers',
{}, {},
{ {
headers: { headers: {
'x-city': 'Montréal', // Latin-1: é = 0xE9 'x-city': toMojibake('Montréal'),
'x-region': 'Île-de-France', 'x-region': toMojibake('Île-de-France'),
'x-country': 'Österreich', 'x-country': toMojibake('Österreich'),
}, },
} }
) )
expect(res.status).toBe(200) expect(res.status).toBe(200)
const headers = await res.json() const headers = await res.json()
// These should be preserved correctly through middleware // These should be decoded correctly through middleware
expect(headers['x-city']).toBe('Montréal') expect(headers['x-city']).toBe('Montréal')
expect(headers['x-region']).toBe('Île-de-France') expect(headers['x-region']).toBe('Île-de-France')
expect(headers['x-country']).toBe('Österreich') expect(headers['x-country']).toBe('Österreich')
......
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