OLD | NEW |
1 // Copyright 2007 Google Inc. All Rights Reserved. | 1 // Copyright 2007 Google Inc. All Rights Reserved. |
2 // Author: brettw@google.com (Brett Wilson) | 2 // Author: brettw@google.com (Brett Wilson) |
3 | 3 |
4 #include "googleurl/src/gurl.h" | 4 #include "googleurl/src/gurl.h" |
5 #include "googleurl/src/url_canon.h" | 5 #include "googleurl/src/url_canon.h" |
6 #include "googleurl/src/url_test_utils.h" | 6 #include "googleurl/src/url_test_utils.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 // Some implementations of base/basictypes.h may define ARRAYSIZE. | 9 // Some implementations of base/basictypes.h may define ARRAYSIZE. |
10 // If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro | 10 // If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro |
(...skipping 13 matching lines...) Expand all Loading... |
24 url_canon::Replacements<CHAR>* replacements, | 24 url_canon::Replacements<CHAR>* replacements, |
25 const CHAR* str) { | 25 const CHAR* str) { |
26 if (str) { | 26 if (str) { |
27 url_parse::Component comp; | 27 url_parse::Component comp; |
28 if (str[0]) | 28 if (str[0]) |
29 comp.len = static_cast<int>(strlen(str)); | 29 comp.len = static_cast<int>(strlen(str)); |
30 (replacements->*func)(str, comp); | 30 (replacements->*func)(str, comp); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
| 34 // Returns the canonicalized string for the given URL string for the |
| 35 // GURLTest.Types test. |
| 36 std::string TypesTestCase(const char* src) { |
| 37 GURL gurl(src); |
| 38 return gurl.possibly_invalid_spec(); |
| 39 } |
| 40 |
34 } // namespace | 41 } // namespace |
35 | 42 |
36 // Different types of URLs should be handled differently by url_util, and | 43 // Different types of URLs should be handled differently by url_util, and |
37 // handed off to different canonicalizers. | 44 // handed off to different canonicalizers. |
38 TEST(GURLTest, Types) { | 45 TEST(GURLTest, Types) { |
39 struct TypeTest { | 46 // URLs with unknown schemes should be treated as path URLs, even when they |
40 const char* src; | 47 // have things like "://". |
41 const char* expected; | 48 EXPECT_EQ("something:///HOSTNAME.com/", |
42 } type_cases[] = { | 49 TypesTestCase("something:///HOSTNAME.com/")); |
43 // URLs with "://" should be treated as standard and have a hostname, even | 50 |
44 // when the scheme is unknown. | 51 // In the reverse, known schemes should always trigger standard URL handling. |
45 {"something:///HOSTNAME.com/", "something://hostname.com/"}, | 52 EXPECT_EQ("http://hostname.com/", TypesTestCase("http:HOSTNAME.com")); |
46 // In the reverse, lacking a "://" means a path URL so no canonicalization | 53 EXPECT_EQ("http://hostname.com/", TypesTestCase("http:/HOSTNAME.com")); |
47 // should happen. | 54 EXPECT_EQ("http://hostname.com/", TypesTestCase("http://HOSTNAME.com")); |
48 {"something:HOSTNAME.com/", "something:HOSTNAME.com/"}, | 55 EXPECT_EQ("http://hostname.com/", TypesTestCase("http:///HOSTNAME.com")); |
49 {"something:/HOSTNAME.com/", "something:/HOSTNAME.com/"}, | 56 |
50 #ifdef WIN32 | 57 #ifdef WIN32 |
51 // URLs that look like absolute Windows drive specs. | 58 // URLs that look like absolute Windows drive specs. |
52 {"c:\\foo.txt", "file:///C:/foo.txt"}, | 59 EXPECT_EQ("file:///C:/foo.txt", TypesTestCase("c:\\foo.txt")); |
53 {"Z|foo.txt", "file:///Z:/foo.txt"}, | 60 EXPECT_EQ("file:///Z:/foo.txt", TypesTestCase("Z|foo.txt")); |
54 {"\\\\server\\foo.txt", "file://server/foo.txt"}, | 61 EXPECT_EQ("file://server/foo.txt", TypesTestCase("\\\\server\\foo.txt")); |
55 {"//server/foo.txt", "file://server/foo.txt"}, | 62 EXPECT_EQ("file://server/foo.txt", TypesTestCase("//server/foo.txt")); |
56 #endif | 63 #endif |
57 }; | |
58 | |
59 for (size_t i = 0; i < ARRAYSIZE(type_cases); i++) { | |
60 GURL gurl(type_cases[i].src); | |
61 EXPECT_STREQ(type_cases[i].expected, gurl.spec().c_str()); | |
62 } | |
63 } | 64 } |
64 | 65 |
65 // Test the basic creation and querying of components in a GURL. We assume | 66 // Test the basic creation and querying of components in a GURL. We assume |
66 // the parser is already tested and works, so we are mostly interested if the | 67 // the parser is already tested and works, so we are mostly interested if the |
67 // object does the right thing with the results. | 68 // object does the right thing with the results. |
68 TEST(GURLTest, Components) { | 69 TEST(GURLTest, Components) { |
69 GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref")); | 70 GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref")); |
70 EXPECT_TRUE(url.is_valid()); | 71 EXPECT_TRUE(url.is_valid()); |
71 EXPECT_TRUE(url.SchemeIs("http")); | 72 EXPECT_TRUE(url.SchemeIs("http")); |
72 EXPECT_FALSE(url.SchemeIsFile()); | 73 EXPECT_FALSE(url.SchemeIsFile()); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 const char* base; | 160 const char* base; |
160 const char* relative; | 161 const char* relative; |
161 bool expected_valid; | 162 bool expected_valid; |
162 const char* expected; | 163 const char* expected; |
163 } resolve_cases[] = { | 164 } resolve_cases[] = { |
164 {"http://www.google.com/", "foo.html", true, "http://www.google.com/foo.html
"}, | 165 {"http://www.google.com/", "foo.html", true, "http://www.google.com/foo.html
"}, |
165 {"http://www.google.com/", "http://images.google.com/foo.html", true, "http:
//images.google.com/foo.html"}, | 166 {"http://www.google.com/", "http://images.google.com/foo.html", true, "http:
//images.google.com/foo.html"}, |
166 {"http://www.google.com/blah/bloo?c#d", "../../../hello/./world.html?a#b", t
rue, "http://www.google.com/hello/world.html?a#b"}, | 167 {"http://www.google.com/blah/bloo?c#d", "../../../hello/./world.html?a#b", t
rue, "http://www.google.com/hello/world.html?a#b"}, |
167 {"http://www.google.com/foo#bar", "#com", true, "http://www.google.com/foo#c
om"}, | 168 {"http://www.google.com/foo#bar", "#com", true, "http://www.google.com/foo#c
om"}, |
168 {"http://www.google.com/", "Https:images.google.com", true, "https://images.
google.com/"}, | 169 {"http://www.google.com/", "Https:images.google.com", true, "https://images.
google.com/"}, |
169 // Unknown schemes with a "://" should be treated as standard. | 170 // Unknown schemes are not standard. |
170 {"somescheme://foo/", "bar", true, "somescheme://foo/bar"}, | |
171 // Unknown schemes with no "://" are not standard. | |
172 {"data:blahblah", "http://google.com/", true, "http://google.com/"}, | 171 {"data:blahblah", "http://google.com/", true, "http://google.com/"}, |
173 {"data:blahblah", "http:google.com", true, "http://google.com/"}, | 172 {"data:blahblah", "http:google.com", true, "http://google.com/"}, |
174 {"data:/blahblah", "file.html", false, ""}, | 173 {"data:/blahblah", "file.html", false, ""}, |
175 }; | 174 }; |
176 | 175 |
177 for (size_t i = 0; i < ARRAYSIZE(resolve_cases); i++) { | 176 for (size_t i = 0; i < ARRAYSIZE(resolve_cases); i++) { |
178 // 8-bit code path. | 177 // 8-bit code path. |
179 GURL input(resolve_cases[i].base); | 178 GURL input(resolve_cases[i].base); |
180 GURL output = input.Resolve(resolve_cases[i].relative); | 179 GURL output = input.Resolve(resolve_cases[i].relative); |
181 EXPECT_EQ(resolve_cases[i].expected_valid, output.is_valid()); | 180 EXPECT_EQ(resolve_cases[i].expected_valid, output.is_valid()) << i; |
182 EXPECT_EQ(resolve_cases[i].expected, output.spec()); | 181 EXPECT_EQ(resolve_cases[i].expected, output.spec()) << i; |
183 | 182 |
184 // Wide code path. | 183 // Wide code path. |
185 GURL inputw(ConvertUTF8ToUTF16(resolve_cases[i].base)); | 184 GURL inputw(ConvertUTF8ToUTF16(resolve_cases[i].base)); |
186 GURL outputw = | 185 GURL outputw = |
187 input.Resolve(ConvertUTF8ToUTF16(resolve_cases[i].relative)); | 186 input.Resolve(ConvertUTF8ToUTF16(resolve_cases[i].relative)); |
188 EXPECT_EQ(resolve_cases[i].expected_valid, outputw.is_valid()); | 187 EXPECT_EQ(resolve_cases[i].expected_valid, outputw.is_valid()) << i; |
189 EXPECT_EQ(resolve_cases[i].expected, outputw.spec()); | 188 EXPECT_EQ(resolve_cases[i].expected, outputw.spec()) << i; |
190 } | 189 } |
191 } | 190 } |
192 | 191 |
193 TEST(GURLTest, GetOrigin) { | 192 TEST(GURLTest, GetOrigin) { |
194 struct TestCase { | 193 struct TestCase { |
195 const char* input; | 194 const char* input; |
196 const char* expected; | 195 const char* expected; |
197 } cases[] = { | 196 } cases[] = { |
198 {"http://www.google.com", "http://www.google.com/"}, | 197 {"http://www.google.com", "http://www.google.com/"}, |
199 {"javascript:window.alert(\"hello,world\");", ""}, | 198 {"javascript:window.alert(\"hello,world\");", ""}, |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 } | 421 } |
423 | 422 |
424 TEST(GURLTest, IsStandard) { | 423 TEST(GURLTest, IsStandard) { |
425 GURL a("http:foo/bar"); | 424 GURL a("http:foo/bar"); |
426 EXPECT_TRUE(a.IsStandard()); | 425 EXPECT_TRUE(a.IsStandard()); |
427 | 426 |
428 GURL b("foo:bar/baz"); | 427 GURL b("foo:bar/baz"); |
429 EXPECT_FALSE(b.IsStandard()); | 428 EXPECT_FALSE(b.IsStandard()); |
430 | 429 |
431 GURL c("foo://bar/baz"); | 430 GURL c("foo://bar/baz"); |
432 EXPECT_TRUE(c.IsStandard()); | 431 EXPECT_FALSE(c.IsStandard()); |
433 } | 432 } |
OLD | NEW |