commit c445e60fc732450da4c6b530fcecad1511b2c3b2
parent 7aeb37b9291072f5fdad9b7e7864b4de65a066bc
Author: Tom Lebreux <me@tomlebreux.com>
Date: Mon, 30 May 2022 17:51:34 -0400
net::uri: add fmt tests
Signed-off-by: Tom Lebreux <me@tomlebreux.com>
Diffstat:
M | net/uri/+test.ha | | | 108 | ++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------- |
1 file changed, 68 insertions(+), 40 deletions(-)
diff --git a/net/uri/+test.ha b/net/uri/+test.ha
@@ -1,7 +1,17 @@
+use fmt;
use net::ip;
-@test fn parse() void = {
- test_uri(
+@test fn roundtrip() void = {
+ test_uri_roundtrip(
+ "file:///my/path/to/file",
+ uri {
+ scheme = "file",
+ host = "",
+ path = "/my/path/to/file",
+ ...
+ },
+ )!;
+ test_uri_roundtrip(
"http://harelang.org/",
uri {
scheme = "http",
@@ -10,7 +20,7 @@ use net::ip;
...
},
)!;
- test_uri(
+ test_uri_roundtrip(
"irc+insecure://chat.sr.ht:6667",
uri {
scheme = "irc+insecure",
@@ -19,7 +29,7 @@ use net::ip;
...
},
)!;
- test_uri(
+ test_uri_roundtrip(
"ldap://13.37.73.31:1234/",
uri {
scheme = "ldap",
@@ -29,7 +39,7 @@ use net::ip;
...
},
)!;
- test_uri(
+ test_uri_roundtrip(
"http://[::1]/test",
uri {
scheme = "http",
@@ -39,7 +49,36 @@ use net::ip;
},
)!;
- // Test percent decoding in various places
+ // Some non-URL variants like mailto: or URN
+ test_uri_roundtrip(
+ "urn:example:animal:ferret:nose",
+ uri {
+ scheme = "urn",
+ host = "",
+ path = "example:animal:ferret:nose",
+ ...
+ },
+ )!;
+ test_uri_roundtrip(
+ "mailto:~sircmpwn/hare-dev@lists.sr.ht",
+ uri {
+ scheme = "mailto",
+ host = "",
+ path = "~sircmpwn/hare-dev@lists.sr.ht",
+ ...
+ },
+ )!;
+
+};
+
+@test fn invalid() void = {
+ // Scheme
+ assert(parse(":") is invalid);
+ assert(parse("hello*:") is invalid);
+ assert(parse("hello") is invalid);
+};
+
+@test fn percent_encoding() void = {
test_uri(
"https://git%2esr.ht/~sircmpw%6e/hare#Build%20status",
uri {
@@ -49,6 +88,7 @@ use net::ip;
fragment = "Build status",
...
},
+ "https://git.sr.ht/~sircmpwn/hare#Build%20status",
)!;
// IPv6
@@ -61,26 +101,7 @@ use net::ip;
query = "objectClass?one",
...
},
- )!;
-
- // Some non-URL variants like mailto: or URN
- test_uri(
- "urn:example:animal:ferret:nose",
- uri {
- scheme = "urn",
- host = "",
- path = "example:animal:ferret:nose",
- ...
- },
- )!;
- test_uri(
- "mailto:~sircmpwn/hare-dev@lists.sr.ht",
- uri {
- scheme = "mailto",
- host = "",
- path = "~sircmpwn/hare-dev@lists.sr.ht",
- ...
- },
+ "ldap://[2001:db8::7]/c=GB?objectClass%3Fone",
)!;
// https://bugs.chromium.org/p/chromium/issues/detail?id=841105
@@ -92,30 +113,37 @@ use net::ip;
path = "/..;@www.google.com:443",
...
},
+ "https://web-safety.net/..;@www.google.com:443",
)!;
};
-@test fn invalid() void = {
- // Scheme
- assert(parse(":") is invalid);
- assert(parse("hello*:") is invalid);
- assert(parse("hello") is invalid);
+fn test_uri_roundtrip(in: str, expected: uri) (void | invalid) = {
+ test_uri(in, expected, in)?;
};
-fn test_uri(in: str, expected: uri) (void | invalid) = {
+fn test_uri(in: str, expected_uri: uri, expected_str: str) (void | invalid) = {
const u = parse(in)?;
defer finish(&u);
- assert(u.scheme == expected.scheme);
+ assert(u.scheme == expected_uri.scheme);
match (u.host) {
case let s: str =>
- assert(s == expected.host as str);
+ assert(s == expected_uri.host as str);
case let i: ip::addr =>
- assert(ip::equal(i, expected.host as ip::addr));
+ assert(ip::equal(i, expected_uri.host as ip::addr));
+ };
+ assert(u.port == expected_uri.port);
+ assert(u.userinfo == expected_uri.userinfo);
+ assert(u.path == expected_uri.path);
+ assert(u.query == expected_uri.query);
+ assert(u.fragment == expected_uri.fragment);
+
+ const s = string(&u);
+ defer free(s);
+
+ if (s != expected_str) {
+ fmt::errorfln("=== wanted\n{}", expected_str)!;
+ fmt::errorfln("=== got\n{}", s)!;
+ abort();
};
- assert(u.port == expected.port);
- assert(u.userinfo == expected.userinfo);
- assert(u.path == expected.path);
- assert(u.query == expected.query);
- assert(u.fragment == expected.fragment);
};