Skip to content

feat: add stable response format output via optional template index#181

Merged
jaredwray merged 3 commits into
mainfrom
claude/mockhttp-issue-180-cbvw2y
Jul 22, 2026
Merged

feat: add stable response format output via optional template index#181
jaredwray merged 3 commits into
mainfrom
claude/mockhttp-issue-180-cbvw2y

Conversation

@jaredwray

Copy link
Copy Markdown
Owner

Please check if the PR fulfills these requirements

  • Followed the Contributing and Code of Conduct guidelines.
  • Tests for the changes have been added (for bug fixes/features) with 100% code coverage.

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Feature — fixes #180.

httpbin's /json output 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 /json now 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.
  • Omitting the index keeps the existing random behavior, so /json etc. are fully backward compatible.
  • An out-of-range or non-integer index returns 404 with {"error": "Template index must be between 1 and N"} rather than silently falling back to random output.
  • The live timestamps embedded in the JSON and XML templates (new Date()) are now fixed literals — without this, a fixed index still wouldn't be byte-stable across requests or server replicas.
  • Swagger schemas updated (index param + 404 response), so the generated API docs describe the new behavior.
  • Added tests covering stable output per index, all HTTP methods, distinct templates per index, and invalid-index handling for every endpoint; coverage remains at 100% lines/functions.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xy1LT9B3FV7ugKLR1uWodh


Generated by Claude Code

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

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (7022f37) to head (0df4d22).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/routes/response-formats/index.ts Outdated
Comment thread src/routes/response-formats/index.ts
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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/routes/response-formats/index.ts
/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
@jaredwray
jaredwray merged commit 8868343 into main Jul 22, 2026
11 checks passed
@jaredwray
jaredwray deleted the claude/mockhttp-issue-180-cbvw2y branch July 22, 2026 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a way to have a stable response format output

2 participants