OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/url_util.h" | 5 #include "net/base/url_util.h" |
6 | 6 |
| 7 #include "base/format_macros.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/strings/stringprintf.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "url/gurl.h" | 11 #include "url/gurl.h" |
9 | 12 |
| 13 using base::ASCIIToUTF16; |
| 14 using base::WideToUTF16; |
| 15 |
10 namespace net { | 16 namespace net { |
11 namespace { | 17 namespace { |
12 | 18 |
13 TEST(UrlUtilTest, AppendQueryParameter) { | 19 TEST(UrlUtilTest, AppendQueryParameter) { |
14 // Appending a name-value pair to a URL without a query component. | 20 // Appending a name-value pair to a URL without a query component. |
15 EXPECT_EQ("http://example.com/path?name=value", | 21 EXPECT_EQ("http://example.com/path?name=value", |
16 AppendQueryParameter(GURL("http://example.com/path"), | 22 AppendQueryParameter(GURL("http://example.com/path"), |
17 "name", "value").spec()); | 23 "name", "value").spec()); |
18 | 24 |
19 // Appending a name-value pair to a URL with a query component. | 25 // Appending a name-value pair to a URL with a query component. |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 | 158 |
153 EXPECT_TRUE(it.IsAtEnd()); | 159 EXPECT_TRUE(it.IsAtEnd()); |
154 } | 160 } |
155 | 161 |
156 TEST(UrlUtilTest, ParseQueryInvalidURL) { | 162 TEST(UrlUtilTest, ParseQueryInvalidURL) { |
157 const GURL url("http://%01/?test"); | 163 const GURL url("http://%01/?test"); |
158 QueryIterator it(url); | 164 QueryIterator it(url); |
159 EXPECT_TRUE(it.IsAtEnd()); | 165 EXPECT_TRUE(it.IsAtEnd()); |
160 } | 166 } |
161 | 167 |
| 168 TEST(NetUtilTest, GetIdentityFromURL) { |
| 169 struct { |
| 170 const char* const input_url; |
| 171 const char* const expected_username; |
| 172 const char* const expected_password; |
| 173 } tests[] = { |
| 174 { |
| 175 "http://username:password@google.com", |
| 176 "username", |
| 177 "password", |
| 178 }, |
| 179 { // Test for http://crbug.com/19200 |
| 180 "http://username:p@ssword@google.com", |
| 181 "username", |
| 182 "p@ssword", |
| 183 }, |
| 184 { // Special URL characters should be unescaped. |
| 185 "http://username:p%3fa%26s%2fs%23@google.com", |
| 186 "username", |
| 187 "p?a&s/s#", |
| 188 }, |
| 189 { // Username contains %20. |
| 190 "http://use rname:password@google.com", |
| 191 "use rname", |
| 192 "password", |
| 193 }, |
| 194 { // Keep %00 as is. |
| 195 "http://use%00rname:password@google.com", |
| 196 "use%00rname", |
| 197 "password", |
| 198 }, |
| 199 { // Use a '+' in the username. |
| 200 "http://use+rname:password@google.com", |
| 201 "use+rname", |
| 202 "password", |
| 203 }, |
| 204 { // Use a '&' in the password. |
| 205 "http://username:p&ssword@google.com", |
| 206 "username", |
| 207 "p&ssword", |
| 208 }, |
| 209 }; |
| 210 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 211 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, |
| 212 tests[i].input_url)); |
| 213 GURL url(tests[i].input_url); |
| 214 |
| 215 base::string16 username, password; |
| 216 GetIdentityFromURL(url, &username, &password); |
| 217 |
| 218 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_username), username); |
| 219 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_password), password); |
| 220 } |
| 221 } |
| 222 |
| 223 // Try extracting a username which was encoded with UTF8. |
| 224 TEST(UrlUtilTest, GetIdentityFromURL_UTF8) { |
| 225 GURL url(WideToUTF16(L"http://foo:\x4f60\x597d@blah.com")); |
| 226 |
| 227 EXPECT_EQ("foo", url.username()); |
| 228 EXPECT_EQ("%E4%BD%A0%E5%A5%BD", url.password()); |
| 229 |
| 230 // Extract the unescaped identity. |
| 231 base::string16 username, password; |
| 232 GetIdentityFromURL(url, &username, &password); |
| 233 |
| 234 // Verify that it was decoded as UTF8. |
| 235 EXPECT_EQ(ASCIIToUTF16("foo"), username); |
| 236 EXPECT_EQ(WideToUTF16(L"\x4f60\x597d"), password); |
| 237 } |
| 238 |
162 } // namespace | 239 } // namespace |
163 } // namespace net | 240 } // namespace net |
OLD | NEW |