If one URL can return either HTML or JSON, test its Accept-header contract before changing canonical tags or caching. A browser navigation, an API client and a generic request may legitimately ask for different representations. The failure is an undocumented or inconsistent selection that serves the wrong resource for the intended task.
The useful artifact is a header-to-representation matrix plus a cache-order test. The query identity review varies URL parameters;this exercise holds the URI steady and varies a request header that can affect selection.
Do not assume JSON is universally unindexable or that a particular media type guarantees search visibility. Decide what the ordinary public page should serve, what the API contract requires and which responses the shared cache may reuse.
Define a small fictional negotiation contract
Suppose a fictional public URL /guide/connect supports an HTML guide and a JSON summary. The owner’s approved contract selects HTML for an ordinary browser navigation, an absent Accept header or a wildcard preference. An explicit application/json request selects the JSON representation. An unsupported-only request receives 406 under this fictional design.
RFC 9110 defines Accept, content negotiation, Content-Type and Vary. Its rules do not require every application to implement this particular selection policy. In particular, the server’s handling of an unacceptable preference must be understood from the actual implementation rather than guessed from this table.
Both representations describe the same fictional guide, but they are not identical response bytes. Give each a stable test marker and media type. Keep authentication, language, content encoding and URI constant while isolating the Accept decision.
Write exact request and response expectations
Use simple single-type requests first, then add real supported preference lists and quality values after the basic contract passes. A test that changes several headers at once can make a representation mismatch hard to attribute.
| Accept input | Expected fixture response | Required identity |
|---|---|---|
| Absent | 200;text/html | HTML_CONNECT heading and approved instruction |
| */* | 200;text/html | Same approved HTML representation |
| text/html | 200;text/html | HTML_CONNECT |
| application/json | 200;application/json | JSON guide identifier connect and matching approved summary |
| image/png only | 406 under this contract | Truthful unavailable-representation response |
Record the complete actual Content-Type, including any parameters, and inspect the body. A header claiming text/html around a JSON payload does not satisfy the HTML row. A generic error object with a matching JSON media type does not satisfy the guide-summary row.
Test quality values without a naive substring rule
If the application supports standard preference negotiation, add a request that explicitly makes one representation unacceptable with quality zero. A naive implementation that merely searches the header for application/json can select it even when the caller has rejected that type.
Record the exact supported precedence behavior for quality values, specificity and wildcards under the chosen implementation. Use a maintained parser or framework facility that matches the contract rather than a few ad hoc string contains checks. The fixture’s expected result must be independently written from the documented rules.
For the fictional policy, add the exact input Accept: application/json;q=0, text/html;q=1 and expect the HTML guide. Then reverse the accepted preference using application/json;q=1, text/html;q=0 and expect the JSON summary. Preserve both values in the fixture, including the zeroes. A substring-based selector that always prefers JSON will fail the first case even though the media-type name is present.
Also decide how malformed or excessively complex inputs are handled under the application’s ordinary validation policy. Do not invent a permissive fallback that changes the public response unpredictably. Keep such tests local and harmless;this is not an invitation to stress a production header parser.
Warm the cache in both directions
When selection depends on Accept, a cache needs a correct way to distinguish reusable responses. Vary identifies request fields that influenced selection under HTTP caching rules. Its presence alone does not prove the deployed cache actually uses the intended key or stores the right response.
In an authorized test environment, request JSON first and then HTML from the same URI. Verify the second response’s actual media type and content. Reset the controlled cache state, then request HTML first and JSON second. Both orders must preserve the approved representations.
Include wildcard and absent-header requests under the stated contract. Do not infer equivalence from a successful status. A cache that serves JSON into a browser route can return 200 throughout while omitting the public guide. Preserve cache-state setup, delivery layer and any observable hit evidence rather than assuming every second request was a cache hit.
Align metadata with the selected representation
Inspect HTML canonical declarations only in the actual HTML representation. If another representation uses an HTTP Link relationship, record its exact meaning and source. Do not copy an HTML tag into a JSON body and assume it has the same effect.
Use the canonical signal matrix when declarations disagree. Google’s canonicalization guidance describes ways to communicate preferred URLs, but canonical hints cannot repair a cache that delivers the wrong media type.
Check the ordinary anonymous public navigation through the real delivery path after release. The rendered-page inspection can confirm that the approved explanation remains visible when the browser receives the selected document.
Approve selection and reuse together
The release record should include the selected policy, exact header fixtures, actual response identities, quality-value cases and both cache warming orders. State untested client contexts instead of claiming universal compatibility.
Retain a deliberate wrong-variant cache fixture that fails the body marker or media-type assertion. This catches a regression even when every request still returns 200. Preserve the unsupported representation case so the handler does not silently become an unrelated asset fallback.
Approve when ordinary navigation and documented API requests each obtain their intended representation, and the tested cache does not substitute one for the other. Later crawl and indexing observations remain separate. The result is a coherent public HTTP contract, not a claim that content negotiation itself improves rankings.
