From 418a0132d033c7556cbd3db665affd6059daa6bf Mon Sep 17 00:00:00 2001 From: Yoshihiro Tanaka Date: Sun, 14 May 2023 11:10:53 +0900 Subject: Join URL queries with & --- tests/core/net/test_core_net.odin | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/core/net/test_core_net.odin b/tests/core/net/test_core_net.odin index 00c29db95..510ac7c64 100644 --- a/tests/core/net/test_core_net.odin +++ b/tests/core/net/test_core_net.odin @@ -67,6 +67,8 @@ main :: proc() { tcp_tests(t) } + join_url_test(t) + fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count) print_tracking_allocator_report() @@ -508,4 +510,36 @@ client_sends_server_data :: proc(t: ^testing.T) { okay = received == CONTENT msg = fmt.tprintf("Expected client to send \"{}\", got \"{}\"", CONTENT, received) expect(t, okay, msg) -} \ No newline at end of file +} + +@test +join_url_test :: proc(t: ^testing.T) { + URL_Join_Test :: struct { + scheme, host, path: string, + queries: map[string]string, + expected: string, + } + test_cases := []URL_Join_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" }, + } + + for test in test_cases { + url := net.join_url(test.scheme, test.host, test.path, test.queries) + defer { + delete(test.queries) + delete(url) + } + + okay := url == test.expected + msg := fmt.tprintf("Expected `net.join_url` to return %s, got %s", test.expected, url) + expect(t, okay, msg) + } +} -- cgit v1.2.3