dotnet / dotnet/aspnetcore

[WebApplicationFactory] UseKestrel with SSL feedback

Open
#63,012 5 comments 1 reaction 0 assignees View on GitHub
area-mvc feature-mvc-testing
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

First of all, thank you for adding UseKestrel I am happy to be able to write browser-based integration tests now.

Getting SSL working was a bit tricky so I wanted to give feedback. For context I am using SSL certificates provided by dotnet dev-certs, and Selenium ChromeDriver as well as the HttpClient returned by CreateClient. I was only interested in using SSL, but might need both SSL and non-SSL connections in the future.

Adding the following to the override of WebApplicationFactory worked to get SSL mostly working:

```
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureKestrel(options =>
{
options.Listen(
IPAddress.Loopback,
ConfiguredPort,
listenOptions => listenOptions.UseHttps()
);
});

...
```

I use a predetermined "ConfiguredPort" because I need to set a configure value with the applications root url before starting the server.

The problem is HttpClient returned by CreateClient doesn't work:
* it is using the http schema, not https.
* it is using an IP address, and my dev-certs are for localhost

I worked around this with:
```
HttpClient CreateClient()
{
var result = serverFactory.CreateClient();

var uriBuilder = new UriBuilder(result.BaseAddress!);

uriBuilder.Host = "localhost";
uriBuilder.Scheme = "https";

result.BaseAddress = uriBuilder.Uri;

return result;
}
```

It works, but could be easier or at least have some documentation on how to proceed with SSL.

Last, its minor, but it would be nice to document when to call UseKestrel(). I initially tried it from my derived classes constructor, that didn't work, calling it from an override of InitializeAsync did work.

### Describe the solution you'd like

Not sure

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.