Add response header configuration#131
Merged
Merged
Conversation
Add WithHeader(name, value) and WithHeader(name, params string[]) to RequestMockResponseBuilder. The mock's Responder is wrapped so configured headers are applied to every response. Content headers are routed to the response content headers, others to the response headers, using TryAddWithoutValidation. Includes XML docs, tests, updated approved API and docs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Coverage Report for CI Build 26691388414Coverage increased (+0.2%) to 84.624%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
On net8.0 HttpResponseMessage.Content is non-null by default, so the null-coalescing left operand is never null and InspectCode flags it. The check is only needed on net472 where Content defaults to null. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…-headers # Conflicts: # Mockly.Specs/HttpMockSpecs.cs
This was referenced Jun 1, 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.
Closes #113
Summary
Adds the ability to configure response headers on a mocked response, so consumers no longer need to drop down to hand-building an
HttpResponseMessageto set headers likeLocation,ETag,Cache-Control, or a customContent-Type.Two additive, fluent methods are added to
RequestMockResponseBuilder:Usage:
How it works
Responderis wrapped so that, after it produces theHttpResponseMessage, the configured headers are applied on every invocation.Content-Type,Content-Length,Content-Encoding,Content-Disposition,Expires,Last-Modified, etc.) are routed toresponse.Content.Headers; all other headers go toresponse.Headers.TryAddWithoutValidation, so non-standard headers are supported.ByteArrayContentis created to host it.New public API
RequestMockResponseBuilder.WithHeader(string name, string value)RequestMockResponseBuilder.WithHeader(string name, params string[] values)The approved API verification files (
net472.verified.txt,net8.0.verified.txt) were regenerated viaAcceptApiChanges.ps1.Tests
Added a
ResponseHeadersspec class inMockly.Specs(xUnit, FluentAssertions, AAA) covering: standard header, content-header routing, empty-content creation, multi-value header, header applied on every invocation, chaining, last-value-wins, configuration-time name validation, and array-mutation safety. Full suite is green onnet472andnet8.0.Breaking changes
None. The methods are purely additive.
RequestMock.Responderremains a publicFunc<RequestInfo, HttpResponseMessage>.Follow-up
This issue also describes companion response-header assertions for the separate
dennisdoomen/fluentassertions.mocklyrepository (WithResponseHeaderonCapturedRequestAssertions/ContainedRequestAssertions, for both the v7 and v8 packages). That work is not part of this repository and requires a separate PR indennisdoomen/fluentassertions.mockly.