Gemenon.jl/test/URIs.jl

61 lines
2.3 KiB
Julia

using Test
unescape_expected = [
"String without percent" => "String without percent",
"String%20with%20spaces" => "String with spaces",
"String%20with some spaces" => "String with some spaces",
"String%20with invalid end%" => "String with invalid end%",
"String%20with invalid end%a" => "String with invalid end%a",
"String%20with valid end%aa" => "String with valid end\xaa",
"String%20with invalid%-- data" => "String with invalid%-- data",
"Lech_Kaczy%C5%84ski" => "Lech_Kaczyński"
]
@testset "Unescape Query String" begin
for t in unescape_expected
@test unescape(t[1]) == t[2]
end
end
function test_request(input::String, params)
url = Request(input)
@test url.protocol == params[1]
@test url.host == params[2]
@test url.port == params[3]
@test url.path == params[4]
@test url.query == params[5]
end
@testset "Parse urls" begin
@testset "Complete url" begin
test_request("https://hostname:999/a?b",
("https", "hostname", 999, ["a"], "b"))
end
@testset "Defaults" begin
@testset "Default protocol" begin
test_request("hostname:999/a?b",
("gemini", "hostname", 999, ["a"], "b"))
end
@testset "Default port" begin
test_request("gemini://hostname/a?b",
("gemini", "hostname", 1965, ["a"], "b"))
end
end
@testset "Paths" begin
test_request("hostname/a/b/c?b",
("gemini", "hostname", 1965, ["a", "b", "c"], "b"))
test_request("hostname/a/./c?b",
("gemini", "hostname", 1965, ["a", "c"], "b"))
# Not sure if this is expected or if we should remove empty...
test_request("hostname/a/b/c/..?b",
("gemini", "hostname", 1965, ["a", "b", ""], "b"))
end
@testset "Query" begin
test_request("hostname/a/b/c",
("gemini", "hostname", 1965, ["a", "b", "c"], nothing))
test_request("hostname/a/b/c?example",
("gemini", "hostname", 1965, ["a", "b", "c"], "example"))
test_request("hostname/a/b/c?percent%20encoding",
("gemini", "hostname", 1965, ["a", "b", "c"], "percent encoding"))
end
end