Files
rosetta/tests/codecs/url.luau
T

104 lines
4.8 KiB
Luau

-- codecs/text/Url + Text tests: RFC 3986 5.4 examples, urljoin against Python's urllib, cp1252 against Python.
-- Fixtures: tools/fixtures/playlist.py.
local T = require("@tests/T")
local Url = require("@rosetta/codecs/text/Url")
local Text = require("@rosetta/codecs/text/Text")
local Ref = require("@tests/gen/playlist_url")
local Cp = require("@tests/gen/playlist_cp1252")
local t = T.suite("url")
local function eq(got: any, want: any, label: string)
t:eq(got, want, label)
end
local BASE = "http://a/b/c/d;p?q"
local cases = {
-- 5.4.1 normal
{ "g:h", "g:h" }, { "g", "http://a/b/c/g" }, { "./g", "http://a/b/c/g" }, { "g/", "http://a/b/c/g/" },
{ "/g", "http://a/g" }, { "//g", "http://g" }, { "?y", "http://a/b/c/d;p?y" }, { "g?y", "http://a/b/c/g?y" },
{ "#s", "http://a/b/c/d;p?q#s" }, { "g#s", "http://a/b/c/g#s" }, { "g?y#s", "http://a/b/c/g?y#s" },
{ ";x", "http://a/b/c/;x" }, { "g;x", "http://a/b/c/g;x" }, { "g;x?y#s", "http://a/b/c/g;x?y#s" },
{ "", "http://a/b/c/d;p?q" }, { ".", "http://a/b/c/" }, { "./", "http://a/b/c/" }, { "..", "http://a/b/" },
{ "../", "http://a/b/" }, { "../g", "http://a/b/g" }, { "../..", "http://a/" }, { "../../", "http://a/" },
{ "../../g", "http://a/g" },
-- 5.4.2 abnormal
{ "../../../g", "http://a/g" }, { "../../../../g", "http://a/g" }, { "/./g", "http://a/g" },
{ "/../g", "http://a/g" }, { "g.", "http://a/b/c/g." }, { ".g", "http://a/b/c/.g" }, { "g..", "http://a/b/c/g.." },
{ "..g", "http://a/b/c/..g" }, { "./../g", "http://a/b/g" }, { "./g/.", "http://a/b/c/g/" },
{ "g/./h", "http://a/b/c/g/h" }, { "g/../h", "http://a/b/c/h" }, { "g;x=1/./y", "http://a/b/c/g;x=1/y" },
{ "g;x=1/../y", "http://a/b/c/y" }, { "g?y/./x", "http://a/b/c/g?y/./x" }, { "g?y/../x", "http://a/b/c/g?y/../x" },
{ "g#s/./x", "http://a/b/c/g#s/./x" }, { "g#s/../x", "http://a/b/c/g#s/../x" }, { "http:g", "http:g" },
}
for _, c in cases do
eq(Url.resolve(c[1], BASE), c[2], "resolve " .. c[1])
end
-- base with an empty path
eq(Url.resolve("g", "http://a"), "http://a/g", "empty base path")
eq(Url.resolve("g", "http://a?x"), "http://a/g", "empty base path with query")
eq(Url.removeDotSegments("/a/b/c/./../../g"), "/a/g", "remove_dot_segments 1")
eq(Url.removeDotSegments("mid/content=5/../6"), "mid/6", "remove_dot_segments 2")
local p = Url.split("https://user@host:8080/p/a.mp3?x=1&y#frag")
eq(p.scheme, "https", "split scheme")
eq(p.authority, "user@host:8080", "split authority")
eq(p.path, "/p/a.mp3", "split path")
eq(p.query, "x=1&y", "split query")
eq(p.fragment, "frag", "split fragment")
eq(Url.join(p), "https://user@host:8080/p/a.mp3?x=1&y#frag", "join")
local q = Url.split("a/b")
eq(q.scheme, nil, "relative has no scheme")
eq(q.authority, nil, "relative has no authority")
eq(Url.encodeUnsafe("a b/ü%20?x=\"1\"#f"), "a%20b/%C3%BC%20?x=%221%22#f", "encodeUnsafe")
eq(Url.isWindowsPath("C:\\Music\\a.mp3"), true, "windows drive")
eq(Url.isWindowsPath("d:/x.mp3"), true, "windows drive with slash")
eq(Url.isWindowsPath("\\\\server\\share\\a.mp3"), true, "UNC")
eq(Url.isWindowsPath("http://x/a"), false, "http is not windows")
eq(Url.scheme("C:\\a.mp3"), nil, "a drive letter is not a scheme")
eq(Url.scheme("HTTPS://x"), "https", "scheme lower-cased")
eq(Url.scheme("music/a.mp3"), nil, "relative has no scheme")
for _, c in Ref do
eq(Url.resolve(c[1], c[2]), c[3], "urljoin " .. c[1] .. " against " .. c[2])
end
t:note("%d random references resolve as urllib.parse.urljoin", #Ref)
do
local w = {}
local base, scheme = Url.base("https://h/a/list.m3u", w)
t:check(base == "https://h/a/list.m3u" and scheme == "https", "base")
local l = Url.locate(" b c.mp3 ", base, scheme)
t:check(l.url == "https://h/a/b%20c.mp3" and l.playable, "locate relative")
t:check(Url.locate("C:\\x.mp3", base, scheme).reason == "local path", "locate windows")
t:check(Url.locate("ftp://x/y", base, scheme).reason == "scheme ftp", "locate ftp")
local nb, ns = Url.base("lists/x.m3u", w)
t:check(nb == nil and ns == nil and #w == 1, "a relative base is ignored with a warning")
t:check(select(2, Url.base("C:\\Music\\l.m3u")) == "windows", "windows base")
end
do
for _, c in Cp do
eq(Text.toUtf8("x" .. string.char(c[1]) .. "y"), "x" .. c[2] .. "y", string.format("cp1252 0x%02x", c[1]))
end
local w = {}
Text.toUtf8("\150", w)
t:check(#w == 1, "cp1252 warning")
eq(Text.toUtf8("\239\187\191abc"), "abc", "BOM stripped")
eq(Text.toUtf8("h\u{e9}"), "h\u{e9}", "UTF-8 kept")
local ls = Text.lines("a\r\nb\rc\nd")
t:check(#ls == 4 and ls[1] == "a" and ls[2] == "b" and ls[3] == "c" and ls[4] == "d", "lines")
eq(Text.trim(" x y \t"), "x y", "trim")
end
t:fuzz("resolve + locate", function(b: buffer)
local s = buffer.tostring(b)
Url.resolve(s, "http://a/b/c/d;p?q")
Url.resolve("g/../h", s)
Url.locate(s, "http://h/", "http")
return Text.lines(Text.toUtf8(s))
end, { "http://a/b/../c?x#y", "C:\\a\\b.mp3", "\239\187\191#EXTM3U\r\n" }, { mutations = 30 })
t:done()