|
| 1 | +require_relative "test_helper" |
| 2 | + |
| 3 | +class TestGenerateUri < TestCase |
| 4 | + def setup |
| 5 | + @client = AzureBlob::Client.new( |
| 6 | + account_name: "testaccount", |
| 7 | + access_key: "ACCESS-KEY", |
| 8 | + container: "test-container" |
| 9 | + ) |
| 10 | + end |
| 11 | + |
| 12 | + def test_generate_uri_with_special_characters |
| 13 | + test_cases = { |
| 14 | + "container/simple.txt" => "https://testaccount.blob.core.windows.net/container/simple.txt", |
| 15 | + "container/path/to/file.txt" => "https://testaccount.blob.core.windows.net/container/path/to/file.txt", |
| 16 | + "container\\backslash\\path.txt" => "https://testaccount.blob.core.windows.net/container/backslash/path.txt", |
| 17 | + "container/file with spaces.txt" => "https://testaccount.blob.core.windows.net/container/file%20with%20spaces.txt", |
| 18 | + "container/file+with+plus.txt" => "https://testaccount.blob.core.windows.net/container/file%2Bwith%2Bplus.txt", |
| 19 | + "container/file?with?question.txt" => "https://testaccount.blob.core.windows.net/container/file%3Fwith%3Fquestion.txt", |
| 20 | + "container/file#with#hash.txt" => "https://testaccount.blob.core.windows.net/container/file%23with%23hash.txt", |
| 21 | + "container/file&with&ersand.txt" => "https://testaccount.blob.core.windows.net/container/file%26with%26ampersand.txt", |
| 22 | + "container/file%with%percent.txt" => "https://testaccount.blob.core.windows.net/container/file%25with%25percent.txt", |
| 23 | + "container/file<with>brackets.txt" => "https://testaccount.blob.core.windows.net/container/file%3Cwith%3Ebrackets.txt", |
| 24 | + "container/file\"with\"quotes.txt" => "https://testaccount.blob.core.windows.net/container/file%22with%22quotes.txt", |
| 25 | + "container/file'with'apostrophe.txt" => "https://testaccount.blob.core.windows.net/container/file%27with%27apostrophe.txt", |
| 26 | + "container/test ?#&<>\"'%+/\\.txt" => "https://testaccount.blob.core.windows.net/container/test%20%3F%23%26%3C%3E%22%27%25%2B//.txt", |
| 27 | + } |
| 28 | + |
| 29 | + test_cases.each do |input, expected| |
| 30 | + uri = @client.send(:generate_uri, input) |
| 31 | + assert_equal expected, uri.to_s, "Failed for input: #{input}" |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + def test_generate_uri_preserves_container_name |
| 36 | + containers = [ |
| 37 | + "mycontainer", |
| 38 | + "my-container", |
| 39 | + "container123", |
| 40 | + "my-container-123", |
| 41 | + ] |
| 42 | + |
| 43 | + containers.each do |container| |
| 44 | + path = "#{container}/file.txt" |
| 45 | + uri = @client.send(:generate_uri, path) |
| 46 | + assert uri.to_s.include?("/#{container}/"), "Container name was incorrectly encoded: #{container}" |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + def test_generate_uri_with_utf8_characters |
| 51 | + uri = @client.send(:generate_uri, "test-container/文件名.txt") |
| 52 | + assert_equal "https://testaccount.blob.core.windows.net/test-container/%E6%96%87%E4%BB%B6%E5%90%8D.txt", uri.to_s |
| 53 | + end |
| 54 | +end |
0 commit comments