Skip to content

Fix 6 issues: WithBody matching, ILoggerFactory overloads, thread safety, fire-and-forget fix, terminal methods return IRequestStub - #127

Merged
hibri merged 3 commits into
masterfrom
copilot/review-repo-for-next-steps
Apr 16, 2026
Merged

hibri merged 3 commits into
masterfrom
copilot/review-repo-for-next-steps

Conversation

Copilot AI commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes all 6 issues identified in the prior review session. The package version remains at 3.0.0. To publish to NuGet, create a GitHub release tagged v3.0.0 — this triggers the publish.yml workflow which builds, tests, packs and pushes both HttpMock and HttpMock.Verify.NUnit.


Changes

1. IRequestStub terminal methods now return IRequestStub

OK(), NotFound(), and WithStatus() previously returned void, preventing any fluent chaining after the status call. They now return this, making chains like .OK().WithDelay(500) valid. This also fixes the broken WithDelay example in the README.

2. Request body matching for stubs (WithBody)

Added WithBody(string) and WithBody(Func<string, bool>) to IRequestStub / RequestHandler. The request body is now buffered synchronously in RequestProcessor.OnRequest (before matching) and passed down to HandleRequest as a pre-read string. RequestMatcher.Match filters by IRequestHandler.MatchesBody() alongside the existing URL / header / query checks — allowing multiple stubs on the same path+method to be distinguished by body content.

3. HttpMockRepository.At with ILoggerFactory

Added At(string, ILoggerFactory) and At(Uri, ILoggerFactory) overloads to HttpMockRepository, HttpServerFactory, and IHttpMockRepository. The logger flows through to HttpServer and RequestProcessor.

4. Silent fire-and-forget exception loss fixed

HandleRequest task is now chained with .ContinueWith(t => _log.LogError(t.Exception?.InnerException ?? t.Exception, ...), OnlyOnFaulted) so any exception after an await is logged rather than silently swallowed.

5. HttpServerFactory thread safety

Replaced bare Dictionary access with a private readonly object _serverLock + lock blocks. The Get method atomically checks availability and creates the server under the lock, preventing races when multiple tests share the same static factory.

6. README fixes

  • HttpMockRepository.At("...", loggerFactory) example is now backed by a real overload.
  • WithDelay chaining example after OK() is now valid (see fix 1).
  • New Matching by Request Body section documents WithBody.

Tests

  • 7 new integration tests in StubBodyConstraintTests.cs covering exact-string match, predicate match, multi-stub disambiguation by body, no-constraint fallback, and the At logger-factory overload.
  • RequestMatcherTests updated to stub the new MatchesBody member on mocked IRequestHandler.
  • 35 unit tests + 69 integration tests — all pass.

Copilot AI and others added 2 commits April 16, 2026 11:03
…ds, thread safety, fire-and-forget fix

Agent-Logs-Url: https://github.com/hibri/HttpMock/sessions/4e0be8ef-654f-4a47-8ca1-152473b652b0

Co-authored-by: hibri <122442+hibri@users.noreply.github.com>
try
{
using var reader = new StreamReader(requestBody, Encoding.UTF8, false, 4096, leaveOpen: false);
bufferedBody = reader.ReadToEnd();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot why is this 4096?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The 4096 was a leftover from when the code used the 5-argument constructor overload (new StreamReader(stream, encoding, detectBom, bufferSize, leaveOpen: true)) — leaveOpen: true was needed because the stream was passed further downstream at the time. Once we switched to leaveOpen: false (the default), the full overload was no longer needed. 4096 is also just the default StreamReader internal buffer size, so specifying it explicitly was redundant noise.

Simplified to new StreamReader(requestBody, Encoding.UTF8) in 41454f1.

@hibri
hibri marked this pull request as ready for review April 16, 2026 13:08
… size and leaveOpen params

Agent-Logs-Url: https://github.com/hibri/HttpMock/sessions/7f7beb41-da18-4255-b846-1fd6a2eff36a

Co-authored-by: hibri <122442+hibri@users.noreply.github.com>
Copilot AI requested a review from hibri April 16, 2026 13:11
@hibri
hibri merged commit 0a76fbc into master Apr 16, 2026
1 check passed
@hibri
hibri deleted the copilot/review-repo-for-next-steps branch April 16, 2026 13:38
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.

2 participants