Join URL queries with &

This commit is contained in:
Yoshihiro Tanaka
2023-05-14 11:10:53 +09:00
parent 8693a045bb
commit 418a0132d0
2 changed files with 42 additions and 2 deletions

View File

@@ -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)

View File

@@ -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)
}
}
@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)
}
}