// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-FileCopyrightText: © 2017 the hyperlink authors // SPDX-License-Identifier: MIT //! Test cases taken from the suite of Python's `hyperlink`, the package this //! library is modelled on, at version 21.0.0 -- its `ROUNDTRIP_TESTS` list //! plus the inputs its individual tests use to pin down corner cases. Both //! projects are MIT-licensed. //! //! They earn their place because they are someone else's idea of what is //! pathological: percent-encoded delimiters in every position that has one, //! address literals of every shape, schemes that do and do not use an //! authority, and a handful of inputs that exist only because they were once //! bugs. Where the two libraries disagree the difference is pinned here too, //! so that a deliberate divergence cannot drift into an accidental one. const std = @import("std"); const uri = @import("root.zig"); const Uri = uri.Uri; const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectEqualStrings = std.testing.expectEqualStrings; /// `hyperlink`'s `ROUNDTRIP_TESTS`, which it asserts survive /// `URL.from_text(t).to_text() == t`. That is a stronger claim than this /// library makes -- components are stored decoded here, so output is /// canonically rather than identically escaped -- so what is checked is the /// round trip through *components*, plus that writing is a fixed point. const hyperlink_roundtrip = [_][]const u8{ "http://localhost", "http://localhost/", "http://127.0.0.1/", "http://[::127.0.0.1]/", "http://[::1]/", "http://localhost/foo", "http://localhost/foo/", "http://localhost/foo!!bar/", "http://localhost/foo%20bar/", "http://localhost/foo%2Fbar/", "http://localhost/foo?n", "http://localhost/foo?n=v", "http://localhost/foo?n=/a/b", "http://example.com/foo!@$bar?b!@z=123", "http://localhost/asd?a=asd%20sdf/345", "http://(%2525)/(%2525)?(%2525)&(%2525)=(%2525)#(%2525)", "http://(%C3%A9)/(%C3%A9)?(%C3%A9)&(%C3%A9)=(%C3%A9)#(%C3%A9)", "?sslrootcert=/Users/glyph/Downloads/rds-ca-2015-root.pem&sslmode=verify", "http://googlewebsite.com/e-shops.aspx", "http://example.com:8080/search?q=123&business=Nothing%20Special", "http://hatnote.com:9000/?arg=1&arg=2&arg=3", "https://xn--bcher-kva.ch", "http://xn--ggbla1c4e.xn--ngbc5azd/", "http://tools.ietf.org/html/rfc3986#section-3.4", "ftp://ftp.rfc-editor.org/in-notes/tar/RFCs0001-0500.tar.gz", "http://[1080:0:0:0:8:800:200C:417A]/index.html", "ssh://192.0.2.16:2222/", "https://[::101.45.75.219]:80/?hi=bye", "ldap://[::192.9.5.5]/dc=example,dc=com??sub?(sn=Jensen)", "mailto:me@example.com?to=me@example.com&body=hi%20http://wikipedia.org", "news:alt.rec.motorcycle", "tel:+1-800-867-5309", "urn:oasis:member:A00024:x", "magnet:?xt=urn:btih:1a42b9e04e122b97a5254e3df77ab3c4b7da725f&dn=Puppy%20Linux%20precise-5.7.1.iso&tr=udp://tracker.openbittorrent.com:80&tr=udp://tracker.publicbt.com:80&tr=udp://tracker.istole.it:6969&tr=udp://tracker.ccc.de:80&tr=udp://open.demonii.com:1337", "https://%3A@example.com/", "https://%40@example.com/", "https://%2f@example.com/", "https://a:%3a@example.com/", "https://a:%40@example.com/", "https://a:%2f@example.com/", "https://a:%3f@example.com/", "https://example.com/%2F/", "https://example.com/%3F/", "https://example.com/%23/", "https://example.com/?%23=b", "https://example.com/?%3D=b", "https://example.com/?%26=b", "https://example.com/?a=%23", "https://example.com/?a=%26", "https://example.com/?a=%3D", "https://example.com/?foo+bar=baz", "https://example.com/?foo=bar+baz", "http://(%2525):(%2525)@example.com/(%2525)/?(%2525)=(%2525)#(%2525)", "first_seg_rel_path__colon%3Anotok/second_seg__colon%3Aok", }; test "hyperlink: the round-trip corpus survives a write/read cycle" { const alloc = std.testing.allocator; for (hyperlink_roundtrip) |text| { const a = Uri.parse(alloc, text) catch |err| { std.debug.print("\nparsing {s} failed\n", .{text}); return err; }; defer a.deinit(); const once = try a.toText(alloc, .{ .with_password = true }); defer alloc.free(once); const b = try Uri.parse(alloc, once); defer b.deinit(); expect(a.eql(b)) catch |err| { std.debug.print("\ncomponents changed: {s} -> {s}\n", .{ text, once }); return err; }; try expectEqual(a.hash(), b.hash()); const twice = try b.toText(alloc, .{ .with_password = true }); defer alloc.free(twice); expectEqualStrings(once, twice) catch |err| { std.debug.print("\nunstable output from: {s}\n", .{text}); return err; }; } } test "hyperlink: where the canonical form differs" { // Every case here is `hyperlink` writing back what it was given and this // library writing the canonical form of the same components. All of them // follow from storing components decoded and re-escaping with the largest // set that cannot change how the result re-parses. const alloc = std.testing.allocator; for ([_][2][]const u8{ // Hex digits are uppercased (RFC 3986 §6.2.2.1). .{ "https://%2f@example.com/", "https://%2F@example.com/" }, .{ "https://a:%2f@example.com/", "https://a:%2F@example.com/" }, .{ "https://a:%3f@example.com/", "https://a:%3F@example.com/" }, // An escape that need not be one is written out: a `:` is // unambiguous in a password, an `=` in a query value, and a `:` in // any path segment but the first of a relative reference. .{ "https://a:%3a@example.com/", "https://a::@example.com/" }, .{ "https://example.com/?a=%3D", "https://example.com/?a==" }, .{ "first_seg_rel_path__colon%3Anotok/second_seg__colon%3Aok", "first_seg_rel_path__colon%3Anotok/second_seg__colon:ok", }, // Unreserved characters are never escaped, however they arrived. .{ "HTTP://Example.com/A%61/./../A%61?B%62=C%63#D%64", "HTTP://Example.com/Aa/./../Aa?Bb=Cc#Dd", }, // A URI is ASCII; `hyperlink`'s `to_text` is closer to an IRI. .{ "http://bücher.ch", "http://b%C3%BCcher.ch" }, }) |c| { const u = try Uri.parse(alloc, c[0]); defer u.deinit(); const text = try u.toText(alloc, .{ .with_password = true }); defer alloc.free(text); expectEqualStrings(c[1], text) catch |err| { std.debug.print("\nwriting {s}\n", .{c[0]}); return err; }; } // The IRI form is where the non-ASCII goes back to being literal. Note // that a space stays escaped: RFC 3987 does not admit one, though // `hyperlink`'s `to_iri` writes it out. const u = try Uri.parse(alloc, "http://(%C3%A9)/(%C3%A9)?(%C3%A9)#(%C3%A9)"); defer u.deinit(); const iri = try u.toText(alloc, .{ .form = .iri }); defer alloc.free(iri); try expectEqualStrings("http://(é)/(é)?(é)#(é)", iri); } test "hyperlink: input this library accepts and hyperlink rejects" { const alloc = std.testing.allocator; // A control character is not URI syntax, but rejecting the input is one // choice and escaping it on the way out is another. Browsers take the // second, and so does this library: the result is a conforming URI. const control = try Uri.parse(alloc, "#\n\n"); defer control.deinit(); try expectEqualStrings("\n\n", control.fragment.?); const control_text = try control.toText(alloc, .{}); defer alloc.free(control_text); try expectEqualStrings("#%0A%0A", control_text); // RFC 3986 §3.2.3 allows an authority to end in a bare `:` with no port, // and browsers accept it. `hyperlink` raises. const empty_port = try Uri.parse(alloc, "http://127.0.0.1:"); defer empty_port.deinit(); try expectEqual(@as(?u16, null), empty_port.port); const empty_port_text = try empty_port.toText(alloc, .{}); defer alloc.free(empty_port_text); try expectEqualStrings("http://127.0.0.1", empty_port_text); } test "hyperlink: authority and address-literal inputs it rejects too" { const alloc = std.testing.allocator; // `test_from_text_bad_authority` and `test_invalid_ipv6`. for ([_][]const u8{ "http://[::1/", "http://[[::1]/", "http://[::1]]/", "http://127.0.0.1:hi", "http://127.0.0.1::80", "http://[2001::0234:C1ab::A0:aabc:003F]", "http://[2001::1::3F]", "http://[:]", "http://[::::]", "http://[::256.0.0.1]", "ftp://portmouth:smash", // Not an address literal at all -- there is no opening bracket -- so // this reads as an empty registered name followed by a port of `:1]`, // and it is the port that rejects it. "http://::1]/", }) |text| { if (Uri.parse(alloc, text)) |u| { defer u.deinit(); std.debug.print("\n{s} parsed but should not have\n", .{text}); return error.TestUnexpectedResult; } else |_| {} } }