Conversation
…ting subscriber, and fluent extension Agent-Logs-Url: https://github.com/hibri/HttpMock/sessions/140eba6b-a97a-4bd8-841f-9781fddde147 Co-authored-by: hibri <122442+hibri@users.noreply.github.com>
…ockServer null guard Agent-Logs-Url: https://github.com/hibri/HttpMock/sessions/140eba6b-a97a-4bd8-841f-9781fddde147 Co-authored-by: hibri <122442+hibri@users.noreply.github.com>
Agent-Logs-Url: https://github.com/hibri/HttpMock/sessions/140eba6b-a97a-4bd8-841f-9781fddde147 Co-authored-by: hibri <122442+hibri@users.noreply.github.com>
…rt(); make HttpMockResource.Url public Agent-Logs-Url: https://github.com/hibri/HttpMock/sessions/140eba6b-a97a-4bd8-841f-9781fddde147 Co-authored-by: hibri <122442+hibri@users.noreply.github.com>
…nd integration-test link Agent-Logs-Url: https://github.com/hibri/HttpMock/sessions/d77ab419-5192-490c-8f7d-aa086b54ff9f Co-authored-by: hibri <122442+hibri@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds first-class .NET Aspire Hosting integration for HttpMock by introducing a new HttpMock.Aspire.Hosting package that registers and manages HttpMock stub servers as Aspire resources, plus consolidates free-port selection into the core library.
Changes:
- Introduces
HttpMock.Aspire.Hostingpackage withHttpMockResource,HttpMockEventingSubscriber, andAddHttpMock()builder extension. - Adds
HttpMockRepository.FindFreePort()and updates test helpers to use it instead of ad-hoc port probing. - Adds unit + integration test projects for the Aspire hosting integration and updates the Aspire example + README guidance.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/HttpMock/HttpMockRepository.cs | Adds FindFreePort() helper to centralize port allocation. |
| src/HttpMock.Integration.Tests/PortHelper.cs | Replaces custom port probing with HttpMockRepository.FindFreePort(). |
| src/HttpMock.Aspire.Hosting/HttpMockResourceBuilderExtensions.cs | Adds AddHttpMock() fluent registration entry point for app hosts. |
| src/HttpMock.Aspire.Hosting/HttpMockResource.cs | Defines Aspire resource model for an HttpMock server (URL + server handle + connection string expression). |
| src/HttpMock.Aspire.Hosting/HttpMockEventingSubscriber.cs | Starts/stops HttpMock servers via Aspire lifecycle eventing and publishes resource state/URL. |
| src/HttpMock.Aspire.Hosting/HttpMock.Aspire.Hosting.csproj | New packable project for Aspire hosting integration. |
| src/HttpMock.Aspire.Hosting.Tests/HttpMockResourceTests.cs | Unit + in-process subscriber tests for the new Aspire resource. |
| src/HttpMock.Aspire.Hosting.Tests/HttpMock.Aspire.Hosting.Tests.csproj | New test project for the Aspire hosting integration. |
| src/HttpMock.Aspire.Hosting.IntegrationTests/HttpMockResourceIntegrationTests.cs | End-to-end lifecycle tests using Aspire.Hosting.Testing. |
| src/HttpMock.Aspire.Hosting.IntegrationTests/HttpMock.Aspire.Hosting.IntegrationTests.csproj | New integration test project referencing the AppHost + hosting package. |
| src/HttpMock.Aspire.Hosting.IntegrationTests.AppHost/HttpMock.Aspire.Hosting.IntegrationTests.AppHost.csproj | Minimal Aspire AppHost used by integration tests. |
| src/HttpMock.Aspire.Hosting.IntegrationTests.AppHost/AppHost.cs | Registers AddHttpMock("mock-api") in the test AppHost. |
| examples/AspireExample/README.md | Updates example docs to use AddHttpMock and resource-based stubbing. |
| examples/AspireExample/AspireExample.Tests/IntegrationTest1.cs | Updates example tests to discover HttpMockResource and configure stubs via MockServer. |
| examples/AspireExample/AspireExample.Tests/AspireExample.Tests.csproj | Switches example test dependency to HttpMock.Aspire.Hosting and adds required usings. |
| examples/AspireExample/AspireExample.AppHost/AspireExample.AppHost.csproj | References HttpMock.Aspire.Hosting as a non-resource dependency. |
| examples/AspireExample/AspireExample.AppHost/AppHost.cs | Registers HttpMock as a first-class Aspire resource instead of a connection string override. |
| README.md | Replaces stale Aspire instructions with new HttpMock.Aspire.Hosting guidance + FindFreePort() section. |
| HttpMock.sln | Adds new projects to the solution and extends configurations. |
| .github/workflows/publish.yml | Packs and publishes the new HttpMock.Aspire.Hosting package on release. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /// resource.MockServer.WithNewContext().Stub(x => x.Get("/path")).Return("body").OK(); | ||
| /// </code> | ||
| /// </summary> | ||
| public IHttpServer? MockServer { get; internal set; } |
| var port = HttpMockRepository.FindFreePort(); | ||
| var url = $"http://localhost:{port}"; | ||
|
|
||
| resource.Url = url; | ||
| resource.MockServer = new HttpServer(new Uri(url)); |
There was a problem hiding this comment.
Done in d7b4765. Added explicit using HttpMock; to HttpMockEventingSubscriber.cs, HttpMockResource.cs, and HttpMockResourceTests.cs. Also replaced the hard-coded port 59998 in the Dispose_ClearsMockServerProperty test with HttpMockRepository.FindFreePort() so it can't collide with other tests or processes.
| public void Dispose_ClearsMockServerProperty() | ||
| { | ||
| var resource = new HttpMockResource("test"); | ||
| resource.MockServer = HttpMockRepository.At("http://localhost:59998"); |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…ndFreePort() Agent-Logs-Url: https://github.com/hibri/HttpMock/sessions/888e1a44-f8df-4830-a549-32b056e778b2 Co-authored-by: hibri <122442+hibri@users.noreply.github.com>
| } No newline at end of file | ||
| namespace HttpMock.Integration.Tests | ||
| { | ||
| internal static class PortHelper |
There was a problem hiding this comment.
@copilot remove the PortHelper class and update its usages with HttpMock.HttpMockRepository.FindFreePort();
There was a problem hiding this comment.
Done in 661bcb1. Deleted PortHelper.cs and updated HostHelper.cs and HttpServerTests.cs to call HttpMockRepository.FindFreePort() directly.
…indFreePort() Agent-Logs-Url: https://github.com/hibri/HttpMock/sessions/d30c5893-49ac-4eb2-862d-e2d86a96938a Co-authored-by: hibri <122442+hibri@users.noreply.github.com>
…pire' into copilot/evaluate-httpmock-net-aspire
Ships
HttpMock.Aspire.Hosting, a new NuGet package that registers an HttpMock stub server as a proper Aspire resource. Eliminates manual port allocation,appHost.Configuration[...]overrides, andFindAvailablePortboilerplate from test projects.New package:
HttpMock.Aspire.Hosting(net10.0, Aspire 13.2.2)HttpMockResource—Resource+IResourceWithConnectionString+IDisposable. PublicUrl(read after start) andMockServer(for stub configuration in tests).HttpMockEventingSubscriber—IDistributedApplicationEventingSubscriber(Aspire 13 eventing API). Starts the server inBeforeStartEvent, publishesRunningstate to the dashboard, disposes on teardown.HttpMockResourceBuilderExtensions.AddHttpMock()— single fluent entry point viaTryAddEventingSubscriber.HttpMockRepository.FindFreePort()— consolidated port allocationReplaced the ad-hoc
lsof-basedPortHelper.FindLocalAvailablePortForTesting()and the privateFindFreePort()inHttpMockEventingSubscriberwith a single publicTcpListener(port 0)implementation onHttpMockRepository. Both former callers now delegate to it.New test projects
HttpMock.Aspire.Hosting.Tests— 5 unit/subscriber tests; in-process, no DCP required. Subscriber test firesBeforeStartEventdirectly via aCapturingEventingtest double.HttpMock.Aspire.Hosting.IntegrationTests.AppHost— minimalAspire.AppHost.Sdkhost registeringAddHttpMock("mock-api").HttpMock.Aspire.Hosting.IntegrationTests— full lifecycle viaAspire.Hosting.Testing: start → stub → real HTTP calls. Covers GET/POST, JSON, status codes,WithNewContextisolation, and connection-string expression.Updated
examples/AspireExample— AppHost and tests useAddHttpMock/WithReference;README.mdupdated.README.md— new Finding a Free Port and.NET Aspire Integrationsections replace the stale manual example.publish.yml— packsHttpMock.Aspire.Hostingon release alongside existing packages.