Files
cloudflare__vinext/tests/http-date.test.ts
T
Boy Steven 3d1ab6bd50 fix(server): honor static freshness validators (#2715)
* fix(server): honor static freshness validators

* fix(server): preserve content type on cached 416 responses

* fix(cloudflare): preserve partial asset response status

* docs(server): clarify static range representation

* fix(server): align cached precondition headers

* fix(server): align slow-path not-modified headers

* fix(server): adjust RFC 850 leap-day centuries

* fix(server): align static range and method handling

* fix(server): match weak static etags for If-Match

---------

Co-authored-by: James <james@eli.cx>
2026-07-27 17:39:22 +01:00

44 lines
1.5 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { parseHttpDate } from "../packages/vinext/src/server/http-date.js";
describe("parseHttpDate", () => {
const timestamp = Date.parse("2026-01-01T00:00:00Z");
it("parses all three RFC 9110 HTTP-date formats", () => {
expect(parseHttpDate("Thu, 01 Jan 2026 00:00:00 GMT")).toBe(timestamp);
expect(parseHttpDate("Thursday, 01-Jan-26 00:00:00 GMT")).toBe(timestamp);
expect(parseHttpDate("Thu Jan 1 00:00:00 2026")).toBe(timestamp);
});
it("rejects non-HTTP dates and normalized calendar or weekday mistakes", () => {
expect(parseHttpDate("2026-01-01T00:00:00Z")).toBeNaN();
expect(parseHttpDate("Sun, 31 Feb 2099 00:00:00 GMT")).toBeNaN();
expect(parseHttpDate("Fri, 01 Jan 2026 00:00:00 GMT")).toBeNaN();
});
it("resolves an RFC 850 year using the full 50-year timestamp boundary", () => {
vi.useFakeTimers();
vi.setSystemTime("2026-07-27T12:00:00Z");
try {
expect(parseHttpDate("Thursday, 31-Dec-76 00:00:00 GMT")).toBeNaN();
expect(parseHttpDate("Friday, 31-Dec-76 00:00:00 GMT")).toBe(
Date.parse("1976-12-31T00:00:00Z"),
);
} finally {
vi.useRealTimers();
}
});
it("adjusts an RFC 850 leap day before validating the inferred century", () => {
vi.useFakeTimers();
vi.setSystemTime("2026-07-27T12:00:00Z");
try {
expect(parseHttpDate("Tuesday, 29-Feb-00 00:00:00 GMT")).toBe(
Date.parse("2000-02-29T00:00:00Z"),
);
} finally {
vi.useRealTimers();
}
});
});