Skip to content

feat: support json_body and body_from_file in static mock YAML files - #299

Merged
alexliesenfeld merged 3 commits into
masterfrom
static-yaml-response-bodies
Sep 2, 2026
Merged

alexliesenfeld merged 3 commits into
masterfrom
static-yaml-response-bodies

Conversation

@alexliesenfeld

Copy link
Copy Markdown
Member

Problem

The static mock YAML format silently dropped json_body and body_from_file because StaticHTTPResponse did not define these fields, so Serde discarded them and the mock responded without a body.

Closes #118
Closes #185

Changes

  • json_body is converted to the runtime response body. A YAML string value is parsed as JSON (matching the examples in the issues), structured YAML values are serialized as JSON. No content-type header is added, consistent with the Rust Then::json_body API.
  • body_from_file is resolved by the static mock directory loader relative to the YAML file that references it. The resolved path must stay inside the configured static mock directory: absolute paths, .. escapes, directories, and symlinks escaping the directory are rejected. The file is read once at load time.
  • Outside the static directory loader (e.g. mocks imported from a recording via the HTTP API), body_from_file is rejected with a clear error. Recording export never emits json_body or body_from_file.

Design notes

File resolution lives entirely in server/persistence.rs: the loader takes the body_from_file reference out of the parsed definition, converts the rest with the ordinary IO-free conversion in common/data.rs, then reads the file and sets the runtime response body. The response body field precedence (json_body > body > body_base64, with a resolved file reference always winning) is documented and implemented in one place.

Tests

Unit tests cover the json_body conversion, path confinement for body_from_file (including .. and symlink escapes), and the rejection of body_from_file outside the static mock directory loader.

@codecov

codecov Bot commented Sep 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.65079% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/server/persistence.rs 93.00% 7 Missing ⚠️
src/common/data.rs 96.15% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

The static mock YAML format silently dropped json_body and body_from_file
because StaticHTTPResponse did not define these fields, leaving responses
without a body.

- json_body is converted to the runtime response body. A YAML string value
  is parsed as JSON, matching the documented examples.
- body_from_file is resolved by the static mock directory loader relative
  to the YAML file. The resolved path must stay inside the configured
  static mock directory; absolute paths, .. escapes, directories, and
  symlinks escaping the directory are rejected. Outside the loader (e.g.
  recordings imported via the HTTP API), body_from_file is rejected with a
  clear error, and recording export never emits it.
@alexliesenfeld
alexliesenfeld force-pushed the static-yaml-response-bodies branch from ccdd1f5 to 72cc84a Compare September 2, 2026 19:53
@alexliesenfeld
alexliesenfeld merged commit af2e9dd into master Sep 2, 2026
15 checks passed
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.

body_from_file does not work in Standalone mock server json_body doesn't seem to be working for YAML mocks (standalone)

1 participant