aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYoshihiro Tanaka <contact@cordea.jp>2023-05-14 11:49:19 +0900
committerYoshihiro Tanaka <contact@cordea.jp>2023-05-14 12:15:20 +0900
commit59e66ffe49fdceba919bd4faaa739254cd4c8819 (patch)
treee541c8ae0e9b9dda99c3e8cf82350aefde1802e6
parent418a0132d033c7556cbd3db665affd6059daa6bf (diff)
Add test for net.split_url
-rw-r--r--tests/core/net/test_core_net.odin58
1 files changed, 49 insertions, 9 deletions
diff --git a/tests/core/net/test_core_net.odin b/tests/core/net/test_core_net.odin
index 510ac7c64..14e1cbb17 100644
--- a/tests/core/net/test_core_net.odin
+++ b/tests/core/net/test_core_net.odin
@@ -67,6 +67,7 @@ main :: proc() {
tcp_tests(t)
}
+ split_url_test(t)
join_url_test(t)
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
@@ -512,14 +513,53 @@ client_sends_server_data :: proc(t: ^testing.T) {
expect(t, okay, msg)
}
+URL_Test :: struct {
+ scheme, host, path: string,
+ queries: map[string]string,
+ url: string,
+}
+
@test
-join_url_test :: proc(t: ^testing.T) {
- URL_Join_Test :: struct {
- scheme, host, path: string,
- queries: map[string]string,
- expected: string,
+split_url_test :: proc(t: ^testing.T) {
+ test_cases := []URL_Test{
+ { "http", "example.com", "/", {}, "http://example.com" },
+ { "https", "odin-lang.org", "/", {}, "https://odin-lang.org" },
+ { "https", "odin-lang.org", "/docs/", {}, "https://odin-lang.org/docs/" },
+ { "https", "odin-lang.org", "/docs/overview", {}, "https://odin-lang.org/docs/overview" },
+ { "http", "example.com", "/", {"a" = "b"}, "http://example.com?a=b" },
+ { "http", "example.com", "/", {"a" = ""}, "http://example.com?a" },
+ { "http", "example.com", "/", {"a" = "b", "c" = "d"}, "http://example.com?a=b&c=d" },
+ { "http", "example.com", "/", {"a" = "", "c" = "d"}, "http://example.com?a&c=d" },
+ { "http", "example.com", "/example", {"a" = "", "b" = ""}, "http://example.com/example?a&b" },
}
- test_cases := []URL_Join_Test{
+
+ for test in test_cases {
+ scheme, host, path, queries := net.split_url(test.url)
+ defer {
+ delete(queries)
+ delete(test.queries)
+ }
+
+ msg := fmt.tprintf("Expected `net.split_url` to return %s, got %s", test.scheme, scheme)
+ expect(t, scheme == test.scheme, msg)
+ msg = fmt.tprintf("Expected `net.split_url` to return %s, got %s", test.host, host)
+ expect(t, host == test.host, msg)
+ msg = fmt.tprintf("Expected `net.split_url` to return %s, got %s", test.path, path)
+ expect(t, path == test.path, msg)
+ msg = fmt.tprintf("Expected `net.split_url` to return %d queries, got %d queries", len(test.queries), len(queries))
+ expect(t, len(queries) == len(test.queries), msg)
+ for k, v in queries {
+ expected := test.queries[k]
+ msg = fmt.tprintf("Expected `net.split_url` to return %s, got %s", expected, v)
+ expect(t, v == expected, msg)
+ }
+ }
+}
+
+
+@test
+join_url_test :: proc(t: ^testing.T) {
+ test_cases := []URL_Test{
{ "http", "example.com", "", {}, "http://example.com" },
{ "https", "odin-lang.org", "", {}, "https://odin-lang.org" },
{ "https", "odin-lang.org", "docs/", {}, "https://odin-lang.org/docs/" },
@@ -534,12 +574,12 @@ join_url_test :: proc(t: ^testing.T) {
for test in test_cases {
url := net.join_url(test.scheme, test.host, test.path, test.queries)
defer {
- delete(test.queries)
delete(url)
+ delete(test.queries)
}
- okay := url == test.expected
- msg := fmt.tprintf("Expected `net.join_url` to return %s, got %s", test.expected, url)
+ okay := url == test.url
+ msg := fmt.tprintf("Expected `net.join_url` to return %s, got %s", test.url, url)
expect(t, okay, msg)
}
}