From 418a0132d033c7556cbd3db665affd6059daa6bf Mon Sep 17 00:00:00 2001 From: Yoshihiro Tanaka Date: Sun, 14 May 2023 11:10:53 +0900 Subject: [PATCH] Join URL queries with & --- core/net/url.odin | 8 ++++++- tests/core/net/test_core_net.odin | 36 ++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/core/net/url.odin b/core/net/url.odin index 460bb70ad..b529ca082 100644 --- a/core/net/url.odin +++ b/core/net/url.odin @@ -78,13 +78,19 @@ join_url :: proc(scheme, host, path: string, queries: map[string]string, allocat } - if len(queries) > 0 do write_string(&b, "?") + query_length := len(queries) + if query_length > 0 do write_string(&b, "?") + i := 0 for query_name, query_value in queries { write_string(&b, query_name) if query_value != "" { write_string(&b, "=") write_string(&b, query_value) } + if i < query_length - 1 { + write_string(&b, "&") + } + i += 1 } return to_string(b) 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) + } +}