Skip to content

Conversation

@manandre
Copy link
Contributor

CreateServer with TestServerOptions in WebApplicationFactory

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Description

Applies the configured TestServerOptions when creating a TestServer in a WebApplicationFactory.
It addresses a regression introduced in c80f3e5#diff-c70063a52c125c1f25ca65f24827d42e1385d34eac3b567d180b5eb9cd53e696R341

Fixes #64593

Copilot AI review requested due to automatic review settings December 16, 2025 22:13
@manandre manandre requested a review from a team as a code owner December 16, 2025 22:13
@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Dec 16, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Dec 16, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a regression where TestServerOptions configured via UseTestServer were not being applied when creating a TestServer in WebApplicationFactory. The fix ensures that when a TestServer is created from an IServiceProvider, any configured IOptions<TestServerOptions> are properly retrieved and passed to the TestServer constructor.

Key Changes

  • Modified CreateServer(IServiceProvider) to retrieve and use IOptions<TestServerOptions> when available
  • Added a comprehensive integration test to verify the fix works correctly

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs Updated CreateServer method to retrieve and apply TestServerOptions from the service provider, falling back to the parameterless constructor when options are not configured
src/Mvc/test/Mvc.FunctionalTests/TestServerWithCustomConfigurationIntegrationTests.cs Added integration test with custom WebApplicationFactory to verify that TestServerOptions configured via UseTestServer are correctly applied to the created TestServer

protected virtual TestServer CreateServer(IServiceProvider serviceProvider) => new(serviceProvider);
protected virtual TestServer CreateServer(IServiceProvider serviceProvider)
{
var options = serviceProvider.GetService<IOptions<TestServerOptions>>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should instead change the TestServer ctor to try getting the options?

: this(services, featureCollection, Options.Create(new TestServerOptions()))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I wondered also myself but I assume there was a good reason to not do it initially and now it could be considered as a breaking change, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume it was just an oversight 😄

Sure, it could break someone who set TestServer options but didn't expect them to apply to the TestServer, which would be weird. We can leave it since no one has complained in the 6 years it's been that way.

@BrennanConroy BrennanConroy merged commit 1b7269e into dotnet:main Dec 17, 2025
30 of 31 checks passed
@BrennanConroy
Copy link
Member

/backport to release/10.0

@dotnet-policy-service dotnet-policy-service bot added this to the 11.0-preview1 milestone Dec 17, 2025
@github-actions
Copy link
Contributor

Started backporting to release/10.0 (link to workflow run)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates that the PR has been added by a community member needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TestServerOptions not applied anymore by WebApplicationFactory

2 participants