mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 14:20:29 +01:00
Enable Request-Aware Responses in AdminApiMappingBuilder When Using Aspire Integration. #702
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @nadzzz on GitHub (Jul 2, 2025).
Originally assigned to: @StefH on GitHub.
Is your feature request related to a problem? Please describe.
I'm unable to customize WireMock responses based on the incoming request content when using the AdminApiMappingBuilder which seems the only way to configure WireMock in Aspire context. The current response methods in AdminApiMappingBuilder do not provide signatures that include the request object. In contrast, the IResponseBuilder interface (and its inheritance chain, such as IBodyResponseBuilder) offers several methods that accept Func<IRequestMessage, ...> parameters, allowing dynamic response generation based on the request. This limitation makes it difficult to create flexible mocks that adapt their responses to the request data
Describe the solution you'd like
I would like the AdminApiMappingBuilder (and related APIs) to support response methods that accept delegates such as Func<IRequestMessage, string> or similar, enabling dynamic response content generation based on the request. This would bring parity with the flexibility offered by IResponseBuilder and its related interfaces
Describe alternatives you've considered
Is your feature request supported by WireMock (java version)? Please provide details.
I don't think Aspire compatibility has been implemented in Java.
Additional context
A pertinent use case: My application code uses the links given back by the hypermedia-driven protocol hrefs provided in the list request. The hrefs minimally need to point back to the mock base URL, which is dynamically assigned each time. For example:
@StefH commented on GitHub (Jul 3, 2025):
The AdminApiMappingBuilder is "disconnected" from the C# code.
WireMock.Net runs in Aspire, and all configuration using that builder is done via the admin REST interface, and this interface does not allow Func<> callbacks.
In your case, you need to use templating to dynamically create the response.
For that LoginRegistrationServer, you can pass this as a value using the
Dataproperty in the mapping.Like:
https://github.com/wiremock/WireMock.Net/blob/master/src/WireMock.Net.Abstractions/Admin/Mappings/MappingModel.cs#L97
And then use this Data in the handlebars repsonse templating for the response.
@nadzzz commented on GitHub (Jul 3, 2025):
Ok, I understand the limitation of the current Aspire version. I will use templates for my immediate problem, and that will work. thanks for the help.
I do have another case where this approach won’t be sufficient. Specifically, it means we can’t use features like WithCallback to provide custom implementations, such as hosting a small in-memory repository to hold state, etc.
Aspire WireMock is very useful for stubbing many services, but it’s not a silver bullet for all external dependency scenarios when running services locally.
Still, it’s quite valuable for many use cases.
Thanks again!