-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
[dotnet] [test] In-process test webserver #17339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nvborisenko
merged 20 commits into
SeleniumHQ:trunk
from
nvborisenko:dotnet-test-webserver
Apr 12, 2026
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
aa035b9
Simple test webserver
nvborisenko 2bcb947
I love bazel
nvborisenko 7036e69
Update dotnet_nunit_test_suite.bzl
nvborisenko b9d40d4
Remove TestWebServerConfig and related code
nvborisenko a7a0040
Dynamic port
nvborisenko 7632ebd
Dynamic port
nvborisenko 899d350
Clean website configs
nvborisenko 2e28831
Update EnvironmentManager.cs
nvborisenko 35912ca
Update CreatePageHandler.cs
nvborisenko 9ae3137
Cookies tests
nvborisenko 9b65622
Update BasicAuthHandler.cs
nvborisenko 793aff7
Map to /common
nvborisenko 44166df
Revert to common in cookies test
nvborisenko 2e6b068
Format
nvborisenko 40aa9d9
/common prefix
nvborisenko ada2773
Https
nvborisenko c849e00
Avoid keychain
nvborisenko 0db89fe
2 urls
nvborisenko 93e072d
Update AppServer.cs
nvborisenko ebaabe9
localhost everywhere
nvborisenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| // <copyright file="AppServer.cs" company="Selenium Committers"> | ||
| // Licensed to the Software Freedom Conservancy (SFC) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The SFC licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // </copyright> | ||
|
|
||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Security.Cryptography; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Routing; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.FileProviders; | ||
| using OpenQA.Selenium.Testing.WebServer.Handlers; | ||
|
|
||
| namespace OpenQA.Selenium.Testing.WebServer; | ||
|
|
||
| public class AppServer : IAsyncDisposable | ||
| { | ||
| private WebApplication? _app; | ||
| private readonly string _webContentRoot = FindWebContentRoot(); | ||
| private readonly ConcurrentDictionary<string, string> _pages = new(); | ||
|
|
||
| public async Task<(string HttpUrl, string HttpsUrl)> StartAsync() | ||
| { | ||
| var builder = WebApplication.CreateSlimBuilder(); | ||
|
|
||
| var certificate = GenerateSelfSignedCertificate(); | ||
|
|
||
| builder.WebHost.ConfigureKestrel(options => | ||
| { | ||
| options.Listen(IPAddress.Loopback, 0); | ||
| options.Listen(IPAddress.Loopback, 0, listenOptions => | ||
| { | ||
| listenOptions.UseHttps(certificate); | ||
| }); | ||
| }); | ||
|
nvborisenko marked this conversation as resolved.
|
||
| builder.Services.AddDirectoryBrowser(); | ||
|
nvborisenko marked this conversation as resolved.
nvborisenko marked this conversation as resolved.
|
||
|
|
||
| _app = builder.Build(); | ||
|
|
||
| MapEndpoints(_app); | ||
| MapEndpoints(_app.MapGroup("/common")); | ||
|
|
||
| if (Directory.Exists(_webContentRoot)) | ||
| { | ||
| var fileProvider = new PhysicalFileProvider(_webContentRoot); | ||
|
|
||
| _app.UseStaticFiles(new StaticFileOptions | ||
| { | ||
| FileProvider = fileProvider, | ||
| ServeUnknownFileTypes = true | ||
| }); | ||
|
|
||
| _app.UseStaticFiles(new StaticFileOptions | ||
| { | ||
| FileProvider = fileProvider, | ||
| RequestPath = "/common", | ||
| ServeUnknownFileTypes = true | ||
| }); | ||
|
|
||
| _app.UseDirectoryBrowser(new DirectoryBrowserOptions | ||
| { | ||
| FileProvider = fileProvider | ||
| }); | ||
|
|
||
| _app.UseDirectoryBrowser(new DirectoryBrowserOptions | ||
| { | ||
| FileProvider = fileProvider, | ||
| RequestPath = "/common" | ||
| }); | ||
| } | ||
|
|
||
| await _app.StartAsync(); | ||
|
|
||
| int httpPort = new Uri(_app.Urls.First(u => u.StartsWith("http://"))).Port; | ||
| int httpsPort = new Uri(_app.Urls.First(u => u.StartsWith("https://"))).Port; | ||
|
|
||
| return ($"http://localhost:{httpPort}", $"https://localhost:{httpsPort}"); | ||
| } | ||
|
|
||
| public async Task StopAsync() | ||
| { | ||
| if (_app is not null) | ||
| { | ||
| await _app.StopAsync(); | ||
| await _app.DisposeAsync(); | ||
| _app = null; | ||
| } | ||
| } | ||
|
|
||
| public async ValueTask DisposeAsync() | ||
| { | ||
| await StopAsync(); | ||
| } | ||
|
|
||
| private void MapEndpoints(IEndpointRouteBuilder endpoints) | ||
| { | ||
| endpoints.MapGet("/basicAuth", BasicAuthHandler.Handle); | ||
| endpoints.MapGet("/echo", (Delegate)EchoHandler.Handle); | ||
| endpoints.MapGet("/cookie", CookieHandler.Handle); | ||
| endpoints.MapGet("/encoding", EncodingHandler.Handle); | ||
| endpoints.MapGet("/sleep", (Delegate)SleepHandler.Handle); | ||
| endpoints.MapGet("/redirect", RedirectHandler.Handle); | ||
| endpoints.MapGet("/page/{pageNumber}", PageHandler.Handle); | ||
| endpoints.MapGet("/utf8/{*path}", (HttpContext context, string path) => Utf8Handler.Handle(context, path, _webContentRoot)); | ||
| endpoints.MapPost("/createPage", (Delegate)((HttpContext context) => CreatePageHandler.Handle(context, _pages))); | ||
| endpoints.MapPost("/upload", (Delegate)UploadHandler.Handle); | ||
|
|
||
| endpoints.MapGet("/.well-known/web-identity", (HttpContext context) => FedCmHandler.HandleWebIdentity(context)); | ||
| endpoints.MapGet("/fedcm/config.json", (HttpContext context) => FedCmHandler.HandleConfig(context)); | ||
| endpoints.MapPost("/fedcm/id_assertion.json", (HttpContext context) => FedCmHandler.HandleIdAssertion(context)); | ||
|
|
||
| endpoints.MapGet("/temp/{fileName}", (string fileName) => CreatePageHandler.ServePage(fileName, _pages)); | ||
| } | ||
|
|
||
| private static X509Certificate2 GenerateSelfSignedCertificate() | ||
| { | ||
| using var ecdsa = ECDsa.Create(); | ||
| var request = new CertificateRequest("CN=localhost", ecdsa, HashAlgorithmName.SHA256); | ||
| return request.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddYears(1)); | ||
| } | ||
|
|
||
| private static string FindWebContentRoot() | ||
| { | ||
| var info = new DirectoryInfo(AppContext.BaseDirectory); | ||
| while (info is not null && info != info.Root) | ||
| { | ||
| string webPath = Path.Combine(info.FullName, "common", "src", "web"); | ||
| if (Directory.Exists(webPath)) | ||
| { | ||
| return webPath; | ||
| } | ||
| info = info.Parent; | ||
| } | ||
|
|
||
| return string.Empty; | ||
|
nvborisenko marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| load("//dotnet:defs.bzl", "csharp_library") | ||
|
|
||
| csharp_library( | ||
| name = "testing.webserver", | ||
| testonly = True, | ||
| srcs = glob(["**/*.cs"]), | ||
| out = "Testing.WebServer", | ||
| data = [ | ||
| "//common/src/web", | ||
| ], | ||
| nullable = "enable", | ||
| project_sdk = "web", | ||
| run_analyzers = False, | ||
| target_frameworks = ["net8.0"], | ||
| visibility = [ | ||
| "//dotnet/test:__subpackages__", | ||
| ], | ||
| ) |
52 changes: 52 additions & 0 deletions
52
dotnet/test/testing.webserver/Handlers/BasicAuthHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // <copyright file="BasicAuthHandler.cs" company="Selenium Committers"> | ||
| // Licensed to the Software Freedom Conservancy (SFC) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The SFC licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // </copyright> | ||
|
|
||
| using System; | ||
| using System.Net; | ||
| using System.Text; | ||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| namespace OpenQA.Selenium.Testing.WebServer.Handlers; | ||
|
|
||
| public static class BasicAuthHandler | ||
| { | ||
| private const string ExpectedUser = "test"; | ||
| private const string ExpectedPassword = "test"; | ||
|
|
||
| public static IResult Handle(HttpContext context) | ||
| { | ||
| string? authorization = context.Request.Headers.Authorization; | ||
|
nvborisenko marked this conversation as resolved.
|
||
|
|
||
| if (authorization is not null && authorization.StartsWith("Basic ")) | ||
| { | ||
| string encoded = authorization["Basic ".Length..]; | ||
| string decoded = Encoding.UTF8.GetString(Convert.FromBase64String(encoded)); | ||
| string[] parts = decoded.Split(':', 2); | ||
|
|
||
| if (parts.Length == 2 && parts[0] == ExpectedUser && parts[1] == ExpectedPassword) | ||
| { | ||
| return Results.Content("<h1>authorized</h1>", "text/html; charset=utf-8"); | ||
|
nvborisenko marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| context.Response.Headers["WWW-Authenticate"] = "Basic realm=\"selenium-server\""; | ||
| return Results.Text(string.Empty, statusCode: (int)HttpStatusCode.Unauthorized, | ||
| contentType: "text/html; charset=utf-8"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.