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
22 changes: 21 additions & 1 deletion lib/PuppeteerSharp.TestServer/SimpleServer.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -18,13 +20,19 @@ public class SimpleServer
private readonly IDictionary<string, (string username, string password)> _auths;
private readonly IDictionary<string, string> _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<string> GzipRoutes { get; }
public static SimpleServer Create(int port, string contentRoot) => new SimpleServer(port, contentRoot, isHttps: false);
public static SimpleServer CreateHttps(int port, string contentRoot) => new SimpleServer(port, contentRoot, isHttps: true);

private SimpleServer(int port, string contentRoot, bool isHttps)
{
_isHttps = isHttps;
_requestSubscribers = new ConcurrentDictionary<string, Action<HttpRequest>>();
_routes = new ConcurrentDictionary<string, RequestDelegate>();
_auths = new ConcurrentDictionary<string, (string username, string password)>();
Expand Down Expand Up @@ -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<IServerAddressesFeature>()
.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()
{
Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp.Tests/CoverageTests/CSSCoverageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public async Task ShouldWorkWithComplicatedUsecases()
WriteIndented = true
});
Assert.That(
Regex.Replace(TestUtils.CompressText(coverageAsJsonString), @":\d{4}\/", ":<PORT>/"),
Regex.Replace(TestUtils.CompressText(coverageAsJsonString), @":\d{4,5}\/", ":<PORT>/"),
Is.EqualTo(TestUtils.CompressText(involved)));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp.Tests/CoverageTests/JSCoverageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public async Task ShouldWorkWithConditionals()
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
});
Assert.That(
Regex.Replace(TestUtils.CompressText(coverageAsJsonString), @"\d{4}\/", "<PORT>/"),
Regex.Replace(TestUtils.CompressText(coverageAsJsonString), @":\d{4,5}\/", ":<PORT>/"),
Is.EqualTo(TestUtils.CompressText(involved)));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp.Tests/FrameUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static Task DetachFrameAsync(IFrame frame, string frameId)

public static async Task<IEnumerable<string>> DumpFramesAsync(IFrame frame, string indentation = "")
{
var description = indentation + Regex.Replace(frame.Url, @":\d{4}", ":<PORT>");
var description = indentation + Regex.Replace(frame.Url, @":\d{4,5}", ":<PORT>");
await using var frameElement = await frame.FrameElementAsync();
if (frameElement != null)
{
Expand Down
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp.Tests/NetworkTests/NetworkEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand All @@ -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
Expand Down
33 changes: 24 additions & 9 deletions lib/PuppeteerSharp.Tests/TestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
using Microsoft.Extensions.Logging;
using PuppeteerSharp.Mobile;
using PuppeteerSharp.Nunit;
using PuppeteerSharp.TestServer;

namespace PuppeteerSharp.Tests
{
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];
Expand All @@ -37,6 +39,19 @@ public static class TestConstants
" http://localhost:<PORT>/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")),
Expand Down
6 changes: 4 additions & 2 deletions lib/PuppeteerSharp.Tests/TestServerSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading