feat: add stable response format output via optional template index#181
Conversation
The /plain, /text, /html, /xml, and /json endpoints now accept an optional 1-based index parameter (e.g. /json/1) that returns a fixed template, so repeated calls produce byte-identical responses that can be compared. Omitting the index keeps the existing random behavior, and an out-of-range or non-integer index returns a 404 with the valid range. Timestamps embedded in the JSON and XML templates are now fixed literals so indexed output is stable across server restarts. Fixes #180 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Xy1LT9B3FV7ugKLR1uWodh
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #181 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 41 41
Lines 1310 1310
Branches 261 261
=========================================
Hits 1310 1310 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 241de51c44
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Returning the module-level template object directly meant a payload- mutating preSerialization hook on a consumer's Fastify instance could permanently contaminate later responses for the same index. Clone the template per request and add a regression test with a mutating hook. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Xy1LT9B3FV7ugKLR1uWodh
There was a problem hiding this comment.
Code Review
This pull request introduces support for an optional 1-based template index parameter (e.g., /:index?) across response format routes (/plain, /text, /html, /xml, /json) to allow stable, repeatable outputs. To support this, dynamic timestamps in mock templates have been replaced with static values, and JSON templates are now static objects. Schema definitions, route handlers, and comprehensive tests have been updated accordingly. Feedback on the changes suggests improving the resolveTemplateIndex helper to gracefully handle empty or whitespace-only strings (such as from trailing slashes) by falling back to a random template instead of returning a 404 error.
/json/ resolves the optional index param to an empty string; lock in the intended 404 so a malformed index never silently falls back to a random template. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Xy1LT9B3FV7ugKLR1uWodh
Please check if the PR fulfills these requirements
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Feature — fixes #180.
httpbin's
/jsonoutput was stable between calls, which made responses comparable; mockhttp randomizes it. This PR adds the/json/1,/json/2, … endpoints suggested in the issue, applied consistently across all five content-format endpoints./plain,/text,/html,/xml, and/jsonnow accept an optional 1-based template index (e.g./json/1…/json/5,/plain/1…/plain/10) that returns that fixed template, so repeated calls produce byte-identical output./jsonetc. are fully backward compatible.404with{"error": "Template index must be between 1 and N"}rather than silently falling back to random output.new Date()) are now fixed literals — without this, a fixed index still wouldn't be byte-stable across requests or server replicas.🤖 Generated with Claude Code
https://claude.ai/code/session_01Xy1LT9B3FV7ugKLR1uWodh
Generated by Claude Code