diff --git a/lib/PuppeteerSharp.TestServer/SimpleServer.cs b/lib/PuppeteerSharp.TestServer/SimpleServer.cs index 2d39a8e79..e84f4d6d8 100644 --- a/lib/PuppeteerSharp.TestServer/SimpleServer.cs +++ b/lib/PuppeteerSharp.TestServer/SimpleServer.cs @@ -1,11 +1,13 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; @@ -18,6 +20,11 @@ public class SimpleServer private readonly IDictionary _auths; private readonly IDictionary _csp; private readonly IWebHost _webHost; + private readonly bool _isHttps; + + public int Port { get; private set; } + public string Prefix { get; private set; } + public string IpPrefix { get; private set; } internal IList GzipRoutes { get; } public static SimpleServer Create(int port, string contentRoot) => new SimpleServer(port, contentRoot, isHttps: false); @@ -25,6 +32,7 @@ public class SimpleServer private SimpleServer(int port, string contentRoot, bool isHttps) { + _isHttps = isHttps; _requestSubscribers = new ConcurrentDictionary>(); _routes = new ConcurrentDictionary(); _auths = new ConcurrentDictionary(); @@ -92,7 +100,19 @@ private SimpleServer(int port, string contentRoot, bool isHttps) public void SetCSP(string path, string csp) => _csp.Add(path, csp); - public Task StartAsync() => _webHost.StartAsync(); + public async Task StartAsync() + { + await _webHost.StartAsync(); + var address = _webHost.ServerFeatures + .Get() + .Addresses + .First(); + var uri = new Uri(address); + Port = uri.Port; + var scheme = _isHttps ? "https" : "http"; + Prefix = $"{scheme}://localhost:{Port}"; + IpPrefix = $"{scheme}://127.0.0.1:{Port}"; + } public async Task StopAsync() { diff --git a/lib/PuppeteerSharp.Tests/CoverageTests/CSSCoverageTests.cs b/lib/PuppeteerSharp.Tests/CoverageTests/CSSCoverageTests.cs index c895b5af9..a02e58a01 100644 --- a/lib/PuppeteerSharp.Tests/CoverageTests/CSSCoverageTests.cs +++ b/lib/PuppeteerSharp.Tests/CoverageTests/CSSCoverageTests.cs @@ -129,7 +129,7 @@ public async Task ShouldWorkWithComplicatedUsecases() WriteIndented = true }); Assert.That( - Regex.Replace(TestUtils.CompressText(coverageAsJsonString), @":\d{4}\/", ":/"), + Regex.Replace(TestUtils.CompressText(coverageAsJsonString), @":\d{4,5}\/", ":/"), Is.EqualTo(TestUtils.CompressText(involved))); } diff --git a/lib/PuppeteerSharp.Tests/CoverageTests/JSCoverageTests.cs b/lib/PuppeteerSharp.Tests/CoverageTests/JSCoverageTests.cs index cff9f6c24..a25859633 100644 --- a/lib/PuppeteerSharp.Tests/CoverageTests/JSCoverageTests.cs +++ b/lib/PuppeteerSharp.Tests/CoverageTests/JSCoverageTests.cs @@ -195,7 +195,7 @@ public async Task ShouldWorkWithConditionals() Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, }); Assert.That( - Regex.Replace(TestUtils.CompressText(coverageAsJsonString), @"\d{4}\/", "/"), + Regex.Replace(TestUtils.CompressText(coverageAsJsonString), @":\d{4,5}\/", ":/"), Is.EqualTo(TestUtils.CompressText(involved))); } diff --git a/lib/PuppeteerSharp.Tests/FrameUtils.cs b/lib/PuppeteerSharp.Tests/FrameUtils.cs index a5309036a..b3baba83e 100644 --- a/lib/PuppeteerSharp.Tests/FrameUtils.cs +++ b/lib/PuppeteerSharp.Tests/FrameUtils.cs @@ -33,7 +33,7 @@ public static Task DetachFrameAsync(IFrame frame, string frameId) public static async Task> DumpFramesAsync(IFrame frame, string indentation = "") { - var description = indentation + Regex.Replace(frame.Url, @":\d{4}", ":"); + var description = indentation + Regex.Replace(frame.Url, @":\d{4,5}", ":"); await using var frameElement = await frame.FrameElementAsync(); if (frameElement != null) { diff --git a/lib/PuppeteerSharp.Tests/NetworkTests/NetworkEventTests.cs b/lib/PuppeteerSharp.Tests/NetworkTests/NetworkEventTests.cs index 2a5daeae7..60295ae7a 100644 --- a/lib/PuppeteerSharp.Tests/NetworkTests/NetworkEventTests.cs +++ b/lib/PuppeteerSharp.Tests/NetworkTests/NetworkEventTests.cs @@ -188,7 +188,7 @@ public async Task ShouldSupportRedirects() } }; Server.SetRedirect("/foo.html", "/empty.html"); - const string FOO_URL = TestConstants.ServerUrl + "/foo.html"; + var FOO_URL = TestConstants.ServerUrl + "/foo.html"; var response = await Page.GoToAsync(FOO_URL); Assert.That(events.ToArray(), Is.EqualTo(new[] { $"GET {FOO_URL}", @@ -209,7 +209,7 @@ public async Task ShouldSupportRedirects() public async Task ResponseRemoteAddressShouldSupportRedirects() { Server.SetRedirect("/foo.html", "/empty.html"); - const string FOO_URL = TestConstants.ServerUrl + "/foo.html"; + var FOO_URL = TestConstants.ServerUrl + "/foo.html"; var response = await Page.GoToAsync(FOO_URL); // Check redirect chain remote address diff --git a/lib/PuppeteerSharp.Tests/TestConstants.cs b/lib/PuppeteerSharp.Tests/TestConstants.cs index 9c6ce05c8..deffff36a 100644 --- a/lib/PuppeteerSharp.Tests/TestConstants.cs +++ b/lib/PuppeteerSharp.Tests/TestConstants.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Logging; using PuppeteerSharp.Mobile; using PuppeteerSharp.Nunit; +using PuppeteerSharp.TestServer; namespace PuppeteerSharp.Tests { @@ -11,16 +12,17 @@ public static class TestConstants { public const int DebuggerAttachedTestTimeout = 300_000; public const int DefaultPuppeteerTimeout = 10_000; - public const int Port = 7081; - public const int HttpsPort = Port + 1; - public const string ServerUrl = "http://localhost:7081"; - public const string ServerIpUrl = "http://127.0.0.1:7081"; - public const string HttpsPrefix = "https://localhost:7082"; public const string AboutBlank = "about:blank"; - public static readonly string CrossProcessHttpPrefix = "http://127.0.0.1:7081"; - public static readonly string CrossProcessHttpsPrefix = "https://127.0.0.1:7082"; - public static readonly string EmptyPage = $"{ServerUrl}/empty.html"; - public static readonly string CrossProcessUrl = ServerIpUrl; + + public static int Port { get; private set; } + public static int HttpsPort { get; private set; } + public static string ServerUrl { get; private set; } + public static string ServerIpUrl { get; private set; } + public static string HttpsPrefix { get; private set; } + public static string CrossProcessHttpPrefix { get; private set; } + public static string CrossProcessHttpsPrefix { get; private set; } + public static string EmptyPage { get; private set; } + public static string CrossProcessUrl { get; private set; } public static readonly bool IsChrome = PuppeteerTestAttribute.IsChrome; public static readonly DeviceDescriptor IPhone = Puppeteer.Devices[DeviceDescriptorName.IPhone6]; public static readonly DeviceDescriptor IPhone6Landscape = Puppeteer.Devices[DeviceDescriptorName.IPhone6Landscape]; @@ -37,6 +39,19 @@ public static class TestConstants " http://localhost:/frames/frame.html (aframe)" }; + public static void Initialize(SimpleServer server, SimpleServer httpsServer) + { + Port = server.Port; + HttpsPort = httpsServer.Port; + ServerUrl = server.Prefix; + ServerIpUrl = server.IpPrefix; + HttpsPrefix = httpsServer.Prefix; + CrossProcessHttpPrefix = server.IpPrefix; + CrossProcessHttpsPrefix = httpsServer.IpPrefix; + EmptyPage = $"{ServerUrl}/empty.html"; + CrossProcessUrl = ServerIpUrl; + } + public static LaunchOptions DefaultBrowserOptions() => new() { SlowMo = Convert.ToInt32(Environment.GetEnvironmentVariable("SLOW_MO")), diff --git a/lib/PuppeteerSharp.Tests/TestServerSetup.cs b/lib/PuppeteerSharp.Tests/TestServerSetup.cs index 631f7a61d..fc1d6dc7f 100644 --- a/lib/PuppeteerSharp.Tests/TestServerSetup.cs +++ b/lib/PuppeteerSharp.Tests/TestServerSetup.cs @@ -16,13 +16,15 @@ public async Task InitAllAsync() var browserFetcher = new BrowserFetcher(TestConstants.IsChrome ? SupportedBrowser.Chrome : SupportedBrowser.Firefox); var downloaderTask = browserFetcher.DownloadAsync(); - Server = SimpleServer.Create(TestConstants.Port, TestUtils.FindParentDirectory("PuppeteerSharp.TestServer")); - HttpsServer = SimpleServer.CreateHttps(TestConstants.HttpsPort, TestUtils.FindParentDirectory("PuppeteerSharp.TestServer")); + Server = SimpleServer.Create(0, TestUtils.FindParentDirectory("PuppeteerSharp.TestServer")); + HttpsServer = SimpleServer.CreateHttps(0, TestUtils.FindParentDirectory("PuppeteerSharp.TestServer")); var serverStart = Server.StartAsync(); var httpsServerStart = HttpsServer.StartAsync(); await Task.WhenAll(downloaderTask, serverStart, httpsServerStart); + + TestConstants.Initialize(Server, HttpsServer); } [OneTimeTearDown]