v2 / vlib / net / s3 / encoding_test.v
49 lines · 41 sloc · 1.55 KB · 4142432483c4e8de44ab7b0d6ac944f3251e03c8
Raw
1// Copyright (c) 2019-2026 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module s3
5
6fn test_uri_encode_path_keeps_slashes() {
7 assert uri_encode_path('/foo/bar baz/test.txt') == '/foo/bar%20baz/test.txt'
8 assert uri_encode_path('/folder/file with %.txt') == '/folder/file%20with%20%25.txt'
9}
10
11fn test_uri_encode_path_normalises_backslashes() {
12 // Windows-style separators must collapse to '/' in the canonical key.
13 assert uri_encode_path('foo\\bar\\baz.txt') == 'foo/bar/baz.txt'
14}
15
16fn test_uri_encode_query_encodes_slashes() {
17 assert uri_encode_query('a/b') == 'a%2Fb'
18 assert uri_encode_query('hello world') == 'hello%20world'
19}
20
21fn test_uri_encode_unreserved_passthrough() {
22 // RFC 3986 unreserved set must stay intact.
23 assert uri_encode('AZaz09-_.~', false) == 'AZaz09-_.~'
24}
25
26fn test_uri_encode_percent_uppercase() {
27 // AWS SigV4 mandates uppercase hex digits.
28 assert uri_encode('é', true) == '%C3%A9'
29}
30
31fn test_strip_slashes_trims_both_ends() {
32 assert strip_slashes('/foo/bar/') == 'foo/bar'
33 assert strip_slashes('//foo//') == 'foo'
34 assert strip_slashes('foo') == 'foo'
35 assert strip_slashes('') == ''
36 assert strip_slashes('///') == ''
37}
38
39fn test_contains_crlf() {
40 assert contains_crlf('hello\r\nworld') == true
41 assert contains_crlf('hello\rworld') == true
42 assert contains_crlf('hello\nworld') == true
43 assert contains_crlf('plain') == false
44}
45
46fn test_to_hex_lower() {
47 assert to_hex_lower([u8(0x00), 0x0f, 0xff, 0xa0]) == '000fffa0'
48 assert to_hex_lower([]) == ''
49}
50