|
| 1 | +# Copyright (c) MultiSafepay, Inc. All rights reserved. |
| 2 | + |
| 3 | +# This file is licensed under the Open Software License (OSL) version 3.0. |
| 4 | +# For a copy of the license, see the LICENSE.txt file in the project root. |
| 5 | + |
| 6 | +# See the DISCLAIMER.md file for disclaimer details. |
| 7 | + |
| 8 | + |
| 9 | +from multisafepay.api.base.abstract_manager import AbstractManager |
| 10 | + |
| 11 | + |
| 12 | +def test_encode_path_segment_with_normal_string(): |
| 13 | + """Test encoding a normal string without special characters.""" |
| 14 | + result = AbstractManager.encode_path_segment("normal_string") |
| 15 | + assert result == "normal_string" |
| 16 | + |
| 17 | + |
| 18 | +def test_encode_path_segment_with_spaces(): |
| 19 | + """Test encoding a string with spaces.""" |
| 20 | + result = AbstractManager.encode_path_segment("hello world") |
| 21 | + assert result == "hello%20world" |
| 22 | + |
| 23 | + |
| 24 | +def test_encode_path_segment_with_special_characters(): |
| 25 | + """Test encoding a string with various special characters.""" |
| 26 | + result = AbstractManager.encode_path_segment("hello@world#test") |
| 27 | + assert result == "hello%40world%23test" |
| 28 | + |
| 29 | + |
| 30 | +def test_encode_path_segment_with_forward_slash(): |
| 31 | + """Test encoding a string with forward slashes.""" |
| 32 | + result = AbstractManager.encode_path_segment("path/to/resource") |
| 33 | + assert result == "path%2Fto%2Fresource" |
| 34 | + |
| 35 | + |
| 36 | +def test_encode_path_segment_with_question_mark(): |
| 37 | + """Test encoding a string with question marks.""" |
| 38 | + result = AbstractManager.encode_path_segment("query?param=value") |
| 39 | + assert result == "query%3Fparam%3Dvalue" |
| 40 | + |
| 41 | + |
| 42 | +def test_encode_path_segment_with_ampersand(): |
| 43 | + """Test encoding a string with ampersands.""" |
| 44 | + result = AbstractManager.encode_path_segment("param1¶m2") |
| 45 | + assert result == "param1%26param2" |
| 46 | + |
| 47 | + |
| 48 | +def test_encode_path_segment_with_equals_sign(): |
| 49 | + """Test encoding a string with equals signs.""" |
| 50 | + result = AbstractManager.encode_path_segment("key=value") |
| 51 | + assert result == "key%3Dvalue" |
| 52 | + |
| 53 | + |
| 54 | +def test_encode_path_segment_with_percentage_sign(): |
| 55 | + """Test encoding a string with percentage signs.""" |
| 56 | + result = AbstractManager.encode_path_segment("discount%off") |
| 57 | + assert result == "discount%25off" |
| 58 | + |
| 59 | + |
| 60 | +def test_encode_path_segment_with_plus_sign(): |
| 61 | + """Test encoding a string with plus signs.""" |
| 62 | + result = AbstractManager.encode_path_segment("one+two") |
| 63 | + assert result == "one%2Btwo" |
| 64 | + |
| 65 | + |
| 66 | +def test_encode_path_segment_with_unicode_characters(): |
| 67 | + """Test encoding a string with Unicode characters.""" |
| 68 | + result = AbstractManager.encode_path_segment("café") |
| 69 | + assert result == "caf%C3%A9" |
| 70 | + |
| 71 | + |
| 72 | +def test_encode_path_segment_with_emoji(): |
| 73 | + """Test encoding a string with emoji characters.""" |
| 74 | + result = AbstractManager.encode_path_segment("hello😊world") |
| 75 | + assert result == "hello%F0%9F%98%8Aworld" |
| 76 | + |
| 77 | + |
| 78 | +def test_encode_path_segment_with_empty_string(): |
| 79 | + """Test encoding an empty string.""" |
| 80 | + result = AbstractManager.encode_path_segment("") |
| 81 | + assert result == "" |
| 82 | + |
| 83 | + |
| 84 | +def test_encode_path_segment_with_only_special_characters(): |
| 85 | + """Test encoding a string with only special characters.""" |
| 86 | + result = AbstractManager.encode_path_segment("!@#$%^&*()") |
| 87 | + assert result == "%21%40%23%24%25%5E%26%2A%28%29" |
| 88 | + |
| 89 | + |
| 90 | +def test_encode_path_segment_with_numbers(): |
| 91 | + """Test encoding a string with numbers.""" |
| 92 | + result = AbstractManager.encode_path_segment("123456") |
| 93 | + assert result == "123456" |
| 94 | + |
| 95 | + |
| 96 | +def test_encode_path_segment_with_mixed_alphanumeric(): |
| 97 | + """Test encoding a string with mixed alphanumeric characters.""" |
| 98 | + result = AbstractManager.encode_path_segment("abc123XYZ") |
| 99 | + assert result == "abc123XYZ" |
| 100 | + |
| 101 | + |
| 102 | +def test_encode_path_segment_with_hyphen_and_underscore(): |
| 103 | + """Test encoding a string with hyphens and underscores (safe characters).""" |
| 104 | + result = AbstractManager.encode_path_segment("test-value_123") |
| 105 | + assert result == "test-value_123" |
| 106 | + |
| 107 | + |
| 108 | +def test_encode_path_segment_with_period_and_tilde(): |
| 109 | + """Test encoding a string with periods and tildes (safe characters).""" |
| 110 | + result = AbstractManager.encode_path_segment("file.txt~backup") |
| 111 | + assert result == "file.txt~backup" |
| 112 | + |
| 113 | + |
| 114 | +def test_encode_path_segment_with_integer_input(): |
| 115 | + """Test encoding an integer input (should be converted to string).""" |
| 116 | + result = AbstractManager.encode_path_segment(12345) |
| 117 | + assert result == "12345" |
| 118 | + |
| 119 | + |
| 120 | +def test_encode_path_segment_with_float_input(): |
| 121 | + """Test encoding a float input (should be converted to string).""" |
| 122 | + result = AbstractManager.encode_path_segment(123.45) |
| 123 | + assert result == "123.45" |
| 124 | + |
| 125 | + |
| 126 | +def test_encode_path_segment_with_none_input(): |
| 127 | + """Test encoding None input (should be converted to string).""" |
| 128 | + result = AbstractManager.encode_path_segment(None) |
| 129 | + assert result == "None" |
| 130 | + |
| 131 | + |
| 132 | +def test_encode_path_segment_preserves_unreserved_characters(): |
| 133 | + """Test that unreserved characters are not encoded.""" |
| 134 | + # RFC 3986 unreserved characters: ALPHA / DIGIT / "-" / "." / "_" / "~" |
| 135 | + unreserved = ( |
| 136 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~" |
| 137 | + ) |
| 138 | + result = AbstractManager.encode_path_segment(unreserved) |
| 139 | + assert result == unreserved |
| 140 | + |
| 141 | + |
| 142 | +def test_encode_path_segment_encodes_reserved_characters(): |
| 143 | + """Test that reserved characters are properly encoded.""" |
| 144 | + # Some RFC 3986 reserved characters |
| 145 | + reserved = ":/?#[]@!$&'()*+,;=" |
| 146 | + result = AbstractManager.encode_path_segment(reserved) |
| 147 | + # All characters should be encoded since safe="" is used |
| 148 | + assert ":" not in result |
| 149 | + assert "/" not in result |
| 150 | + assert "?" not in result |
| 151 | + assert "#" not in result |
| 152 | + assert "@" not in result |
| 153 | + assert "!" not in result |
| 154 | + assert "$" not in result |
| 155 | + assert "&" not in result |
| 156 | + assert "'" not in result |
| 157 | + assert "(" not in result |
| 158 | + assert ")" not in result |
| 159 | + assert "*" not in result |
| 160 | + assert "+" not in result |
| 161 | + assert "," not in result |
| 162 | + assert ";" not in result |
| 163 | + assert "=" not in result |
0 commit comments