// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // 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); } }