# 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.view() == "C%3A/a%20b/%E6%97%A5~x"); val decoded := uri::decode(encoded.view()) else (_) { t.expects(false); return; }; t.expects(decoded.view() == "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.view() == "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.view() == "100% of %zz, %4, my notes%"); val plain := uri::decode_lenient("c%3A/x"); t.expects(plain.view() == "c:/x"); t.expects(uri::decode_lenient("").length == 0); }