Skip to content
Merged
430 changes: 347 additions & 83 deletions tests/E2E.Agent.Tests/PlaywrightRoutes.cs

Large diffs are not rendered by default.

61 changes: 57 additions & 4 deletions tests/E2E.NUnit.Tests/Helpers/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public static async Task After(int milliseconds, Func<Task> next)
}

/// <summary>Another origin on the same app, Playwright's <c>server.CROSS_PROCESS_PREFIX</c>.</summary>
public static string CROSS => PREFIX.Replace("127.0.0.1", "localhost", StringComparison.Ordinal);

/// <summary>The absolute path of one of Playwright's test assets.</summary>
public static string Asset(string name) => Path.Combine(AppContext.BaseDirectory, "Testbed", "playwright", "assets", name);
public static string CROSS => E2E.Playground.Testbed.Url.Replace("127.0.0.1", "localhost", StringComparison.Ordinal);

/// <summary>A path that serves <paramref name="html"/> as a page, for Playwright's <c>page.setContent(html)</c>.</summary>
public static string Content(string html) =>
Expand All @@ -58,6 +55,62 @@ public static void Equal<T>(T expected, T actual)
}
}

/// <summary>A check that <paramref name="actual"/> contains <paramref name="expected"/>, for a body run by <see cref="ExpectedToFailAsync"/>. It throws, as <see cref="Equal{T}"/> does.</summary>
public static void Contain(string expected, string actual)
{
if (!actual.Contains(expected, StringComparison.Ordinal))
{
throw new InvalidOperationException($"Expected the text to contain {expected}, but was {actual}.");
}
}

/// <summary>The absolute path of one of Playwright's test assets.</summary>
public static string Asset(string name) => Path.Combine(AppContext.BaseDirectory, "Testbed", "playwright", "assets", name);

/// <summary>A promise with its resolver, for a route handler the test holds open. .NET-only, a test helper.</summary>
public static TaskCompletionSource<T> Deferred<T>() => new(TaskCreationOptions.RunContinuationsAsynchronously);

/// <summary>A <see cref="Deferred{T}"/> with no value.</summary>
public static TaskCompletionSource Deferred() => new(TaskCreationOptions.RunContinuationsAsynchronously);

/// <summary>The request headers with <paramref name="set"/> added or replaced, for <c>{ ...route.request.headers, name: value }</c>. .NET-only, a test helper.</summary>
public static Dictionary<string, string> WithHeaders(IReadOnlyDictionary<string, string> headers, params (string Name, string Value)[] set)
{
var copy = new Dictionary<string, string>(headers, StringComparer.Ordinal);
foreach (var (name, value) in set)
{
copy[name] = value;
}

return copy;
}

/// <summary>The request the server received, which <c>/cors-echo</c> and <c>/echo</c> answer with. .NET-only, a test helper.</summary>
public sealed record Served(string Method, Dictionary<string, string> Headers, string Body, string Url)
{
/// <summary>The header named <paramref name="name"/>, or <see langword="null"/> when the request had none.</summary>
public string? Header(string name) => Headers.GetValueOrDefault(name);

public static Served Parse(string json) =>
System.Text.Json.JsonSerializer.Deserialize<Served>(json, new System.Text.Json.JsonSerializerOptions(System.Text.Json.JsonSerializerDefaults.Web))!;
}

/// <summary>
/// Playwright's <c>Promise.all([wait, action])</c> for a wait whose result the test reads: waits for both and returns the result of <paramref name="wait"/>.
/// .NET-only, a test helper.
/// </summary>
public static async Task<T> AllAsync<T>(Task<T> wait, Task action)
{
var first = await Task.WhenAny(wait, action).ConfigureAwait(false);
if (first.IsFaulted)
{
await first.ConfigureAwait(false);
}

await Task.WhenAll(wait, action).ConfigureAwait(false);
return wait.Result;
}

