Fix 6 issues: WithBody matching, ILoggerFactory overloads, thread safety, fire-and-forget fix, terminal methods return IRequestStub - #127
Merged
Conversation
…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>
Agent-Logs-Url: https://github.com/hibri/HttpMock/sessions/4e0be8ef-654f-4a47-8ca1-152473b652b0 Co-authored-by: hibri <122442+hibri@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
hibri
April 16, 2026 11:08
View session
hibri
reviewed
Apr 16, 2026
| try | ||
| { | ||
| using var reader = new StreamReader(requestBody, Encoding.UTF8, false, 4096, leaveOpen: false); | ||
| bufferedBody = reader.ReadToEnd(); |
Contributor
Author
There was a problem hiding this comment.
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
approved these changes
Apr 16, 2026
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>
hibri
approved these changes
Apr 16, 2026
hibri
approved these changes
Apr 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 taggedv3.0.0— this triggers thepublish.ymlworkflow which builds, tests, packs and pushes bothHttpMockandHttpMock.Verify.NUnit.Changes
1.
IRequestStubterminal methods now returnIRequestStubOK(),NotFound(), andWithStatus()previously returnedvoid, preventing any fluent chaining after the status call. They now returnthis, making chains like.OK().WithDelay(500)valid. This also fixes the brokenWithDelayexample in the README.2. Request body matching for stubs (
WithBody)Added
WithBody(string)andWithBody(Func<string, bool>)toIRequestStub/RequestHandler. The request body is now buffered synchronously inRequestProcessor.OnRequest(before matching) and passed down toHandleRequestas a pre-read string.RequestMatcher.Matchfilters byIRequestHandler.MatchesBody()alongside the existing URL / header / query checks — allowing multiple stubs on the same path+method to be distinguished by body content.3.
HttpMockRepository.AtwithILoggerFactoryAdded
At(string, ILoggerFactory)andAt(Uri, ILoggerFactory)overloads toHttpMockRepository,HttpServerFactory, andIHttpMockRepository. The logger flows through toHttpServerandRequestProcessor.4. Silent fire-and-forget exception loss fixed
HandleRequesttask is now chained with.ContinueWith(t => _log.LogError(t.Exception?.InnerException ?? t.Exception, ...), OnlyOnFaulted)so any exception after anawaitis logged rather than silently swallowed.5.
HttpServerFactorythread safetyReplaced bare
Dictionaryaccess with aprivate readonly object _serverLock+lockblocks. TheGetmethod 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.WithDelaychaining example afterOK()is now valid (see fix 1).WithBody.Tests
StubBodyConstraintTests.cscovering exact-string match, predicate match, multi-stub disambiguation by body, no-constraint fallback, and theAtlogger-factory overload.RequestMatcherTestsupdated to stub the newMatchesBodymember on mockedIRequestHandler.