Something went wrong. Try again.
A URI parser and formatter for Zig, modelled on Python's hyperlink package, with LDAP URLs, file: URIs and sdns:// DNS stamps beside it.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Tests for `Uri.relativize`, the inverse of `click`.
const std = @import("std");const uri = @import("root.zig");
const Uri = uri.Uri;const expect = std.testing.expect;const expectEqualStrings = std.testing.expectEqualStrings;
const Case = struct { base: []const u8, target: []const u8, /// The reference `relativize` should produce, as text. rel: []const u8, opts: Uri.RelativizeOptions = .{},};
const cases = [_]Case{ // -- the ordinary shapes ------------------------------------------------ .{ .base = "http://x/a/b", .target = "http://x/a/c", .rel = "c" }, .{ .base = "http://x/a/b", .target = "http://x/d/e", .rel = "../d/e" }, .{ .base = "http://x/a/b/c", .target = "http://x/a/d", .rel = "../d" }, .{ .base = "http://x/a/b/c/d", .target = "http://x/z", .rel = "../../../z" }, .{ .base = "http://x/a/b", .target = "http://x/a/b/c", .rel = "b/c" },
// A trailing empty segment makes the base name a directory, so nothing is // dropped from it and the reference descends directly. .{ .base = "http://x/a/", .target = "http://x/a/b", .rel = "b" }, .{ .base = "http://x/a/", .target = "http://x/b", .rel = "../b" },
// -- same document ------------------------------------------------------ .{ .base = "http://x/a/b", .target = "http://x/a/b", .rel = "" }, .{ .base = "http://x/a/b", .target = "http://x/a/b#top", .rel = "#top" }, .{ .base = "http://x/a/b#here", .target = "http://x/a/b", .rel = "" }, .{ .base = "http://x/a/b", .target = "http://x/a/b?q=1", .rel = "?q=1" }, .{ .base = "http://x/a/b?q=1", .target = "http://x/a/b?q=2", .rel = "?q=2" }, .{ .base = "http://x/a/b?q=1", .target = "http://x/a/b?q=1#f", .rel = "#f" },
// An empty path cannot clear a query -- a reference without one keeps the // base's (RFC 3986 §5.2.2) -- so the path has to be restated. .{ .base = "http://x/a/b?q=1", .target = "http://x/a/b", .rel = "b" }, .{ .base = "http://x/a/?q=1", .target = "http://x/a/", .rel = "./" },
// -- targets that are directories -------------------------------------- .{ .base = "http://x/a/b", .target = "http://x/a/", .rel = "./" }, .{ .base = "http://x/a/b", .target = "http://x/", .rel = "../" }, .{ .base = "http://x/a/b", .target = "http://x/a", .rel = "../a" }, .{ .base = "http://x/a/b/c", .target = "http://x/a/b", .rel = "../b" },
// -- the root ----------------------------------------------------------- .{ .base = "http://x/", .target = "http://x/a", .rel = "a" }, .{ .base = "http://x/a", .target = "http://x/", .rel = "./" }, .{ .base = "http://x", .target = "http://x/a", .rel = "a" },
// -- segments that would be misread ------------------------------------ // A leading empty segment would be written as `/` and read back as a // rooted path, so a `.` guards it. .{ .base = "http://x/a/b", .target = "http://x/a//c", .rel = ".//c" }, // A `:` in the first segment would be read as a scheme; the formatter // escapes it rather than needing a guard segment. .{ .base = "http://x/a/b", .target = "http://x/a/c:d", .rel = "c%3Ad" }, // A percent-encoded dot segment is *not* preserved, because components are // stored decoded: `%2E%2E` and `..` both parse to a segment whose text is // `..`, and resolution removes it. That is a property of the library as a // whole rather than of this function -- `http://x/a/%2E%2E` already writes // back as `http://x/a/..` and normalizes to `http://x/` -- and the // reference computed here agrees with it. .{ .base = "http://x/a/b", .target = "http://x/a/%2E%2E", .rel = "../" },
// -- dot segments in the input ----------------------------------------- // Resolution removes them, so the reference is computed against what they // resolve to. .{ .base = "http://x/a/./b", .target = "http://x/a/c", .rel = "c" }, .{ .base = "http://x/a/b", .target = "http://x/a/d/../c", .rel = "c" },
// -- options ------------------------------------------------------------ .{ .base = "http://x/a/b/c", .target = "http://x/z", .rel = "/z", .opts = .{ .absolute_path = true }, }, .{ .base = "http://x/a/b", .target = "http://y/c", .rel = "//y/c", .opts = .{ .network_path = true }, },
// -- when nothing relative will do -------------------------------------- // A different scheme cannot be reached from a reference that has none. .{ .base = "http://x/a", .target = "https://x/b", .rel = "https://x/b" }, // Nor a different authority, unless asked for. .{ .base = "http://x/a", .target = "http://y/b", .rel = "http://y/b" }, // Credentials are part of the authority. .{ .base = "http://u@x/a", .target = "http://x/b", .rel = "http://x/b" }, // A port is too. .{ .base = "http://x/a", .target = "http://x:8080/b", .rel = "http://x:8080/b" },};
test "relativize produces the expected reference" { const alloc = std.testing.allocator; for (cases) |c| { const base = try Uri.parse(alloc, c.base); defer base.deinit(); const target = try Uri.parse(alloc, c.target); defer target.deinit();
const rel = try base.relativize(alloc, target, c.opts); defer rel.deinit();
const text = try rel.toText(alloc, .{}); defer alloc.free(text);
expectEqualStrings(c.rel, text) catch |err| { std.debug.print("relativize({s}, {s}) -> {s}\n", .{ c.base, c.target, text }); return err; }; }}
test "every case round-trips through click" { const alloc = std.testing.allocator; for (cases) |c| { const base = try Uri.parse(alloc, c.base); defer base.deinit(); const target = try Uri.parse(alloc, c.target); defer target.deinit();
const rel = try base.relativize(alloc, target, c.opts); defer rel.deinit();
try expectRoundTrip(alloc, base, target, rel, c.base, c.target); }}
/// Every URI here is paired with every other, in both directions, and each/// pair has to satisfy the round trip. These are chosen to cover the shapes/// the algorithm has to reason about: empty paths, trailing slashes, the root,/// repeated separators, queries and fragments on either side, and authorities/// that differ in each of the ways they can.const corpus = [_][]const u8{ "http://x", "http://x/", "http://x/a", "http://x/a/", "http://x/a/b", "http://x/a/b/", "http://x/a/b/c", "http://x/a//b", "http://x/a/b?q=1", "http://x/a/b?q=2", "http://x/a/b?q=1&r=2", "http://x/a/b#f", "http://x/a/b?q=1#f", "http://x/a/c:d", "http://x/z", "http://x/a/./b", "http://x/a/../b", "http://x:8080/a/b", "http://u@x/a/b", "http://u:p@x/a/b", "http://y/a/b", "https://x/a/b", "http://[::1]/a/b", "http://192.0.2.1/a/b",};
/// Resolving the target directly and resolving its relativization have to/// agree. Using `click` for both sides is what makes this a real check: the/// left-hand side is the library's own idea of what the target resolves to,/// canonical path and all, so the assertion cannot be satisfied by a/// relativization that merely looks plausible.fn expectRoundTrip( alloc: std.mem.Allocator, base: Uri, target: Uri, rel: Uri, base_text: []const u8, target_text: []const u8,) !void { const want = try base.clickUri(alloc, target); defer want.deinit(); const got = try base.clickUri(alloc, rel); defer got.deinit();
if (!want.eql(got)) { const rel_text = try rel.toText(alloc, .{}); defer alloc.free(rel_text); const want_text = try want.toText(alloc, .{}); defer alloc.free(want_text); const got_text = try got.toText(alloc, .{}); defer alloc.free(got_text); std.debug.print( "base={s} target={s}\n rel = {s}\n want = {s}\n got = {s}\n", .{ base_text, target_text, rel_text, want_text, got_text }, ); return error.RoundTripFailed; }}
test "the round trip holds for every pair in the corpus" { const alloc = std.testing.allocator; const option_sets = [_]Uri.RelativizeOptions{ .{}, .{ .network_path = true }, .{ .absolute_path = true }, .{ .network_path = true, .absolute_path = true }, };
for (corpus) |base_text| { const base = try Uri.parse(alloc, base_text); defer base.deinit();
for (corpus) |target_text| { const target = try Uri.parse(alloc, target_text); defer target.deinit();
for (option_sets) |opts| { const rel = try base.relativize(alloc, target, opts); defer rel.deinit(); try expectRoundTrip(alloc, base, target, rel, base_text, target_text); } } }}
test "a relative result is actually relative" { const alloc = std.testing.allocator; // Same scheme and authority, so every pair here has a relative answer and // none of them may fall back to returning the target. // // `http://x` is deliberately absent: an authority with no path at all is // not reachable by any reference, since resolving one against a base that // has a path yields at least `/`. That case is asserted separately below. const same_site = [_][]const u8{ "http://x/", "http://x/a", "http://x/a/", "http://x/a/", "http://x/a/b", "http://x/a/b/c", "http://x/a//b", "http://x/a/b#f", "http://x/z", "http://x/a/b?q=1", }; for (same_site) |base_text| { const base = try Uri.parse(alloc, base_text); defer base.deinit(); for (same_site) |target_text| { const target = try Uri.parse(alloc, target_text); defer target.deinit();
const rel = try base.relativize(alloc, target, .{}); defer rel.deinit();
if (rel.scheme != null or rel.host != null) { const text = try rel.toText(alloc, .{}); defer alloc.free(text); std.debug.print( "relativize({s}, {s}) stayed absolute: {s}\n", .{ base_text, target_text, text }, ); return error.NotRelative; } } }}
test "a target no reference can reach comes back as itself" { const alloc = std.testing.allocator; // An authority with no path. Every reference resolved against a base that // has a path produces at least `/`, so there is nothing relative that // reaches `http://x`, and the honest answer is the target unchanged. const base = try Uri.parse(alloc, "http://x/a/b"); defer base.deinit(); const target = try Uri.parse(alloc, "http://x"); defer target.deinit();
const rel = try base.relativize(alloc, target, .{}); defer rel.deinit();
try expect(rel.isAbsolute()); const text = try rel.toText(alloc, .{}); defer alloc.free(text); try expectEqualStrings("http://x", text);}
test "relativize is the inverse of click for the RFC 3986 5.4 targets" { const alloc = std.testing.allocator; // The base and the resolved results from RFC 3986 §5.4's worked examples. // Every one of them must be reachable from the base by a reference, which // is the same claim §5.4 makes in the other direction. const base = try Uri.parse(alloc, "http://a/b/c/d;p?q"); defer base.deinit();
const targets = [_][]const u8{ "g:h", "http://a/b/c/g", "http://a/b/c/g/", "http://a/g", "http://g", "http://a/b/c/d;p?y", "http://a/b/c/g?y", "http://a/b/c/d;p?q#s", "http://a/b/c/g#s", "http://a/b/c/g?y#s", "http://a/b/c/;x", "http://a/b/c/g;x", "http://a/b/c/g;x?y#s", "http://a/b/c/", "http://a/b/", "http://a/", "http://a/g", }; for (targets) |target_text| { const target = try Uri.parse(alloc, target_text); defer target.deinit();
const rel = try base.relativize(alloc, target, .{}); defer rel.deinit();
try expectRoundTrip(alloc, base, target, rel, "http://a/b/c/d;p?q", target_text); }}