Skip to content

Commit

Permalink
make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Dec 12, 2024
1 parent df59854 commit 755c1e1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
35 changes: 35 additions & 0 deletions src/Playwright.Tests/PageRouteWebSocketTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,39 @@ await AssertAreEqualWithRetriesAsync(() => page.EvaluateAsync<string[]>("() => w
$"message: data=echo origin=ws://localhost:{Server.Port} lastEventId=",
});
}

[PlaywrightTest("page-route-web-socket.spec.ts", "should work with no trailing slash")]
public async Task ShouldWorkWithNoTrailingSlash()
{
var log = new List<string>();
// No trailing slash!
await Page.RouteWebSocketAsync($"ws://localhost:{Server.Port}", ws =>
{
ws.OnMessage(message =>
{
log.Add(message.Text);
ws.Send("response");
});
});

await Page.GotoAsync("about:blank");
await Page.EvaluateAsync(@"({ port }) => {
window.log = [];
// No trailing slash in WebSocket URL
window.ws = new WebSocket('ws://localhost:' + port);
window.ws.addEventListener('message', event => window.log.push(event.data));
}", new Dictionary<string, object>
{
["port"] = Server.Port
});

await Page.WaitForFunctionAsync("() => window.ws.readyState === 1");
await Page.EvaluateAsync("() => window.ws.send('query')");
await AssertAreEqualWithRetriesAsync(
() => Task.FromResult(log.ToArray()),
["query"]);
await AssertAreEqualWithRetriesAsync(
() => Page.EvaluateAsync<string[]>("() => window.log"),
["response"]);
}
}
2 changes: 1 addition & 1 deletion src/Playwright/Core/PageAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Task ToHaveTitleAsync(Regex titleOrRegExp, PageAssertionsToHaveTitleOptio
ExpectImplAsync("to.have.title", ExpectedRegex(titleOrRegExp, new() { NormalizeWhiteSpace = true }), titleOrRegExp, "Page title expected to be", ConvertToFrameExpectOptions(options));

public Task ToHaveURLAsync(string urlOrRegExp, PageAssertionsToHaveURLOptions options = null) =>
ExpectImplAsync("to.have.url", new ExpectedTextValue() { String = URLMatch.JoinWithBaseURL(_page.Context.Options.BaseURL, urlOrRegExp), IgnoreCase = options?.IgnoreCase }, urlOrRegExp, "Page URL expected to be", ConvertToFrameExpectOptions(options));
ExpectImplAsync("to.have.url", new ExpectedTextValue() { String = URLMatch.ConstructURLBasedOnBaseURL(_page.Context.Options.BaseURL, urlOrRegExp), IgnoreCase = options?.IgnoreCase }, urlOrRegExp, "Page URL expected to be", ConvertToFrameExpectOptions(options));

public Task ToHaveURLAsync(Regex urlOrRegExp, PageAssertionsToHaveURLOptions options = null) =>
ExpectImplAsync("to.have.url", ExpectedRegex(urlOrRegExp, new() { IgnoreCase = options?.IgnoreCase }), urlOrRegExp, "Page URL expected to match regex", ConvertToFrameExpectOptions(options));
Expand Down
39 changes: 21 additions & 18 deletions src/Playwright/Helpers/URLMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public class URLMatch
public string baseURL { get; set; }

public bool Match(string url)
{
return MatchImpl(url, re, func, glob, baseURL);
}

private static bool MatchImpl(string url, Regex re, Func<string, bool> func, string glob, string baseURL)
{
if (re != null)
{
Expand All @@ -51,33 +56,31 @@ public bool Match(string url)

if (glob != null)
{
var globWithBaseURL = JoinWithBaseURL(baseURL, glob);
// Allow http(s) baseURL to match ws(s) urls.
if (new Regex("^https?://").IsMatch(globWithBaseURL) && new Regex("^wss?://").IsMatch(url))
if (string.IsNullOrEmpty(glob))
{
return true;
}
if (!glob.StartsWith("*", StringComparison.InvariantCultureIgnoreCase))
{
globWithBaseURL = new Regex("^http").Replace(globWithBaseURL, "ws");
// Allow http(s) baseURL to match ws(s) urls.
if (new Regex("^https?://").IsMatch(baseURL) && new Regex("^wss?://").IsMatch(url))
{
baseURL = new Regex("^http").Replace(baseURL, "ws");
}
glob = ConstructURLBasedOnBaseURL(baseURL, glob);
}
return new Regex(globWithBaseURL.GlobToRegex()).IsMatch(url);
return new Regex(glob.GlobToRegex()).IsMatch(url);
}
return true;
}

internal static string JoinWithBaseURL(string baseUrl, string url)
internal static string ConstructURLBasedOnBaseURL(string baseUrl, string url)
{
if (string.IsNullOrEmpty(baseUrl)
|| (url?.StartsWith("*", StringComparison.InvariantCultureIgnoreCase) ?? false)
|| !Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
if (string.IsNullOrEmpty(baseUrl))
{
return url;
return new Uri(url, UriKind.Absolute).ToString();
}

var mUri = new Uri(url, UriKind.RelativeOrAbsolute);
if (!mUri.IsAbsoluteUri)
{
return new Uri(new Uri(baseUrl), mUri).ToString();
}

return url;
return new Uri(new Uri(baseUrl), new Uri(url, UriKind.RelativeOrAbsolute)).ToString();
}

public bool Equals(string globMatch, Regex reMatch, Func<string, bool> funcMatch, string baseURL)
Expand Down

0 comments on commit 755c1e1

Please sign in to comment.