Files
std/tests/net/uri.sakura
T
2026-11-13 21:54:22 -06:00

28 lines
1.1 KiB
Plaintext

# Tests of percent-encoding, as URIs hold text within their parts.
import std::net;
import std::testing;
@test def uri_text_is_percent_encoded_and_decoded(t: &mut tester): void {
val encoded := uri::encode("C:/a b/日~x", "/");
t.expects(encoded.span == "C%3A/a%20b/%E6%97%A5~x");
val decoded := uri::decode(encoded.span) else (_) { t.expects(false); return; };
t.expects(decoded.span == "C:/a b/日~x");
# Strictly, a `%` two hexadecimal digits do not follow is an error.
t.expects(uri::decode("100%") is error);
t.expects(uri::decode("%4") is error);
t.expects(uri::decode("%zz") is error);
val mixed := uri::decode("%4a%4A+") else (_) { t.expects(false); return; };
t.expects(mixed.span == "JJ+");
}
@test def uri_text_is_decoded_leniently_where_asked(t: &mut tester): void {
# Leniently, such a `%` is kept as it is, and the rest decoded.
val kept := uri::decode_lenient("100%25 of %zz, %4, my%20notes%");
t.expects(kept.span == "100% of %zz, %4, my notes%");
val plain := uri::decode_lenient("c%3A/x");
t.expects(plain.span == "c:/x");
t.expects(uri::decode_lenient("").length == 0);
}