Skip to the code

Compare ETags

An entity tag (ETag) names one version of a file, like "menu-v7". The tea shop's server compares the tags a browser sends with the current one to decide between a full 200 and an empty 304 Not Modified. Write three functions, following RFC 9110.

An entity tag is an opaque tag in double quotes, like "menu-v7", optionally after the prefix W/ (a capital W, as the RFC's grammar requires), which marks a weak tag: one that changes when the file's meaning changes, not necessarily with every byte. Inside the quotes, any visible ASCII character except a double quote is allowed, commas included, and there's no escaping. Anything else, such as menu-v7 without quotes, w/"menu-v7", or W/ "menu-v7", isn't an entity tag, and never matches anything.

strongMatch(a, b) is the RFC's strong comparison: two tags match when neither is weak and their opaque tags are the same, character for character. weakMatch(a, b) is its weak comparison: the opaque tags are the same, whether either tag is weak or not. Both take tags exactly as written, without spaces around them.

ifNoneMatch(field, current) evaluates an If-None-Match header as RFC 9110 section 13.1.2 says. field is the header's value: *, or a list of entity tags separated by commas, with optional spaces and empty items. current is the current version's entity tag, or null when the file doesn't exist. Return false when the condition fails, which is when the server answers a GET with 304: field is * (with optional spaces around it) and the file exists, or a listed tag matches current by weak comparison, which the RFC requires here because a weak tag is good enough to reuse a cached copy. Otherwise return true. A * in a list with other items is invalid, and simply never matches.

strongMatch('"menu-v7"', '"menu-v7"'); // true
strongMatch('W/"menu-v7"', '"menu-v7"'); // false: one is weak
weakMatch('W/"menu-v7"', '"menu-v7"'); // true
ifNoneMatch('"menu-v6", W/"menu-v7"', '"menu-v7"'); // false: the browser has this version, so 304
ifNoneMatch("*", null); // true: there's no menu yet

What the tests check

  1. strong comparison matches only two strong tags with the same opaque tag
    expect(strongMatch('W/"1"', 'W/"1"')).toBe(false);
    expect(strongMatch('W/"1"', 'W/"2"')).toBe(false);
    expect(strongMatch('W/"1"', '"1"')).toBe(false);
    expect(strongMatch('"1"', 'W/"1"')).toBe(false);
    expect(strongMatch('"1"', '"1"')).toBe(true);
    expect(strongMatch('"1"', '"2"')).toBe(false);
    
  2. weak comparison ignores the W/ prefix
    expect(weakMatch('W/"1"', 'W/"1"')).toBe(true);
    expect(weakMatch('W/"1"', 'W/"2"')).toBe(false);
    expect(weakMatch('W/"1"', '"1"')).toBe(true);
    expect(weakMatch('"1"', 'W/"1"')).toBe(true);
    expect(weakMatch('"1"', '"1"')).toBe(true);
    
  3. tags compare character for character, and an empty tag is a tag
    expect(strongMatch('"Menu-v7"', '"menu-v7"')).toBe(false);
    expect(weakMatch('"menu-v7"', '"menu-v7-"')).toBe(false);
    expect(strongMatch('""', '""')).toBe(true);
    expect(weakMatch('W/""', '""')).toBe(true);
    
  4. only a quoted tag, with an optional capital W/, is an entity tag
    expect(strongMatch("menu-v7", "menu-v7")).toBe(false);
    expect(weakMatch('w/"menu-v7"', 'w/"menu-v7"')).toBe(false);
    expect(weakMatch('W/ "menu-v7"', '"menu-v7"')).toBe(false);
    expect(weakMatch('"menu"v7"', '"menu"v7"')).toBe(false);
    expect(weakMatch('"menu v7"', '"menu v7"')).toBe(false);
    
  5. If-None-Match fails when the current tag is listed, by weak comparison
    expect(ifNoneMatch('"menu-v7"', '"menu-v7"')).toBe(false);
    expect(ifNoneMatch('W/"menu-v7"', '"menu-v7"')).toBe(false);
    expect(ifNoneMatch('"menu-v7"', 'W/"menu-v7"')).toBe(false);
    expect(ifNoneMatch('"menu-v6"', '"menu-v7"')).toBe(true);
    
  6. If-None-Match reads a list with spaces and empty items, and a comma inside a tag doesn't split it
    expect(ifNoneMatch('"menu-v5",  "menu-v6" ,W/"menu-v7"', '"menu-v7"')).toBe(false);
    expect(ifNoneMatch('"green", , "mint",', '"mint"')).toBe(false);
    expect(ifNoneMatch('"green,mint", "oolong"', '"green,mint"')).toBe(false);
    expect(ifNoneMatch('"green,mint"', '"mint"')).toBe(true);
    
  7. * fails when the file exists, and passes when it doesn't
    expect(ifNoneMatch("*", '"menu-v7"')).toBe(false);
    expect(ifNoneMatch(" * ", 'W/"menu-v7"')).toBe(false);
    expect(ifNoneMatch("*", null)).toBe(true);
    expect(ifNoneMatch('"menu-v7"', null)).toBe(true);
    
  8. a * among other items, an empty header, and items that aren't tags never match
    expect(ifNoneMatch('*, "menu-v6"', '"menu-v7"')).toBe(true);
    expect(ifNoneMatch('"menu-v6", *', '"menu-v6"')).toBe(false);
    expect(ifNoneMatch("", '"menu-v7"')).toBe(true);
    expect(ifNoneMatch("menu-v7", '"menu-v7"')).toBe(true);
    expect(ifNoneMatch('w/"menu-v7"', '"menu-v7"')).toBe(true);
    

Learn more

solution.jsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).