From 3512f1fb1ae05ee18984eccfc7f09b9c2eb71429 Mon Sep 17 00:00:00 2001 From: Halil Durak Date: Thu, 11 Jun 2026 19:11:14 +0300 Subject: [PATCH] update URL-involving tests --- src/browser/URL.zig | 65 ++++++++++++++++++------------ src/browser/markdown.zig | 8 ++-- src/browser/tests/net/request.html | 2 +- 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/browser/URL.zig b/src/browser/URL.zig index 2e448ab58..fc3ca8c7a 100644 --- a/src/browser/URL.zig +++ b/src/browser/URL.zig @@ -860,6 +860,7 @@ test "URL: resolve" { base: [:0]const u8, path: [:0]const u8, expected: [:0]const u8, + expected_error: bool = false, }; const cases = [_]Case{ @@ -1013,30 +1014,37 @@ test "URL: resolve" { .path = "./.././.././hello", .expected = "https://example/hello", }, + // A base must itself be a valid absolute URL; schemeless bases are + // rejected, matching `new URL(path, base)`. .{ .base = "some/page", .path = "hello", - .expected = "some/hello", + .expected = "", + .expected_error = true, }, .{ .base = "some/page/", .path = "hello", - .expected = "some/page/hello", + .expected = "", + .expected_error = true, }, .{ .base = "some/page/other", .path = ".././hello", - .expected = "some/hello", + .expected = "", + .expected_error = true, }, .{ .base = "https://www.example.com/hello/world", .path = "//example/about", .expected = "https://example/about", }, + // "http:" alone is not a valid base (special scheme without a host). .{ .base = "http:", .path = "//example.com/over/9000", - .expected = "http://example.com/over/9000", + .expected = "", + .expected_error = true, }, .{ .base = "https://example.com/", @@ -1081,8 +1089,13 @@ test "URL: resolve" { }; for (cases) |case| { - const result = try resolve(testing.arena_allocator, case.base, case.path, .{}); - try testing.expectString(case.expected, result); + if (case.expected_error) { + const result = resolve(testing.arena_allocator, case.base, case.path, .{}); + try testing.expectError(error.TypeError, result); + } else { + const result = try resolve(testing.arena_allocator, case.base, case.path, .{}); + try testing.expectString(case.expected, result); + } } } @@ -1122,15 +1135,13 @@ test "URL: resolve strips tab and newline from input" { test "URL: resolve validates ASCII punycode (xn--) labels" { defer testing.reset(); - // Valid punycode is left untouched (the needsAscii fast path would skip it, - // so this exercises the xn-- gate going through toAscii and back). - const ok = try resolve(testing.arena_allocator, "", "https://xn--rksmrgs-5wao1o.se/x", .{}); + // Valid punycode is left untouched. + const ok = try resolve(testing.arena_allocator, "https://example.com/", "https://xn--rksmrgs-5wao1o.se/x", .{}); try testing.expectString("https://xn--rksmrgs-5wao1o.se/x", ok); // Malformed punycode must be rejected rather than passed through verbatim. - // (URL.init remaps this error.Idna to TypeError for `new URL`.) - try testing.expectError(error.Idna, resolve(testing.arena_allocator, "", "https://xn--0.pt/x", .{})); - try testing.expectError(error.Idna, resolve(testing.arena_allocator, "", "https://xn--a.pt/x", .{})); + try testing.expectError(error.TypeError, resolve(testing.arena_allocator, "https://example.com/", "https://xn--0.pt/x", .{})); + try testing.expectError(error.TypeError, resolve(testing.arena_allocator, "https://example.com/", "https://xn--a.pt/x", .{})); } test "URL: hasAceLabel" { @@ -1333,11 +1344,11 @@ test "URL: resolve with encoding" { .path = "path with multiple spaces", .expected = "https://example.com/path%20with%20%20multiple%20%20%20spaces", }, - // Special characters that need encoding + // Brackets are not in the WHATWG path percent-encode set .{ .base = "https://example.com/", .path = "file[1].html", - .expected = "https://example.com/file%5B1%5D.html", + .expected = "https://example.com/file[1].html", }, .{ .base = "https://example.com/", @@ -1354,20 +1365,24 @@ test "URL: resolve with encoding" { .path = "file\"quote\".html", .expected = "https://example.com/file%22quote%22.html", }, + // Pipe is not in the WHATWG path percent-encode set .{ .base = "https://example.com/", .path = "file|pipe.html", - .expected = "https://example.com/file%7Cpipe.html", + .expected = "https://example.com/file|pipe.html", }, + // Backslash is a path separator in special URLs .{ .base = "https://example.com/", .path = "file\\backslash.html", - .expected = "https://example.com/file%5Cbackslash.html", + .expected = "https://example.com/file/backslash.html", }, + // Note: the current URL spec percent-encodes '^' in paths, but + // rust-url does not (yet); harmless divergence locked in here. .{ .base = "https://example.com/", .path = "file^caret.html", - .expected = "https://example.com/file%5Ecaret.html", + .expected = "https://example.com/file^caret.html", }, .{ .base = "https://example.com/", @@ -1822,31 +1837,31 @@ test "URL: resolve path scheme" { .{ .base = "https://www.example.com/example", .path = "https://about", - .expected = "https://about", + .expected = "https://about/", }, //different schemes and path as absolute (without slash) .{ .base = "https://www.example.com/example", .path = "http:about", - .expected = "http://about", + .expected = "http://about/", }, //different schemes and path as absolute (with one slash) .{ .base = "https://www.example.com/example", .path = "http:/about", - .expected = "http://about", + .expected = "http://about/", }, //different schemes and path as absolute (with two slashes) .{ .base = "https://www.example.com/example", .path = "http://about", - .expected = "http://about", + .expected = "http://about/", }, //same schemes and path as absolute (with more slashes) .{ .base = "https://site/", .path = "https://path", - .expected = "https://path", + .expected = "https://path/", }, //path scheme is not special and path as absolute (without additional slashes) .{ @@ -1858,19 +1873,19 @@ test "URL: resolve path scheme" { .{ .base = "https://www.example.com/example", .path = "ws://about", - .expected = "ws://about", + .expected = "ws://about/", }, //different schemes and path as absolute (path scheme=wss) .{ .base = "https://www.example.com/example", .path = "wss://about", - .expected = "wss://about", + .expected = "wss://about/", }, //different schemes and path as absolute (path scheme=ftp) .{ .base = "https://www.example.com/example", .path = "ftp://about", - .expected = "ftp://about", + .expected = "ftp://about/", }, //different schemes and path as absolute (path scheme=file) .{ diff --git a/src/browser/markdown.zig b/src/browser/markdown.zig index e49a9dc1b..50c0e26cc 100644 --- a/src/browser/markdown.zig +++ b/src/browser/markdown.zig @@ -709,7 +709,7 @@ test "browser.markdown: block link" { \\### Title \\ \\Description - \\[https://example.com](https://example.com) + \\[https://example.com/](https://example.com/) \\ ); } @@ -725,7 +725,7 @@ test "browser.markdown: block link with aria-label" { \\### Title \\ \\Description - \\[Docs](https://example.com) + \\[Docs](https://example.com/) \\ ); } @@ -741,7 +741,7 @@ test "browser.markdown: block link with title" { \\### Title \\ \\Description - \\[Docs](https://example.com) + \\[Docs](https://example.com/) \\ ); } @@ -751,7 +751,7 @@ test "browser.markdown: inline link" { \\

Visit Example.

, \\ - \\Visit [Example](https://example.com). + \\Visit [Example](https://example.com/). \\ ); } diff --git a/src/browser/tests/net/request.html b/src/browser/tests/net/request.html index 2bfa1829e..440a1aac4 100644 --- a/src/browser/tests/net/request.html +++ b/src/browser/tests/net/request.html @@ -125,7 +125,7 @@ headers: { "Sender": "me", "Target": "you" } } ); - testing.expectEqual("https://google.com", request2.url); + testing.expectEqual("https://google.com/", request2.url); testing.expectEqual("POST", request2.method); testing.expectEqual("omit", request2.credentials); testing.expectEqual("reload", request2.cache);