OLD | NEW |
| (Empty) |
1 // Copyright (c) 2008, Google Inc. | |
2 // All rights reserved. | |
3 // | |
4 // Redistribution and use in source and binary forms, with or without | |
5 // modification, are permitted provided that the following conditions are | |
6 // met: | |
7 // | |
8 // * Redistributions of source code must retain the above copyright | |
9 // notice, this list of conditions and the following disclaimer. | |
10 // * Redistributions in binary form must reproduce the above | |
11 // copyright notice, this list of conditions and the following disclaimer | |
12 // in the documentation and/or other materials provided with the | |
13 // distribution. | |
14 // * Neither the name of Google Inc. nor the names of its | |
15 // contributors may be used to endorse or promote products derived from | |
16 // this software without specific prior written permission. | |
17 // | |
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | |
30 // Basic tests that verify our KURL's interface behaves the same as the | |
31 // original KURL's. | |
32 | |
33 #include "config.h" | |
34 | |
35 #include "base/compiler_specific.h" | |
36 #include "testing/gtest/include/gtest/gtest.h" | |
37 #include "webkit/glue/glue_util.h" | |
38 | |
39 #include "KURL.h" | |
40 | |
41 #undef LOG | |
42 #include "base/basictypes.h" | |
43 #include "base/string16.h" | |
44 #include "base/string_util.h" | |
45 | |
46 namespace { | |
47 | |
48 struct ComponentCase { | |
49 const char* url; | |
50 const char* protocol; | |
51 const char* host; | |
52 const int port; | |
53 const char* user; | |
54 const char* pass; | |
55 const char* path; | |
56 const char* last_path; | |
57 const char* query; | |
58 const char* ref; | |
59 }; | |
60 | |
61 // Output stream operator so gTest's macros work with WebCore strings. | |
62 std::ostream& operator<<(std::ostream& out, | |
63 const WebCore::String& str) { | |
64 if (str.isEmpty()) | |
65 return out; | |
66 return out << WideToUTF8(std::wstring( | |
67 reinterpret_cast<const wchar_t*>(str.characters()), str.length())); | |
68 } | |
69 | |
70 } // namespace | |
71 | |
72 // Test the cases where we should be the same as WebKit's old KURL. | |
73 TEST(GKURL, SameGetters) { | |
74 struct GetterCase { | |
75 const char* url; | |
76 const char* protocol; | |
77 const char* host; | |
78 int port; | |
79 const char* user; | |
80 const char* pass; | |
81 const char* last_path_component; | |
82 const char* query; | |
83 const char* ref; | |
84 bool has_ref; | |
85 } cases[] = { | |
86 {"http://www.google.com/foo/blah?bar=baz#ref", "http", "www.google.com", 0,
"", NULL, "blah", "?bar=baz", "ref", true}, | |
87 {"http://foo.com:1234/foo/bar/", "http", "foo.com", 1234, "", NULL, "bar", "
", NULL, false}, | |
88 {"http://www.google.com?#", "http", "www.google.com", 0, "", NULL, NULL, "?"
, "", true}, | |
89 {"https://me:pass@google.com:23#foo", "https", "google.com", 23, "me", "pass
", NULL, "", "foo", true}, | |
90 {"javascript:hello!//world", "javascript", "", 0, "", NULL, "world", "", NUL
L, false}, | |
91 }; | |
92 | |
93 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { | |
94 // UTF-8 | |
95 WebCore::KURL gurl(cases[i].url); | |
96 | |
97 EXPECT_EQ(cases[i].protocol, gurl.protocol()); | |
98 EXPECT_EQ(cases[i].host, gurl.host()); | |
99 EXPECT_EQ(cases[i].port, gurl.port()); | |
100 EXPECT_EQ(cases[i].user, gurl.user()); | |
101 EXPECT_EQ(cases[i].pass, gurl.pass()); | |
102 EXPECT_EQ(cases[i].last_path_component, gurl.lastPathComponent()); | |
103 EXPECT_EQ(cases[i].query, gurl.query()); | |
104 EXPECT_EQ(cases[i].ref, gurl.ref()); | |
105 EXPECT_EQ(cases[i].has_ref, gurl.hasRef()); | |
106 | |
107 // UTF-16 | |
108 string16 wstr(UTF8ToUTF16(cases[i].url)); | |
109 WebCore::String utf16( | |
110 reinterpret_cast<const ::UChar*>(wstr.c_str()), | |
111 static_cast<int>(wstr.length())); | |
112 gurl = WebCore::KURL(utf16); | |
113 | |
114 EXPECT_EQ(cases[i].protocol, gurl.protocol()); | |
115 EXPECT_EQ(cases[i].host, gurl.host()); | |
116 EXPECT_EQ(cases[i].port, gurl.port()); | |
117 EXPECT_EQ(cases[i].user, gurl.user()); | |
118 EXPECT_EQ(cases[i].pass, gurl.pass()); | |
119 EXPECT_EQ(cases[i].last_path_component, gurl.lastPathComponent()); | |
120 EXPECT_EQ(cases[i].query, gurl.query()); | |
121 EXPECT_EQ(cases[i].ref, gurl.ref()); | |
122 EXPECT_EQ(cases[i].has_ref, gurl.hasRef()); | |
123 } | |
124 } | |
125 | |
126 // Test a few cases where we're different just to make sure we give reasonable | |
127 // output. | |
128 TEST(GKURL, DifferentGetters) { | |
129 ComponentCase cases[] = { | |
130 // url protocol host port us
er pass path last_path query ref | |
131 | |
132 // Old WebKit allows references and queries in what we call "path" URLs | |
133 // like javascript, so the path here will only consist of "hello!". | |
134 {"javascript:hello!?#/\\world", "javascript", "", 0, ""
, NULL, "hello!?#/\\world", "world", "", NULL}, | |
135 | |
136 // Old WebKit doesn't handle "parameters" in paths, so will | |
137 // disagree with us about where the path is for this URL. | |
138 {"http://a.com/hello;world", "http", "a.com", 0, ""
, NULL, "/hello;world", "hello", "", NULL}, | |
139 | |
140 // WebKit doesn't like UTF-8 or UTF-16 input. | |
141 {"http://\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd/", "http", "xn--6q
qa088eba", 0, "", NULL, "/", NULL, "", NULL}, | |
142 | |
143 // WebKit %-escapes non-ASCII characters in reference, but we don't. | |
144 {"http://www.google.com/foo/blah?bar=baz#\xce\xb1\xce\xb2", "http", "www.goo
gle.com", 0, "", NULL, "/foo/blah/", "blah", "?bar=baz", "\xce\xb1\xce\xb2"} | |
145 }; | |
146 | |
147 for (size_t i = 0; i < arraysize(cases); i++) { | |
148 WebCore::KURL gurl(cases[i].url); | |
149 | |
150 EXPECT_EQ(cases[i].protocol, gurl.protocol()); | |
151 EXPECT_EQ(cases[i].host, gurl.host()); | |
152 EXPECT_EQ(cases[i].port, gurl.port()); | |
153 EXPECT_EQ(cases[i].user, gurl.user()); | |
154 EXPECT_EQ(cases[i].pass, gurl.pass()); | |
155 EXPECT_EQ(cases[i].last_path, gurl.lastPathComponent()); | |
156 EXPECT_EQ(cases[i].query, gurl.query()); | |
157 // Want to compare UCS-16 refs (or to NULL). | |
158 if (cases[i].ref) { | |
159 EXPECT_EQ(webkit_glue::StdWStringToString(UTF8ToWide(cases[i].ref)), | |
160 gurl.ref()); | |
161 } else { | |
162 EXPECT_TRUE(gurl.ref().isNull()); | |
163 } | |
164 } | |
165 } | |
166 | |
167 // Ensures that both ASCII and UTF-8 canonical URLs are handled properly and we | |
168 // get the correct string object out. | |
169 TEST(GKURL, UTF8) { | |
170 const char ascii_url[] = "http://foo/bar#baz"; | |
171 WebCore::KURL ascii_gurl(ascii_url); | |
172 EXPECT_TRUE(ascii_gurl.string() == WebCore::String(ascii_url)); | |
173 | |
174 // When the result is ASCII, we should get an ASCII String. Some | |
175 // code depends on being able to compare the result of the .string() | |
176 // getter with another String, and the isASCIIness of the two | |
177 // strings must match for these functions (like equalIgnoringCase). | |
178 EXPECT_TRUE(WebCore::equalIgnoringCase(ascii_gurl, | |
179 WebCore::String(ascii_url))); | |
180 | |
181 // Reproduce code path in FrameLoader.cpp -- equalIgnoringCase implicitly | |
182 // expects gkurl.protocol() to have been created as ascii. | |
183 WebCore::KURL mailto("mailto:foo@foo.com"); | |
184 EXPECT_TRUE(WebCore::equalIgnoringCase(mailto.protocol(), "mailto")); | |
185 | |
186 const char utf8_url[] = "http://foo/bar#\xe4\xbd\xa0\xe5\xa5\xbd"; | |
187 WebCore::KURL utf8_gurl(utf8_url); | |
188 | |
189 EXPECT_TRUE(utf8_gurl.string() == | |
190 webkit_glue::StdWStringToString(UTF8ToWide(utf8_url))); | |
191 } | |
192 | |
193 TEST(GKURL, Setters) { | |
194 // Replace the starting URL with the given components one at a time and | |
195 // verify that we're always the same as the old KURL. | |
196 // | |
197 // Note that old KURL won't canonicalize the default port away, so we | |
198 // can't set setting the http port to "80" (or even "0"). | |
199 // | |
200 // We also can't test clearing the query. | |
201 // | |
202 // The format is every other row is a test, and the row that follows it is the | |
203 // expected result. | |
204 struct ExpectedComponentCase { | |
205 const char* url; | |
206 const char* protocol; | |
207 const char* host; | |
208 const int port; | |
209 const char* user; | |
210 const char* pass; | |
211 const char* path; | |
212 const char* query; | |
213 const char* ref; | |
214 | |
215 // The full expected URL with the given "set" applied. | |
216 const char* expected_protocol; | |
217 const char* expected_host; | |
218 const char* expected_port; | |
219 const char* expected_user; | |
220 const char* expected_pass; | |
221 const char* expected_path; | |
222 const char* expected_query; | |
223 const char* expected_ref; | |
224 } cases[] = { | |
225 // url protocol host p
ort user pass path query ref | |
226 {"http://www.google.com/", "https", "news.google.com", 8
888, "me", "pass", "/foo", "?q=asdf", "heehee", | |
227 "https://www.google.com/", | |
228 "https://news.google
.com/", | |
229 "
https://news.google.com:8888/", | |
230
"https://me@news.google.com:8888/", | |
231
"https://me:pass@news.google.com:8888/", | |
232
"https://me:pass@news.google.com:8888/foo", | |
233
"https://me:pass@news.google.com:8888/foo?q=a
sdf", | |
234
"https://me:pass@news.google.com:8
888/foo?q=asdf#heehee"}, | |
235 | |
236 {"https://me:pass@google.com:88/a?f#b", "http", "goo.com", 9
2, "", "", "/", NULL, "", | |
237 "http://me:pass@google.com:88/a?f#
b", | |
238 "http://me:pass@goo.
com:88/a?f#b", | |
239 "
http://me:pass@goo.com:92/a?f#b", | |
240
"http://:pass@goo.com:92/a?f#b", | |
241
"http://goo.com:92/a?f#b", | |
242
"http://goo.com:92/?f#b", | |
243
"http://goo.com:92/#b", | |
244
"https://goo.com:92/"}, | |
245 }; | |
246 | |
247 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { | |
248 WebCore::KURL gurl(cases[i].url); | |
249 | |
250 gurl.setProtocol(cases[i].protocol); | |
251 EXPECT_STREQ(cases[i].expected_protocol, gurl.string().utf8().data()); | |
252 | |
253 gurl.setHost(cases[i].host); | |
254 EXPECT_STREQ(cases[i].expected_host, gurl.string().utf8().data()); | |
255 | |
256 gurl.setPort(cases[i].port); | |
257 EXPECT_STREQ(cases[i].expected_port, gurl.string().utf8().data()); | |
258 | |
259 gurl.setUser(cases[i].user); | |
260 EXPECT_STREQ(cases[i].expected_user, gurl.string().utf8().data()); | |
261 | |
262 gurl.setPass(cases[i].pass); | |
263 EXPECT_STREQ(cases[i].expected_pass, gurl.string().utf8().data()); | |
264 | |
265 gurl.setPath(cases[i].path); | |
266 EXPECT_STREQ(cases[i].expected_path, gurl.string().utf8().data()); | |
267 | |
268 gurl.setQuery(cases[i].query); | |
269 EXPECT_STREQ(cases[i].expected_query, gurl.string().utf8().data()); | |
270 | |
271 // Refs are tested below. On the Safari 3.1 branch, we don't match their | |
272 // KURL since we integrated a fix from their trunk. | |
273 } | |
274 } | |
275 | |
276 // Tests that KURL::decodeURLEscapeSequences works as expected | |
277 #if USE(GOOGLEURL) | |
278 TEST(GKURL, Decode) { | |
279 struct DecodeCase { | |
280 const char* input; | |
281 const char* output; | |
282 } decode_cases[] = { | |
283 {"hello, world", "hello, world"}, | |
284 {"%01%02%03%04%05%06%07%08%09%0a%0B%0C%0D%0e%0f/", "\x01\x02\x03\x04\x05\x06
\x07\x08\x09\x0a\x0B\x0C\x0D\x0e\x0f/"}, | |
285 {"%10%11%12%13%14%15%16%17%18%19%1a%1B%1C%1D%1e%1f/", "\x10\x11\x12\x13\x14\
x15\x16\x17\x18\x19\x1a\x1B\x1C\x1D\x1e\x1f/"}, | |
286 {"%20%21%22%23%24%25%26%27%28%29%2a%2B%2C%2D%2e%2f/", " !\"#$%&'()*+,-.//"}, | |
287 {"%30%31%32%33%34%35%36%37%38%39%3a%3B%3C%3D%3e%3f/", "0123456789:;<=>?/"}, | |
288 {"%40%41%42%43%44%45%46%47%48%49%4a%4B%4C%4D%4e%4f/", "@ABCDEFGHIJKLMNO/"}, | |
289 {"%50%51%52%53%54%55%56%57%58%59%5a%5B%5C%5D%5e%5f/", "PQRSTUVWXYZ[\\]^_/"}, | |
290 {"%60%61%62%63%64%65%66%67%68%69%6a%6B%6C%6D%6e%6f/", "`abcdefghijklmno/"}, | |
291 {"%70%71%72%73%74%75%76%77%78%79%7a%7B%7C%7D%7e%7f/", "pqrstuvwxyz{|}~\x7f/"
}, | |
292 // Test un-UTF-8-ization. | |
293 {"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd"}, | |
294 }; | |
295 | |
296 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(decode_cases); i++) { | |
297 WebCore::String input(decode_cases[i].input); | |
298 WebCore::String str = WebCore::decodeURLEscapeSequences(input); | |
299 EXPECT_STREQ(decode_cases[i].output, str.utf8().data()); | |
300 } | |
301 | |
302 // Our decode should not decode %00 | |
303 WebCore::String zero = WebCore::decodeURLEscapeSequences("%00"); | |
304 EXPECT_STREQ("%00", zero.utf8().data()); | |
305 | |
306 // Test the error behavior for invalid UTF-8 (we differ from WebKit here). | |
307 WebCore::String invalid = WebCore::decodeURLEscapeSequences( | |
308 "%e4%a0%e5%a5%bd"); | |
309 char16 invalid_expected_helper[4] = { 0x00e4, 0x00a0, 0x597d, 0 }; | |
310 WebCore::String invalid_expected( | |
311 reinterpret_cast<const ::UChar*>(invalid_expected_helper), | |
312 3); | |
313 EXPECT_EQ(invalid_expected, invalid); | |
314 } | |
315 #endif | |
316 | |
317 TEST(GKURL, Encode) { | |
318 // Also test that it gets converted to UTF-8 properly. | |
319 char16 wide_input_helper[3] = { 0x4f60, 0x597d, 0 }; | |
320 WebCore::String wide_input( | |
321 reinterpret_cast<const ::UChar*>(wide_input_helper), 2); | |
322 WebCore::String wide_reference("\xe4\xbd\xa0\xe5\xa5\xbd", 6); | |
323 WebCore::String wide_output = | |
324 WebCore::encodeWithURLEscapeSequences(wide_input); | |
325 EXPECT_EQ(wide_reference, wide_output); | |
326 | |
327 // Our encode only escapes NULLs for safety (see the implementation for | |
328 // more), so we only bother to test a few cases. | |
329 WebCore::String input( | |
330 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16); | |
331 WebCore::String reference( | |
332 "%00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 18); | |
333 WebCore::String output = WebCore::encodeWithURLEscapeSequences(input); | |
334 EXPECT_EQ(reference, output); | |
335 } | |
336 | |
337 TEST(GKURL, ResolveEmpty) { | |
338 WebCore::KURL empty_base; | |
339 | |
340 // WebKit likes to be able to resolve absolute input agains empty base URLs, | |
341 // which would normally be invalid since the base URL is invalid. | |
342 const char abs[] = "http://www.google.com/"; | |
343 WebCore::KURL resolve_abs(empty_base, abs); | |
344 EXPECT_TRUE(resolve_abs.isValid()); | |
345 EXPECT_STREQ(abs, resolve_abs.string().utf8().data()); | |
346 | |
347 // Resolving a non-relative URL agains the empty one should still error. | |
348 const char rel[] = "foo.html"; | |
349 WebCore::KURL resolve_err(empty_base, rel); | |
350 EXPECT_FALSE(resolve_err.isValid()); | |
351 } | |
352 | |
353 // WebKit will make empty URLs and set components on them. GURL doesn't allow | |
354 // replacements on invalid URLs, but here we do. | |
355 TEST(GKURL, ReplaceInvalid) { | |
356 WebCore::KURL gurl; | |
357 | |
358 EXPECT_FALSE(gurl.isValid()); | |
359 EXPECT_TRUE(gurl.isEmpty()); | |
360 EXPECT_STREQ("", gurl.string().utf8().data()); | |
361 | |
362 gurl.setProtocol("http"); | |
363 // GKURL will say that a URL with just a scheme is invalid, KURL will not. | |
364 #if USE(GOOGLEURL) | |
365 EXPECT_FALSE(gurl.isValid()); | |
366 #else | |
367 EXPECT_TRUE(gurl.isValid()); | |
368 #endif | |
369 EXPECT_FALSE(gurl.isEmpty()); | |
370 // At this point, we do things slightly differently if there is only a scheme. | |
371 // We check the results here to make it more obvious what is going on, but it | |
372 // shouldn't be a big deal if these change. | |
373 #if USE(GOOGLEURL) | |
374 EXPECT_STREQ("http:", gurl.string().utf8().data()); | |
375 #else | |
376 EXPECT_STREQ("http:/", gurl.string().utf8().data()); | |
377 #endif | |
378 | |
379 gurl.setHost("www.google.com"); | |
380 EXPECT_TRUE(gurl.isValid()); | |
381 EXPECT_FALSE(gurl.isEmpty()); | |
382 EXPECT_STREQ("http://www.google.com/", gurl.string().utf8().data()); | |
383 | |
384 gurl.setPort(8000); | |
385 EXPECT_TRUE(gurl.isValid()); | |
386 EXPECT_FALSE(gurl.isEmpty()); | |
387 EXPECT_STREQ("http://www.google.com:8000/", gurl.string().utf8().data()); | |
388 | |
389 gurl.setPath("/favicon.ico"); | |
390 EXPECT_TRUE(gurl.isValid()); | |
391 EXPECT_FALSE(gurl.isEmpty()); | |
392 EXPECT_STREQ("http://www.google.com:8000/favicon.ico", gurl.string().utf8().da
ta()); | |
393 | |
394 // Now let's test that giving an invalid replacement still fails. | |
395 #if USE(GOOGLEURL) | |
396 gurl.setProtocol("f/sj#@"); | |
397 EXPECT_FALSE(gurl.isValid()); | |
398 #endif | |
399 } | |
400 | |
401 TEST(GKURL, Path) { | |
402 const char initial[] = "http://www.google.com/path/foo"; | |
403 WebCore::KURL gurl(initial); | |
404 | |
405 // Clear by setting a NULL string. | |
406 WebCore::String null_string; | |
407 EXPECT_TRUE(null_string.isNull()); | |
408 gurl.setPath(null_string); | |
409 EXPECT_STREQ("http://www.google.com/", gurl.string().utf8().data()); | |
410 } | |
411 | |
412 // Test that setting the query to different things works. Thq query is handled | |
413 // a littler differently than some of the other components. | |
414 TEST(GKURL, Query) { | |
415 const char initial[] = "http://www.google.com/search?q=awesome"; | |
416 WebCore::KURL gurl(initial); | |
417 | |
418 // Clear by setting a NULL string. | |
419 WebCore::String null_string; | |
420 EXPECT_TRUE(null_string.isNull()); | |
421 gurl.setQuery(null_string); | |
422 EXPECT_STREQ("http://www.google.com/search", gurl.string().utf8().data()); | |
423 | |
424 // Clear by setting an empty string. | |
425 gurl = WebCore::KURL(initial); | |
426 WebCore::String empty_string(""); | |
427 EXPECT_FALSE(empty_string.isNull()); | |
428 gurl.setQuery(empty_string); | |
429 EXPECT_STREQ("http://www.google.com/search?", gurl.string().utf8().data()); | |
430 | |
431 // Set with something that begins in a question mark. | |
432 const char question[] = "?foo=bar"; | |
433 gurl.setQuery(question); | |
434 EXPECT_STREQ("http://www.google.com/search?foo=bar", | |
435 gurl.string().utf8().data()); | |
436 | |
437 // Set with something that doesn't begin in a question mark. | |
438 const char query[] = "foo=bar"; | |
439 gurl.setQuery(query); | |
440 EXPECT_STREQ("http://www.google.com/search?foo=bar", | |
441 gurl.string().utf8().data()); | |
442 } | |
443 | |
444 TEST(GKURL, Ref) { | |
445 WebCore::KURL gurl("http://foo/bar#baz"); | |
446 | |
447 // Basic ref setting. | |
448 WebCore::KURL cur("http://foo/bar"); | |
449 cur.setRef("asdf"); | |
450 EXPECT_STREQ("http://foo/bar#asdf", cur.string().utf8().data()); | |
451 cur = gurl; | |
452 cur.setRef("asdf"); | |
453 EXPECT_STREQ("http://foo/bar#asdf", cur.string().utf8().data()); | |
454 | |
455 // Setting a ref to the empty string will set it to "#". | |
456 cur = WebCore::KURL("http://foo/bar"); | |
457 cur.setRef(""); | |
458 EXPECT_STREQ("http://foo/bar#", cur.string().utf8().data()); | |
459 cur = gurl; | |
460 cur.setRef(""); | |
461 EXPECT_STREQ("http://foo/bar#", cur.string().utf8().data()); | |
462 | |
463 // Setting the ref to the null string will clear it altogether. | |
464 cur = WebCore::KURL("http://foo/bar"); | |
465 cur.setRef(WebCore::String()); | |
466 EXPECT_STREQ("http://foo/bar", cur.string().utf8().data()); | |
467 cur = gurl; | |
468 cur.setRef(WebCore::String()); | |
469 EXPECT_STREQ("http://foo/bar", cur.string().utf8().data()); | |
470 } | |
471 | |
472 TEST(GKURL, Empty) { | |
473 WebCore::KURL gurl; | |
474 | |
475 // First test that regular empty URLs are the same. | |
476 EXPECT_TRUE(gurl.isEmpty()); | |
477 EXPECT_FALSE(gurl.isValid()); | |
478 EXPECT_TRUE(gurl.isNull()); | |
479 EXPECT_TRUE(gurl.string().isNull()); | |
480 EXPECT_TRUE(gurl.string().isEmpty()); | |
481 | |
482 // Test resolving a NULL URL on an empty string. | |
483 WebCore::KURL gurl2(gurl, ""); | |
484 EXPECT_FALSE(gurl2.isNull()); | |
485 EXPECT_TRUE(gurl2.isEmpty()); | |
486 EXPECT_FALSE(gurl2.isValid()); | |
487 EXPECT_FALSE(gurl2.string().isNull()); | |
488 EXPECT_TRUE(gurl2.string().isEmpty()); | |
489 EXPECT_FALSE(gurl2.string().isNull()); | |
490 EXPECT_TRUE(gurl2.string().isEmpty()); | |
491 | |
492 // Resolve the NULL URL on a NULL string. | |
493 WebCore::KURL gurl22(gurl, WebCore::String()); | |
494 EXPECT_FALSE(gurl2.isNull()); | |
495 EXPECT_TRUE(gurl2.isEmpty()); | |
496 EXPECT_FALSE(gurl2.isValid()); | |
497 EXPECT_FALSE(gurl2.string().isNull()); | |
498 EXPECT_TRUE(gurl2.string().isEmpty()); | |
499 EXPECT_FALSE(gurl2.string().isNull()); | |
500 EXPECT_TRUE(gurl2.string().isEmpty()); | |
501 | |
502 // Test non-hierarchical schemes resolving. The actual URLs will be different. | |
503 // WebKit's one will set the string to "something.gif" and we'll set it to an | |
504 // empty string. I think either is OK, so we just check our behavior. | |
505 #if USE(GOOGLEURL) | |
506 WebCore::KURL gurl3(WebCore::KURL("data:foo"), "something.gif"); | |
507 EXPECT_TRUE(gurl3.isEmpty()); | |
508 EXPECT_FALSE(gurl3.isValid()); | |
509 #endif | |
510 | |
511 // Test for weird isNull string input, | |
512 // see: http://bugs.webkit.org/show_bug.cgi?id=16487 | |
513 WebCore::KURL gurl4(gurl.string()); | |
514 EXPECT_TRUE(gurl4.isEmpty()); | |
515 EXPECT_FALSE(gurl4.isValid()); | |
516 EXPECT_TRUE(gurl4.string().isNull()); | |
517 EXPECT_TRUE(gurl4.string().isEmpty()); | |
518 | |
519 // Resolving an empty URL on an invalid string. | |
520 WebCore::KURL gurl5(WebCore::KURL(), "foo.js"); | |
521 // We'll be empty in this case, but KURL won't be. Should be OK. | |
522 // EXPECT_EQ(kurl5.isEmpty(), gurl5.isEmpty()); | |
523 // EXPECT_EQ(kurl5.string().isEmpty(), gurl5.string().isEmpty()); | |
524 EXPECT_FALSE(gurl5.isValid()); | |
525 EXPECT_FALSE(gurl5.string().isNull()); | |
526 | |
527 // Empty string as input | |
528 WebCore::KURL gurl6(""); | |
529 EXPECT_TRUE(gurl6.isEmpty()); | |
530 EXPECT_FALSE(gurl6.isValid()); | |
531 EXPECT_FALSE(gurl6.string().isNull()); | |
532 EXPECT_TRUE(gurl6.string().isEmpty()); | |
533 | |
534 // Non-empty but invalid C string as input. | |
535 WebCore::KURL gurl7("foo.js"); | |
536 // WebKit will actually say this URL has the string "foo.js" but is invalid. | |
537 // We don't do that. | |
538 // EXPECT_EQ(kurl7.isEmpty(), gurl7.isEmpty()); | |
539 EXPECT_FALSE(gurl7.isValid()); | |
540 EXPECT_FALSE(gurl7.string().isNull()); | |
541 } | |
542 | |
543 TEST(GKURL, UserPass) { | |
544 const char* src = "http://user:pass@google.com/"; | |
545 WebCore::KURL gurl(src); | |
546 | |
547 // Clear just the username. | |
548 gurl.setUser(""); | |
549 EXPECT_EQ("http://:pass@google.com/", gurl.string()); | |
550 | |
551 // Clear just the password. | |
552 gurl = WebCore::KURL(src); | |
553 gurl.setPass(""); | |
554 EXPECT_EQ("http://user@google.com/", gurl.string()); | |
555 | |
556 // Now clear both. | |
557 gurl.setUser(""); | |
558 EXPECT_EQ("http://google.com/", gurl.string()); | |
559 } | |
560 | |
561 TEST(GKURL, Offsets) { | |
562 const char* src1 = "http://user:pass@google.com/foo/bar.html?baz=query#ref"; | |
563 WebCore::KURL gurl1(src1); | |
564 | |
565 EXPECT_EQ(17u, gurl1.hostStart()); | |
566 EXPECT_EQ(27u, gurl1.hostEnd()); | |
567 EXPECT_EQ(27u, gurl1.pathStart()); | |
568 EXPECT_EQ(40u, gurl1.pathEnd()); | |
569 EXPECT_EQ(32u, gurl1.pathAfterLastSlash()); | |
570 | |
571 const char* src2 = "http://google.com/foo/"; | |
572 WebCore::KURL gurl2(src2); | |
573 | |
574 EXPECT_EQ(7u, gurl2.hostStart()); | |
575 EXPECT_EQ(17u, gurl2.hostEnd()); | |
576 EXPECT_EQ(17u, gurl2.pathStart()); | |
577 EXPECT_EQ(22u, gurl2.pathEnd()); | |
578 EXPECT_EQ(22u, gurl2.pathAfterLastSlash()); | |
579 | |
580 const char* src3 = "javascript:foobar"; | |
581 WebCore::KURL gurl3(src3); | |
582 | |
583 EXPECT_EQ(11u, gurl3.hostStart()); | |
584 EXPECT_EQ(11u, gurl3.hostEnd()); | |
585 EXPECT_EQ(11u, gurl3.pathStart()); | |
586 EXPECT_EQ(17u, gurl3.pathEnd()); | |
587 EXPECT_EQ(11u, gurl3.pathAfterLastSlash()); | |
588 } | |
589 | |
590 TEST(GKURL, DeepCopy) { | |
591 const char url[] = "http://www.google.com/"; | |
592 WebCore::KURL src(url); | |
593 EXPECT_TRUE(src.string() == url); // This really just initializes the cache. | |
594 WebCore::KURL dest = src.copy(); | |
595 EXPECT_TRUE(dest.string() == url); // This really just initializes the cache. | |
596 | |
597 // The pointers should be different for both UTF-8 and UTF-16. | |
598 EXPECT_NE(dest.string().characters(), src.string().characters()); | |
599 EXPECT_NE(dest.utf8String().data(), src.utf8String().data()); | |
600 } | |
OLD | NEW |