fix(http): a handler-set ETag array is still the handler's (CARD-15)
With `etag: true`, `tagged()` only recognised a handler ETag given as a string. Given as an array, it compared If-None-Match against the body's computed tag while sending the handler's array, so a client revalidating with the tag it was sent got a 200 instead of a 304, and two ETag values went on the wire. ETag is single-valued, so the last value of an array wins, as a repeated `setHeader` would. It is sent alone and drives the comparison. Found in the pre-freeze integrated review. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+18
-2
No files matched your search
+4
-2
@@ -191,8 +191,10 @@ function tagged(
|
|||||||
): Buffered {
|
): Buffered {
|
||||||
const key = findHeader(result.headers, 'etag');
|
const key = findHeader(result.headers, 'etag');
|
||||||
const existing = key === undefined ? undefined : result.headers[key];
|
const existing = key === undefined ? undefined : result.headers[key];
|
||||||
const etag = typeof existing === 'string' ? existing : bodyEtag(result.body);
|
// ETag is single-valued: from an array, the last value wins, as a repeated `setHeader` would.
|
||||||
const headers = key === undefined ? { ...result.headers, etag } : result.headers;
|
const own = Array.isArray(existing) ? existing[existing.length - 1] : existing;
|
||||||
|
const etag = own ?? bodyEtag(result.body);
|
||||||
|
const headers = { ...result.headers, [key ?? 'etag']: etag };
|
||||||
|
|
||||||
if (isNotModified(req.headers, etag)) return bodyless({ kind: 'buffered', status: 304, headers, body: '' });
|
if (isNotModified(req.headers, etag)) return bodyless({ kind: 'buffered', status: 304, headers, body: '' });
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,12 @@ class Handlers {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('/own-list')
|
||||||
|
@Transformer(() => ({ headers: { ETag: ['"v6"', '"v7"'] }, body: 'v7' }))
|
||||||
|
ownList() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Post('/post')
|
@Post('/post')
|
||||||
post() {
|
post() {
|
||||||
return { n: 1 };
|
return { n: 1 };
|
||||||
@@ -182,6 +188,14 @@ describe('handlers (etag: true)', () => {
|
|||||||
expect(res.headers.get('etag')).toBe('"v7"');
|
expect(res.headers.get('etag')).toBe('"v7"');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("T6: a handler-set ETag array is still the handler's: the last value is sent and compared", async () => {
|
||||||
|
const first = await req(etagApp, '/h/own-list');
|
||||||
|
expect(first.headers.get('etag')).toBe('"v7"');
|
||||||
|
const res = await req(etagApp, '/h/own-list', { headers: { 'if-none-match': '"v7"' } });
|
||||||
|
expect(res.status).toBe(304);
|
||||||
|
expect(res.headers.get('etag')).toBe('"v7"');
|
||||||
|
});
|
||||||
|
|
||||||
it('T7/T8: a list containing the tag, and *', async () => {
|
it('T7/T8: a list containing the tag, and *', async () => {
|
||||||
const etag = (await req(etagApp, '/h/json')).headers.get('etag')!;
|
const etag = (await req(etagApp, '/h/json')).headers.get('etag')!;
|
||||||
const list = await req(etagApp, '/h/json', { headers: { 'if-none-match': `"a", ${etag}, "c"` } });
|
const list = await req(etagApp, '/h/json', { headers: { 'if-none-match': `"a", ${etag}, "c"` } });
|
||||||
|
|||||||
Reference in new issue
Block a user