Debian Codegraph @

LspCpp/third_party/uri/test/uri_normalization_test.cpp

raw
1 // Copyright (c) Glyn Matthews 2012-2016.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <gtest/gtest.h>
7 #include <network/uri.hpp>
8
9 // Compare the underlying strings because ``normalize`` is used in the
10 // ``uri`` equality operator.
11
12 TEST(uri_normalization_test, string_comparison) {
13 network::uri instance("http://www.example.com/");
14 ASSERT_EQ(
15 "http://www.example.com/",
16 instance.normalize(network::uri_comparison_level::string_comparison).string());
17 }
18
19 TEST(uri_normalization_test, string_comparison_with_case) {
20 network::uri instance("HTTP://www.example.com/");
21 ASSERT_EQ(
22 "HTTP://www.example.com/",
23 instance.normalize(network::uri_comparison_level::string_comparison).string());
24 }
25
26 TEST(uri_normalization_test, normalize_case_capitalized_scheme) {
27 network::uri instance("HTTP://www.example.com/");
28 ASSERT_EQ("http://www.example.com/",
29 instance.normalize(network::uri_comparison_level::syntax_based).string());
30 }
31
32 TEST(uri_normalization_test, DISABLED_normalize_case_capitalized_host) {
33 network::uri instance("http://WWW.EXAMPLE.COM/");
34 ASSERT_EQ("http://www.example.com/",
35 instance.normalize(network::uri_comparison_level::syntax_based).string());
36 }
37
38 TEST(uri_normalization_test, normalize_percent_encoding) {
39 network::uri instance(
40 "http://www%2Eexample%2Ecom/%7E%66%6F%6F%62%61%72%5F%36%39/");
41 ASSERT_EQ("http://www.example.com/~foobar_69/",
42 instance.normalize(network::uri_comparison_level::syntax_based).string());
43 }
44
45 TEST(uri_normalization_test,
46 normalize_percent_encoding_with_lower_case_elements) {
47 network::uri instance(
48 "http://www%2eexample%2ecom/%7e%66%6f%6f%62%61%72%5f%36%39/");
49 ASSERT_EQ("http://www.example.com/~foobar_69/",
50 instance.normalize(network::uri_comparison_level::syntax_based).string());
51 }
52
53 TEST(uri_normalization_test, normalize_percent_encoding_is_upper_case) {
54 network::uri instance(
55 "HTTP://www%2Eexample%2Ecom/%7E%66%6F%6F%62%61%72%5F%36%39/");
56 ASSERT_EQ("http://www.example.com/~foobar_69/",
57 instance.normalize(network::uri_comparison_level::syntax_based).string());
58 }
59
60 TEST(uri_normalization_test, path_segmented_add_trailing_slash) {
61 network::uri instance("http://www.example.com");
62 ASSERT_EQ("http://www.example.com/",
63 instance.normalize(network::uri_comparison_level::syntax_based).string());
64 }
65
66 TEST(uri_normalization_test,
67 path_segmented_dont_add_trailing_slash_if_path_is_not_empty) {
68 network::uri instance("http://www.example.com/path");
69 ASSERT_EQ("http://www.example.com/path",
70 instance.normalize(network::uri_comparison_level::syntax_based).string());
71 }
72
73 TEST(uri_normalization_test, path_segmented_remove_dot_segments) {
74 network::uri instance("http://www.example.com/a/./b/");
75 ASSERT_EQ("http://www.example.com/a/b/",
76 instance.normalize(network::uri_comparison_level::syntax_based).string());
77 }
78
79 TEST(uri_normalization_test, path_segmented_remove_double_dot_segments) {
80 network::uri instance("http://www.example.com/a/../b/");
81 ASSERT_EQ("http://www.example.com/b/",
82 instance.normalize(network::uri_comparison_level::syntax_based).string());
83 }
84
85 TEST(uri_normalization_test,
86 path_segmented_remove_dot_segments_with_percent_encoded_dot) {
87 network::uri instance("http://www.example.com/a/%2E/b/");
88 ASSERT_EQ("http://www.example.com/a/b/",
89 instance.normalize(network::uri_comparison_level::syntax_based).string());
90 }
91
92 TEST(uri_normalization_test,
93 path_segmented_remove_double_dot_segments_with_percent_encoded_dots) {
94 network::uri instance("http://www.example.com/a/%2E%2E/b/");
95 ASSERT_EQ("http://www.example.com/b/",
96 instance.normalize(network::uri_comparison_level::syntax_based).string());
97 }
98
99 TEST(uri_normalization_test,
100 path_segmented_remove_double_dot_segments_with_query) {
101 network::uri instance("http://www.example.com/a/../b/?key=value");
102 ASSERT_EQ("http://www.example.com/b/?key=value",
103 instance.normalize(network::uri_comparison_level::syntax_based).string());
104 }
105
106 TEST(uri_normalization_test,
107 path_segmented_remove_double_dot_segments_with_empty_query) {
108 network::uri instance("http://www.example.com/a/../b/?");
109 ASSERT_EQ("http://www.example.com/b/?",
110 instance.normalize(network::uri_comparison_level::syntax_based).string());
111 }
112
113 TEST(uri_normalization_test,
114 path_segmented_remove_double_dot_segments_with_fragment) {
115 network::uri instance("http://www.example.com/a/../b/#fragment");
116 ASSERT_EQ("http://www.example.com/b/#fragment",
117 instance.normalize(network::uri_comparison_level::syntax_based).string());
118 }
119
120 TEST(uri_normalization_test,
121 path_segmented_remove_double_dot_segments_with_empty_fragment) {
122 network::uri instance("http://www.example.com/a/../b/#");
123 ASSERT_EQ("http://www.example.com/b/#",
124 instance.normalize(network::uri_comparison_level::syntax_based).string());
125 }
126
127 TEST(uri_normalization_test,
128 path_segmented_remove_double_dot_segments_with_query_and_fragment) {
129 network::uri instance("http://www.example.com/a/../b/?key=value#fragment");
130 ASSERT_EQ("http://www.example.com/b/?key=value#fragment",
131 instance.normalize(network::uri_comparison_level::syntax_based).string());
132 }
133
134 TEST(uri_normalization_test, path_double_dash) {
135 network::uri instance("http://www.example.com//");
136 ASSERT_EQ("http://www.example.com/",
137 instance.normalize(network::uri_comparison_level::syntax_based).string());
138 }
139
140 TEST(uri_normalization_test, path_triple_dash) {
141 network::uri instance("http://www.example.com///");
142 ASSERT_EQ("http://www.example.com/",
143 instance.normalize(network::uri_comparison_level::syntax_based).string());
144 }
145
146 TEST(uri_normalization_test, path_depth_below_root) {
147 network::uri instance("http://www.example.com/..");
148 ASSERT_THROW(instance.normalize(network::uri_comparison_level::syntax_based),
149 std::system_error);
150 }
151
152 TEST(uri_normalization_test, path_depth_below_root_2) {
153 network::uri instance("http://www.example.com/a/../..");
154 ASSERT_THROW(instance.normalize(network::uri_comparison_level::syntax_based),
155 std::system_error);
156 }
157
158 TEST(uri_normalization_test, path_dash_dot_dash) {
159 network::uri instance("http://www.example.com/./");
160 ASSERT_EQ("http://www.example.com/",
161 instance.normalize(network::uri_comparison_level::syntax_based).string());
162 }
163
164 TEST(uri_normalization_test, path_dash_dot_dash_dot) {
165 network::uri instance("http://www.example.com/./.");
166 ASSERT_EQ("http://www.example.com/",
167 instance.normalize(network::uri_comparison_level::syntax_based).string());
168 }
169
170 TEST(uri_normalization_test, path_percent_encoded_reserved) {
171 // :/?#[]@!$&'()*+,;=
172 network::uri instance(
173 "http://www.example.com/%3a%2f%3f%23%5b%5d%40%21%24%26%27%28%29%2a%2b%2c%3b%3d");
174 ASSERT_EQ(
175 "http://www.example.com/%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D",
176 instance.normalize(network::uri_comparison_level::syntax_based).string());
177 }
178
179 TEST(uri_normalization_test, path_percent_encoded_unreserved) {
180 network::uri instance("http://www.example.com/alpha123%2d%2e%5f%7e");
181 ASSERT_EQ("http://www.example.com/alpha123-._~",
182 instance.normalize(network::uri_comparison_level::syntax_based).string());
183 }
184
185 TEST(uri_normalization_test, query_percent_encoded_reserved) {
186 // :/?#[]@!$&'()*+,;=
187 network::uri instance(
188 "http://www.example.com?foo=%3a%2f%3f%23%5b%5d%40%21%24%26%27%28%29%2a%2b%2c%3b%3d");
189 ASSERT_EQ(
190 "http://www.example.com/?foo=%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D",
191 instance.normalize(network::uri_comparison_level::syntax_based).string());
192 }
193
194 TEST(uri_normalization_test, query_percent_encoded_unreserved) {
195 network::uri instance("http://www.example.com?foo=alpha123%2d%2e%5f%7e");
196 ASSERT_EQ("http://www.example.com/?foo=alpha123-._~",
197 instance.normalize(network::uri_comparison_level::syntax_based).string());
198 }
199
200 TEST(uri_test, path_adjacent_slashes_122) {
201 // https://github.com/cpp-netlib/uri/issues/122
202 network::uri instance("http://www.example.com/abc//elementary");
203 ASSERT_EQ("http://www.example.com/abc/elementary",
204 instance.normalize(network::uri_comparison_level::syntax_based).string());
205 }
206
207 TEST(uri_test, path_adjacent_slashes_122_part_2) {
208 // https://github.com/cpp-netlib/uri/issues/122
209 network::uri instance("http://www.example.com/abc//.//../elementary");
210 ASSERT_EQ("http://www.example.com/elementary",
211 instance.normalize(network::uri_comparison_level::syntax_based).string());
212 }