|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { matchesIfNoneMatch } from "../packages/vinext/src/server/http-conditional.js"; |
| 3 | + |
| 4 | +describe("matchesIfNoneMatch", () => { |
| 5 | + it("matches an identical entity tag", () => { |
| 6 | + expect(matchesIfNoneMatch('"asset"', '"asset"')).toBe(true); |
| 7 | + }); |
| 8 | + |
| 9 | + it("uses weak comparison in either direction", () => { |
| 10 | + expect(matchesIfNoneMatch('"asset"', 'W/"asset"')).toBe(true); |
| 11 | + expect(matchesIfNoneMatch('W/"asset"', '"asset"')).toBe(true); |
| 12 | + }); |
| 13 | + |
| 14 | + it("matches a validator within a comma-separated field value", () => { |
| 15 | + expect(matchesIfNoneMatch('"other", W/"asset", "last"', '"asset"')).toBe(true); |
| 16 | + }); |
| 17 | + |
| 18 | + it("does not split commas inside an opaque tag", () => { |
| 19 | + // RFC 9110 permits commas in opaque tags. This is intentionally stricter |
| 20 | + // than Next.js 16.2.7's compiled `fresh` parser, which splits every comma. |
| 21 | + expect(matchesIfNoneMatch('"other", W/"asset,part"', '"asset,part"')).toBe(true); |
| 22 | + expect(matchesIfNoneMatch('"asset,part"', '"asset"')).toBe(false); |
| 23 | + }); |
| 24 | + |
| 25 | + it("treats backslashes as opaque characters rather than escapes", () => { |
| 26 | + expect(matchesIfNoneMatch('W/"asset\\part"', '"asset\\part"')).toBe(true); |
| 27 | + }); |
| 28 | + |
| 29 | + it("matches a wildcard with optional whitespace", () => { |
| 30 | + expect(matchesIfNoneMatch(" \t * \t ", 'W/"asset"')).toBe(true); |
| 31 | + }); |
| 32 | + |
| 33 | + it("rejects a wildcard mixed into an entity-tag list", () => { |
| 34 | + expect(matchesIfNoneMatch('*, "asset"', '"asset"')).toBe(false); |
| 35 | + expect(matchesIfNoneMatch('"other", *', '"asset"')).toBe(false); |
| 36 | + }); |
| 37 | + |
| 38 | + it("does not match different opaque tags or case", () => { |
| 39 | + expect(matchesIfNoneMatch('"other"', '"asset"')).toBe(false); |
| 40 | + expect(matchesIfNoneMatch('"ASSET"', '"asset"')).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it("does not treat a lowercase weak prefix as W/", () => { |
| 44 | + expect(matchesIfNoneMatch('w/"asset"', '"asset"')).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it("rejects malformed entity tags", () => { |
| 48 | + expect(matchesIfNoneMatch('asset, "match"', '"match"')).toBe(false); |
| 49 | + expect(matchesIfNoneMatch('"match", garbage', '"match"')).toBe(false); |
| 50 | + expect(matchesIfNoneMatch('"unterminated', '"unterminated"')).toBe(false); |
| 51 | + expect(matchesIfNoneMatch('W/ "asset"', '"asset"')).toBe(false); |
| 52 | + expect(matchesIfNoneMatch('"asset" suffix', '"asset"')).toBe(false); |
| 53 | + expect(matchesIfNoneMatch('"asset\x7fpart"', '"asset\x7fpart"')).toBe(false); |
| 54 | + }); |
| 55 | + |
| 56 | + it("ignores empty list members", () => { |
| 57 | + expect(matchesIfNoneMatch(' , W/"asset", ', '"asset"')).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it("rejects an invalid current entity tag", () => { |
| 61 | + expect(matchesIfNoneMatch('"asset"', "asset")).toBe(false); |
| 62 | + expect(matchesIfNoneMatch('"asset"', 'W/ "asset"')).toBe(false); |
| 63 | + }); |
| 64 | + |
| 65 | + it("rejects an absent or empty field value", () => { |
| 66 | + expect(matchesIfNoneMatch(undefined, '"asset"')).toBe(false); |
| 67 | + expect(matchesIfNoneMatch("", '"asset"')).toBe(false); |
| 68 | + }); |
| 69 | +}); |
0 commit comments