/// <summary>
/// Runs the body of a test upstream marks <c>{ fail: 'reason' }</c>, a known difference from Playwright: the test passes while
/// <paramref name="body"/> still fails with a message that contains <paramref name="failure"/>, and fails once it passes, so the marker is removed. The port has no <c>fail</c> test option
Expand Down
112 changes: 112 additions & 0 deletions tests/E2E.NUnit.Tests/RequestFallback/PageRequestFallbackTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2026 TesterArmy.
// Modified by Dario Kondratiuk.
// SPDX-License-Identifier: Apache-2.0

using E2E.Engine;
using static E2E.NUnit.Tests.Helpers;

namespace E2E.NUnit.Tests.RequestFallback;

/// <summary>
/// <c>test.describe('page request fallback')</c> of upstream's <c>apps/testbed/tests/request-fallback.e2e.ts</c> (Playwright's
/// <c>page-request-fallback.spec.ts</c>). Not ported, as upstream: should chain once (no <c>times</c>), should amend HTTP headers,
/// should delete header with undefined value, should amend method, should override request url, and the <c>post data</c> tests
/// (no options on <c>fallback</c>).
/// </summary>
public sealed class PageRequestFallbackTests : PlaywrightTest
{
private static string EmptyPage => $"{PREFIX}/empty.html";

[Test]
public async Task Should_work()
{
await Browser.RouteAsync("**/*", route => route.FallbackAsync());
await Browser.GotoAsync(EmptyPage);
}

[Test]
public async Task Should_fall_back()
{
var intercepted = new List<int>();
foreach (var index in new[] { 1, 2, 3 })
{
await Browser.RouteAsync("**/empty.html", async route =>
{
intercepted.Add(index);
await route.FallbackAsync();
});
}

await Browser.GotoAsync(EmptyPage);
Assert.That(intercepted, Is.EqualTo(new[] { 3, 2, 1 }));
}

[Test]
public async Task Should_fall_back_async()
{
var intercepted = new List<int>();
foreach (var index in new[] { 1, 2, 3 })
{
await Browser.RouteAsync("**/empty.html", async route =>
{
intercepted.Add(index);
await Task.Delay(100);
await route.FallbackAsync();
});
}

await Browser.GotoAsync(EmptyPage);
Assert.That(intercepted, Is.EqualTo(new[] { 3, 2, 1 }));
}

[Test]
public async Task Should_not_chain_fulfill()
{
var failed = false;
await Browser.RouteAsync("**/empty.html", async route =>
{
failed = true;
await route.ContinueAsync();
});
await Browser.RouteAsync("**/empty.html", route => route.FulfillAsync(new RouteFulfillResponse { Status = 200, Body = "fulfilled" }));
await Browser.RouteAsync("**/empty.html", route => route.FallbackAsync());
var response = await AllAsync(Browser.WaitForResponseAsync("**/empty.html"), Browser.GotoAsync(EmptyPage));
Assert.That(await response.TextAsync(), Is.EqualTo("fulfilled"));
Assert.That(failed, Is.False);
}

[Test]
public async Task Should_not_chain_abort()
{
var failed = false;
await Browser.RouteAsync("**/empty.html", async route =>
{
failed = true;
await route.ContinueAsync();
});
await Browser.RouteAsync("**/empty.html", route => route.AbortAsync());
await Browser.RouteAsync("**/empty.html", route => route.FallbackAsync());
var e = await Assert.CatchAsync(() => Browser.GotoAsync(EmptyPage));
Assert.That(e, Is.Not.Null);
Assert.That(failed, Is.False);
}

[Test]
public async Task Should_fall_back_after_exception()
{
await Browser.RouteAsync("**/empty.html", route => route.ContinueAsync());
await Browser.RouteAsync("**/empty.html", async route =>
{
try
{
// Upstream passes an unknown option; here a body and a json together, which fulfill refuses before it decides.
await route.FulfillAsync(new RouteFulfillResponse { Body = "", Json = new { } });
}
catch
{
await route.FallbackAsync();
}
});
await Browser.GotoAsync(EmptyPage);
}
}
Loading
Loading