Skip to content

Commit cec1282

Browse files
committed
test(plugin-hono-server): pin which 304 answers carry Vary: Accept-Encoding
Claude-Session: https://claude.ai/code/session_016WV4cxHwXNcGcAHNerZsom Co-authored-by: Claude <noreply@anthropic.com>
1 parent ba35dfd commit cec1282

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎packages/plugins/plugin-hono-server/src/response-compression.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,41 @@ describe('HonoHttpServer response compression', () => {
229229
expect(head.headers['vary']).toMatch(/accept-encoding/i);
230230
});
231231

232+
// A `304` is never encoded, and it carries `Vary: Accept-Encoding` exactly
233+
// when it carries a compressible `Content-Type`: the middleware judges the
234+
// answer it is handed and cannot see the `200` the `304` stands for.
235+
it('gives a 304 Vary: Accept-Encoding only when it carries a compressible Content-Type, and never encodes it', async () => {
236+
const etag = '"v1"';
237+
const port = await boot((s) => {
238+
// The Console's stable-file shape: `ETag` and `Cache-Control`, no `Content-Type`.
239+
s.getRawApp().get('/bare-304', () => new Response(null, {
240+
status: 304,
241+
headers: { etag, 'cache-control': 'no-cache' },
242+
}));
243+
// The dispatcher's shape (the translation bundle): `res.json()` of no body.
244+
s.get('/json-304', (_req, res) => {
245+
res.status(304).header('ETag', etag);
246+
res.json(undefined);
247+
});
248+
});
249+
250+
const bare = await get(port, '/bare-304', { 'accept-encoding': 'gzip, br', 'if-none-match': etag });
251+
expect(bare.status).toBe(304);
252+
expect(bare.headers['content-type']).toBeUndefined();
253+
expect(bare.headers['vary']).toBeUndefined();
254+
expect(bare.headers['content-encoding']).toBeUndefined();
255+
expect(bare.headers['etag']).toBe(etag);
256+
expect(bare.body.byteLength).toBe(0);
257+
258+
const typed = await get(port, '/json-304', { 'accept-encoding': 'gzip, br', 'if-none-match': etag });
259+
expect(typed.status).toBe(304);
260+
expect(typed.headers['content-type']).toMatch(/^application\/json/);
261+
expect(typed.headers['vary']).toMatch(/accept-encoding/i);
262+
expect(typed.headers['content-encoding']).toBeUndefined();
263+
expect(typed.headers['etag']).toBe(etag);
264+
expect(typed.body.byteLength).toBe(0);
265+
});
266+
232267
it('survives a client that leaves in the middle of an encoded answer', async () => {
233268
// Text that compresses, but not to nothing: the encoder still has
234269
// output pending when the client goes away.

0 commit comments

Comments
 (0)