Skip to content

breaking: use http types in HttpMockRequest - #273

Open
danieleades wants to merge 2 commits into
httpmock:masterfrom
danieleades:agent/use-http-request-types
Open

danieleades wants to merge 2 commits into
httpmock:masterfrom
danieleades:agent/use-http-request-types

Conversation

@danieleades

@danieleades danieleades commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

This is the request half of #259, split out as requested.

HttpMockRequest currently stores its URI, method, headers, scheme and version as strings, then parses or rebuilds the corresponding http types whenever they are used. This stores the typed values once and validates them at the conversion boundary instead.

The main benefit is maintainability: there are fewer repeated conversions, fewer panic paths, and the internal state is correct by construction. Accessors can borrow the stored values instead of allocating replacements. It also fixes IPv6 authority handling by using http::uri::Authority, so this supersedes #208. For absolute-form targets the URI authority wins over Host; origin-form targets still use Host.

The existing text wire format is preserved. Opaque header values now have a byte-safe representation, so they can still appear in remote verification diagnostics rather than causing serialization to fail. Serialization borrows the typed request where possible, avoiding per-header copies.

Excluding tests, changelog and documentation changes, this replaces roughly 255 production lines with 321: a net increase of about 66 lines. The extra code is mainly the explicit, byte-safe wire conversion and the typed public conversion error; the in-memory request model itself is simpler.

Breaking changes:

  • uri(), scheme(), method() and headers() now borrow their corresponding http types, and host() returns Option<&str>. uri_str() and headers_vec() are removed, since they borrow strings the request no longer stores. The other existing accessors (method_str(), query_params(), query_param_length(), body_string(), body_ref(), body_vec(), body_bytes(), version_ref(), and to_http_request() as restored in Restore selected 0.8.3 APIs and add HTTP response conversion #326) are kept as thin wrappers over the typed values.
  • Converting an http::Request into HttpMockRequest now uses TryFrom with the public HttpMockRequestConversionError. Conversions back to Request<Bytes>, Request<String> and Request<()> are kept.
  • RequestMetadata::new and its public scheme field now use http::uri::Scheme rather than &'static str.

Boxed byte bodies remain supported after rebasing onto the upstream conversion fix. Request<()> remains supported for empty incoming requests.

Checks:

  • cargo test
  • cargo test --all-features --lib
  • cargo clippy --all-targets --all-features
  • cargo fmt --all -- --check

Rebase validation:

  • cargo +stable clippy --offline --all-targets --all-features
  • cargo +stable clippy --offline --all-targets --no-default-features
  • cargo +stable test --offline --all-features --lib
  • cargo +stable test --offline --all-features --test lib body_conversion_tests — all four boxed-body regression tests pass, adapted to this PR's API

@codecov

codecov Bot commented Aug 9, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.14040% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/common/data.rs 99.34% 2 Missing ⚠️
src/server/state.rs 93.75% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@danieleades
danieleades marked this pull request as ready for review August 9, 2026 20:05
@danieleades
danieleades force-pushed the agent/use-http-request-types branch 5 times, most recently from d183023 to 40359f3 Compare August 16, 2026 12:38
@danieleades
danieleades force-pushed the agent/use-http-request-types branch from 40359f3 to 66741bf Compare August 17, 2026 18:51
@danieleades
danieleades force-pushed the agent/use-http-request-types branch 2 times, most recently from 49b8645 to dc39bf8 Compare September 20, 2026 15:36
@alexliesenfeld

Copy link
Copy Markdown
Member

I considered once myself to use the data structures of the http crate, but in my view http is not meant for simplicity, it's for correctness. For a testing library, I felt it overcomplicates things. What was earlier a short line in code, becomes a multiline statement and there I felt it's not worth the change. Especially because our methods already accept and convert from the http creates structures, so we are compatible with it where makes sense and if the user already uses them.

@danieleades

Copy link
Copy Markdown
Contributor Author

I considered once myself to use the data structures of the http crate, but in my view http is not meant for simplicity, it's for correctness. For a testing library, I felt it overcomplicates things. What was earlier a short line in code, becomes a multiline statement and there I felt it's not worth the change. Especially because our methods already accept and convert from the http creates structures, so we are compatible with it where makes sense and if the user already uses them.

this crate already exposes the http methods in the public APIs, but it did an awkward conversion back and forth to stringly-typed internal types. That lead to some hidden, implicit cloning as well as some genuine correctness bugs (including not supporting IPv6).

Some of my changes do go a bit deeper and push more onus onto the user in the pursuit of a smaller, more focused core library.

Some of the ergonomic changes are due to a change in ownership and are unavoidable (but worth it, in my opinion- and i'm not sure forcing callers to explicitly clone instead of implicitly cloning is a downgrade) while other conveniences could be reintroduced.

Would you like me to try and find a halfway house?

@danieleades
danieleades force-pushed the agent/use-http-request-types branch 2 times, most recently from 33408bc to 9b5111d Compare September 30, 2026 07:37
@FalkWoldmann FalkWoldmann added this to the 0.10 milestone Oct 20, 2026
@FalkWoldmann

Copy link
Copy Markdown
Member

Deferring this to 0.10 so we can get 0.9 out, added it to the milestone

@danieleades
danieleades force-pushed the agent/use-http-request-types branch from 9b5111d to 41dcf25 Compare October 20, 2026 19:05

This branch has not been deployed

No deployments
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.

3 participants