mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +01:00
[PR #1285] [MERGED] Enable support for WireMock Middleware in Hosted Services #1315
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?
📋 Pull Request Information
Original PR: https://github.com/wiremock/WireMock.Net/pull/1285
Author: @etkr
Created: 4/24/2025
Status: ✅ Merged
Merged: 4/25/2025
Merged by: @StefH
Base:
master← Head:feature/wiremock-middleware-hosted-service-support📝 Commits (1)
021a03dEnable support for WireMock Middleware in Hosted Services📊 Changes
5 files changed (+94 additions, -3 deletions)
View changed files
📝
src/WireMock.Net.AspNetCore.Middleware/HttpDelegatingHandler/WireMockDelegationHandler.cs(+16 -3)📝
test/WireMock.Net.Middleware.Tests/IntegrationTests.cs(+1 -0)📝
test/WireMock.Net.TestWebApplication/Program.cs(+15 -0)➕
test/WireMock.Net.TestWebApplication/TaskQueue.cs(+38 -0)➕
test/WireMock.Net.TestWebApplication/TestBackgroundService.cs(+24 -0)📄 Description
Background
While attempting to use Wiremock Middle in an application that uses hosted services i noticed that it breaks and throws an
ArgumentNullException.See #1284
Problem
The problem is that
IHttpContextAccessor.HttpContextcan not be relied upon to always be available.It would be best if
WireMockDelegationHandlercan fallback gracefully ifHttpContextis null, which is expected in some cases.Solution
I changed
IsWireMockRedirectHeaderSetToTrueandTryGetDelayHeaderValueinWireMockDelegationHandlerto default to false ifHttpContextis null.I created a test to check that it works when started in a hosted service.
Limitations
The
X-WireMock-RedirectandX-WireMock-Response-Delayheaders are obviously not going to work as expected when mocking a request from a background service, maybe this needs to be documented somewhere.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.