Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dotnet/Selenium.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<Project Path="test/remote/Selenium.WebDriver.Remote.Tests.csproj" />
<Project Path="test/support/Selenium.WebDriver.Support.Tests.csproj" />
<Project Path="test/webdriver/Selenium.WebDriver.Tests.csproj" />
<Project Path="test/testing.webserver/Selenium.Testing.WebServer.csproj" />
</Folder>
</Solution>
15 changes: 3 additions & 12 deletions dotnet/private/dotnet_nunit_test_suite.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ _BROWSERS = {
"--params=DriverServiceLocation=$(location @mac_chromedriver//:chromedriver)",
"--params=BrowserLocation=$(location @mac_chrome//:Chrome.app)/Contents/MacOS/Chrome",
],
"@selenium//common:use_local_chromedriver": [],
"//conditions:default": [
"--where=SkipTest==True",
],
"//conditions:default": [],
Comment thread
nvborisenko marked this conversation as resolved.
}),
"data": chrome_data,
"tags": [],
Expand All @@ -40,10 +37,7 @@ _BROWSERS = {
"--params=DriverServiceLocation=$(location @mac_edgedriver//:msedgedriver)",
"\"--params=BrowserLocation=$(location @mac_edge//:Edge.app)/Contents/MacOS/Microsoft Edge\"",
],
"@selenium//common:use_local_msedgedriver": [],
"//conditions:default": [
"--where=SkipTest==True",
],
"//conditions:default": [],
}),
"data": edge_data,
"tags": [],
Expand All @@ -60,10 +54,7 @@ _BROWSERS = {
"--params=DriverServiceLocation=$(location @mac_geckodriver//:geckodriver)",
"--params=BrowserLocation=$(location @mac_firefox//:Firefox.app)/Contents/MacOS/firefox",
],
"@selenium//common:use_local_geckodriver": [],
"//conditions:default": [
"--where=SkipTest==True",
],
"//conditions:default": [],
Comment thread
nvborisenko marked this conversation as resolved.
}),
"data": firefox_data,
"tags": [],
Expand Down
2 changes: 2 additions & 0 deletions dotnet/test/remote/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ dotnet_nunit_test_suite(
"//dotnet/test/webdriver:test-data",
],
flaky = True,
project_sdk = "web",
target_frameworks = ["net8.0"],
deps = [
"//dotnet/src/webdriver:webdriver-net8.0",
"//dotnet/test/testing.webserver",
"//dotnet/test/webdriver",
nuget_package("NUnit"),
nuget_package("Runfiles"),
Expand Down
2 changes: 2 additions & 0 deletions dotnet/test/support/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ dotnet_nunit_test_suite(
data = [
"//dotnet/test/webdriver:test-data",
],
project_sdk = "web",
target_frameworks = ["net8.0"],
deps = [
"//dotnet/src/support",
"//dotnet/src/webdriver:webdriver-net8.0",
"//dotnet/test/testing.webserver",
"//dotnet/test/webdriver",
nuget_package("Microsoft.Bcl.AsyncInterfaces"),
nuget_package("Moq"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\support\Selenium.WebDriver.Support.csproj" />
<ProjectReference Include="..\webdriver\Selenium.WebDriver.Tests.csproj" />
<ProjectReference Include="..\testing.webserver\Selenium.Testing.WebServer.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
159 changes: 159 additions & 0 deletions dotnet/test/testing.webserver/AppServer.cs
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);
});
});
Comment thread
nvborisenko marked this conversation as resolved.
builder.Services.AddDirectoryBrowser();
Comment thread
nvborisenko marked this conversation as resolved.
Comment thread
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;
Comment thread
nvborisenko marked this conversation as resolved.
}
}
18 changes: 18 additions & 0 deletions dotnet/test/testing.webserver/BUILD.bazel
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 dotnet/test/testing.webserver/Handlers/BasicAuthHandler.cs
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;
Comment thread
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");
Comment thread
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");
}
}
Loading
Loading