|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/util/url_encoder.h" |
| 21 | + |
| 22 | +#include <gtest/gtest.h> |
| 23 | + |
| 24 | +#include "iceberg/test/matchers.h" |
| 25 | + |
| 26 | +namespace iceberg { |
| 27 | + |
| 28 | +TEST(UrlEncoderTest, Encode) { |
| 29 | + // RFC 3986 unreserved characters should not be encoded |
| 30 | + EXPECT_THAT(UrlEncoder::Encode("abc123XYZ"), ::testing::Eq("abc123XYZ")); |
| 31 | + EXPECT_THAT(UrlEncoder::Encode("test-file_name.txt~backup"), |
| 32 | + ::testing::Eq("test-file_name.txt~backup")); |
| 33 | + |
| 34 | + // Spaces and special characters should be encoded |
| 35 | + EXPECT_THAT(UrlEncoder::Encode("hello world"), ::testing::Eq("hello%20world")); |
| 36 | + EXPECT_THAT( UrlEncoder::Encode( "[email protected]"), |
| 37 | + ::testing::Eq("test%40example.com")); |
| 38 | + EXPECT_THAT(UrlEncoder::Encode("path/to/file"), ::testing::Eq("path%2fto%2ffile")); |
| 39 | + EXPECT_THAT(UrlEncoder::Encode("key=value&foo=bar"), |
| 40 | + ::testing::Eq("key%3dvalue%26foo%3dbar")); |
| 41 | + EXPECT_THAT(UrlEncoder::Encode("100%"), ::testing::Eq("100%25")); |
| 42 | + EXPECT_THAT(UrlEncoder::Encode("hello\x1fworld"), ::testing::Eq("hello%1fworld")); |
| 43 | + EXPECT_THAT(UrlEncoder::Encode(""), ::testing::Eq("")); |
| 44 | +} |
| 45 | + |
| 46 | +TEST(UrlEncoderTest, Decode) { |
| 47 | + // Decode percent-encoded strings |
| 48 | + EXPECT_THAT(UrlEncoder::Decode("hello%20world"), ::testing::Eq("hello world")); |
| 49 | + EXPECT_THAT(UrlEncoder::Decode("test%40example.com"), |
| 50 | + ::testing::Eq( "[email protected]")); |
| 51 | + EXPECT_THAT(UrlEncoder::Decode("path%2fto%2Ffile"), ::testing::Eq("path/to/file")); |
| 52 | + EXPECT_THAT(UrlEncoder::Decode("key%3dvalue%26foo%3Dbar"), |
| 53 | + ::testing::Eq("key=value&foo=bar")); |
| 54 | + EXPECT_THAT(UrlEncoder::Decode("100%25"), ::testing::Eq("100%")); |
| 55 | + |
| 56 | + // ASCII Unit Separator (0x1F) |
| 57 | + EXPECT_THAT(UrlEncoder::Decode("hello%1Fworld"), ::testing::Eq("hello\x1Fworld")); |
| 58 | + |
| 59 | + // Unreserved characters remain unchanged |
| 60 | + EXPECT_THAT(UrlEncoder::Decode("test-file_name.txt~backup"), |
| 61 | + ::testing::Eq("test-file_name.txt~backup")); |
| 62 | + EXPECT_THAT(UrlEncoder::Decode(""), ::testing::Eq("")); |
| 63 | +} |
| 64 | + |
| 65 | +TEST(UrlEncoderTest, EncodeDecodeRoundTrip) { |
| 66 | + std::vector<std::string> test_cases = {"hello world", |
| 67 | + |
| 68 | + "path/to/file", |
| 69 | + "key=value&foo=bar", |
| 70 | + "100%", |
| 71 | + "hello\x1Fworld", |
| 72 | + "special!@#$%^&*()chars", |
| 73 | + "mixed-123_test.file~ok", |
| 74 | + ""}; |
| 75 | + |
| 76 | + for (const auto& test : test_cases) { |
| 77 | + std::string encoded = UrlEncoder::Encode(test); |
| 78 | + std::string decoded = UrlEncoder::Decode(encoded); |
| 79 | + EXPECT_EQ(decoded, test) << "Round-trip failed for: " << test; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace iceberg |
0 commit comments