2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 // Basic tests that verify our KURL's interface behaves the same as the
36 #include <gtest/gtest.h>
41 // Output stream operator so gTest's macros work with WebCore strings.
42 std::ostream& operator<<(std::ostream& out, const String& str)
44 return str.isEmpty() ? out : out << str.utf8().data();
51 struct ComponentCase {
64 // Test the cases where we should be the same as WebKit's old KURL.
65 TEST(KURLTest, SameGetters)
74 const char* lastPathComponent;
79 {"http://www.google.com/foo/blah?bar=baz#ref", "http", "www.google.com", 0, "", 0, "blah", "bar=baz", "ref", true},
80 {"http://foo.com:1234/foo/bar/", "http", "foo.com", 1234, "", 0, "bar", 0, 0, false},
81 {"http://www.google.com?#", "http", "www.google.com", 0, "", 0, 0, "", "", true},
82 {"https://me:pass@google.com:23#foo", "https", "google.com", 23, "me", "pass", 0, 0, "foo", true},
83 {"javascript:hello!//world", "javascript", "", 0, "", 0, "world", 0, 0, false},
86 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
88 WebCore::KURL kurl(WebCore::ParsedURLString, cases[i].url);
90 EXPECT_EQ(cases[i].protocol, kurl.protocol());
91 EXPECT_EQ(cases[i].host, kurl.host());
92 EXPECT_EQ(cases[i].port, kurl.port());
93 EXPECT_EQ(cases[i].user, kurl.user());
94 EXPECT_EQ(cases[i].pass, kurl.pass());
95 EXPECT_EQ(cases[i].lastPathComponent, kurl.lastPathComponent());
96 EXPECT_EQ(cases[i].query, kurl.query());
97 EXPECT_EQ(cases[i].ref, kurl.fragmentIdentifier());
98 EXPECT_EQ(cases[i].hasRef, kurl.hasFragmentIdentifier());
101 WTF::String utf16(cases[i].url);
102 kurl = WebCore::KURL(WebCore::ParsedURLString, utf16);
104 EXPECT_EQ(cases[i].protocol, kurl.protocol());
105 EXPECT_EQ(cases[i].host, kurl.host());
106 EXPECT_EQ(cases[i].port, kurl.port());
107 EXPECT_EQ(cases[i].user, kurl.user());
108 EXPECT_EQ(cases[i].pass, kurl.pass());
109 EXPECT_EQ(cases[i].lastPathComponent, kurl.lastPathComponent());
110 EXPECT_EQ(cases[i].query, kurl.query());
111 EXPECT_EQ(cases[i].ref, kurl.fragmentIdentifier());
112 EXPECT_EQ(cases[i].hasRef, kurl.hasFragmentIdentifier());
116 // Test a few cases where we're different just to make sure we give reasonable
118 TEST(KURLTest, DifferentGetters)
120 ComponentCase cases[] = {
121 // url protocol host port user pass path lastPath query ref
123 // Old WebKit allows references and queries in what we call "path" URLs
124 // like javascript, so the path here will only consist of "hello!".
125 {"javascript:hello!?#/\\world", "javascript", "", 0, "", 0, "hello!?#/\\world", "world", 0, 0},
127 // Old WebKit doesn't handle "parameters" in paths, so will
128 // disagree with us about where the path is for this URL.
129 {"http://a.com/hello;world", "http", "a.com", 0, "", 0, "/hello;world", "hello", 0, 0},
131 // WebKit doesn't like UTF-8 or UTF-16 input.
132 {"http://\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd/", "http", "xn--6qqa088eba", 0, "", 0, "/", 0, 0, 0},
134 // WebKit %-escapes non-ASCII characters in reference, but we don't.
135 {"http://www.google.com/foo/blah?bar=baz#\xce\xb1\xce\xb2", "http", "www.google.com", 0, "", 0, "/foo/blah/", "blah", "bar=baz", "\xce\xb1\xce\xb2"},
138 for (size_t i = 0; i < arraysize(cases); i++) {
139 WebCore::KURL kurl(WebCore::ParsedURLString, cases[i].url);
141 EXPECT_EQ(cases[i].protocol, kurl.protocol());
142 EXPECT_EQ(cases[i].host, kurl.host());
143 EXPECT_EQ(cases[i].port, kurl.port());
144 EXPECT_EQ(cases[i].user, kurl.user());
145 EXPECT_EQ(cases[i].pass, kurl.pass());
146 EXPECT_EQ(cases[i].lastPath, kurl.lastPathComponent());
147 EXPECT_EQ(cases[i].query, kurl.query());
148 // Want to compare UCS-16 refs (or to null).
150 EXPECT_EQ(WTF::String::fromUTF8(cases[i].ref), kurl.fragmentIdentifier());
152 EXPECT_TRUE(kurl.fragmentIdentifier().isNull());
156 // Ensures that both ASCII and UTF-8 canonical URLs are handled properly and we
157 // get the correct string object out.
160 const char asciiURL[] = "http://foo/bar#baz";
161 WebCore::KURL asciiKURL(WebCore::ParsedURLString, asciiURL);
162 EXPECT_TRUE(asciiKURL.string() == WTF::String(asciiURL));
164 // When the result is ASCII, we should get an ASCII String. Some
165 // code depends on being able to compare the result of the .string()
166 // getter with another String, and the isASCIIness of the two
167 // strings must match for these functions (like equalIgnoringCase).
168 EXPECT_TRUE(WTF::equalIgnoringCase(asciiKURL, WTF::String(asciiURL)));
170 // Reproduce code path in FrameLoader.cpp -- equalIgnoringCase implicitly
171 // expects gkurl.protocol() to have been created as ascii.
172 WebCore::KURL mailto(WebCore::ParsedURLString, "mailto:foo@foo.com");
173 EXPECT_TRUE(WTF::equalIgnoringCase(mailto.protocol(), "mailto"));
175 const char utf8URL[] = "http://foo/bar#\xe4\xbd\xa0\xe5\xa5\xbd";
176 WebCore::KURL utf8KURL(WebCore::ParsedURLString, utf8URL);
178 EXPECT_TRUE(utf8KURL.string() == WTF::String::fromUTF8(utf8URL));
181 TEST(KURLTest, Setters)
183 // Replace the starting URL with the given components one at a time and
184 // verify that we're always the same as the old KURL.
186 // Note that old KURL won't canonicalize the default port away, so we
187 // can't set setting the http port to "80" (or even "0").
189 // We also can't test clearing the query.
191 // The format is every other row is a test, and the row that follows it is the
193 struct ExpectedComponentCase {
195 const char* protocol;
204 // The full expected URL with the given "set" applied.
205 const char* expectedProtocol;
206 const char* expectedHost;
207 const char* expectedPort;
208 const char* expectedUser;
209 const char* expectedPass;
210 const char* expectedPath;
211 const char* expectedQuery;
212 const char* expectedRef;
214 // url protocol host port user pass path query ref
215 {"http://www.google.com/", "https", "news.google.com", 8888, "me", "pass", "/foo", "?q=asdf", "heehee",
216 "https://www.google.com/",
217 "https://news.google.com/",
218 "https://news.google.com:8888/",
219 "https://me@news.google.com:8888/",
220 "https://me:pass@news.google.com:8888/",
221 "https://me:pass@news.google.com:8888/foo",
222 "https://me:pass@news.google.com:8888/foo?q=asdf",
223 "https://me:pass@news.google.com:8888/foo?q=asdf#heehee"},
225 {"https://me:pass@google.com:88/a?f#b", "http", "goo.com", 92, "", "", "/", 0, "",
226 "http://me:pass@google.com:88/a?f#b",
227 "http://me:pass@goo.com:88/a?f#b",
228 "http://me:pass@goo.com:92/a?f#b",
229 "http://:pass@goo.com:92/a?f#b",
230 "http://goo.com:92/a?f#b",
231 "http://goo.com:92/?f#b",
232 "http://goo.com:92/#b",
233 "https://goo.com:92/"},
236 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
237 WebCore::KURL kurl(WebCore::ParsedURLString, cases[i].url);
239 kurl.setProtocol(cases[i].protocol);
240 EXPECT_STREQ(cases[i].expectedProtocol, kurl.string().utf8().data());
242 kurl.setHost(cases[i].host);
243 EXPECT_STREQ(cases[i].expectedHost, kurl.string().utf8().data());
245 kurl.setPort(cases[i].port);
246 EXPECT_STREQ(cases[i].expectedPort, kurl.string().utf8().data());
248 kurl.setUser(cases[i].user);
249 EXPECT_STREQ(cases[i].expectedUser, kurl.string().utf8().data());
251 kurl.setPass(cases[i].pass);
252 EXPECT_STREQ(cases[i].expectedPass, kurl.string().utf8().data());
254 kurl.setPath(cases[i].path);
255 EXPECT_STREQ(cases[i].expectedPath, kurl.string().utf8().data());
257 kurl.setQuery(cases[i].query);
258 EXPECT_STREQ(cases[i].expectedQuery, kurl.string().utf8().data());
260 // Refs are tested below. On the Safari 3.1 branch, we don't match their
261 // KURL since we integrated a fix from their trunk.
265 // Tests that KURL::decodeURLEscapeSequences works as expected
267 TEST(KURLTest, Decode)
273 {"hello, world", "hello, world"},
274 {"%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/"},
275 {"%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/"},
276 {"%20%21%22%23%24%25%26%27%28%29%2a%2B%2C%2D%2e%2f/", " !\"#$%&'()*+,-.//"},
277 {"%30%31%32%33%34%35%36%37%38%39%3a%3B%3C%3D%3e%3f/", "0123456789:;<=>?/"},
278 {"%40%41%42%43%44%45%46%47%48%49%4a%4B%4C%4D%4e%4f/", "@ABCDEFGHIJKLMNO/"},
279 {"%50%51%52%53%54%55%56%57%58%59%5a%5B%5C%5D%5e%5f/", "PQRSTUVWXYZ[\\]^_/"},
280 {"%60%61%62%63%64%65%66%67%68%69%6a%6B%6C%6D%6e%6f/", "`abcdefghijklmno/"},
281 {"%70%71%72%73%74%75%76%77%78%79%7a%7B%7C%7D%7e%7f/", "pqrstuvwxyz{|}~\x7f/"},
282 // Test un-UTF-8-ization.
283 {"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd"},
286 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(decodeCases); i++) {
287 WTF::String input(decodeCases[i].input);
288 WTF::String str = WebCore::decodeURLEscapeSequences(input);
289 EXPECT_STREQ(decodeCases[i].output, str.utf8().data());
292 // Our decode should decode %00
293 WTF::String zero = WebCore::decodeURLEscapeSequences("%00");
294 EXPECT_STRNE("%00", zero.utf8().data());
296 // Test the error behavior for invalid UTF-8 (we differ from WebKit here).
297 WTF::String invalid = WebCore::decodeURLEscapeSequences(
299 char16 invalidExpectedHelper[4] = { 0x00e4, 0x00a0, 0x597d, 0 };
300 WTF::String invalidExpected(
301 reinterpret_cast<const ::UChar*>(invalidExpectedHelper),
303 EXPECT_EQ(invalidExpected, invalid);
307 TEST(KURLTest, Encode)
313 {"hello, world", "hello%2C%20world"},
314 {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
315 "%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"},
316 {"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
317 "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"},
318 {" !\"#$%&'()*+,-./",
319 "%20!%22%23%24%25%26'()*%2B%2C-.%2F"},
321 "0123456789%3A%3B%3C%3D%3E%3F"},
323 "%40ABCDEFGHIJKLMNO"},
324 {"PQRSTUVWXYZ[\\]^_",
325 "PQRSTUVWXYZ%5B%5C%5D%5E_"},
327 "%60abcdefghijklmno"},
328 {"pqrstuvwxyz{|}~\x7f",
329 "pqrstuvwxyz%7B%7C%7D~%7F"},
332 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(encode_cases); i++) {
333 WTF::String input(encode_cases[i].input);
334 WTF::String expectedOutput(encode_cases[i].output);
335 WTF::String output = WebCore::encodeWithURLEscapeSequences(input);
336 EXPECT_EQ(expectedOutput, output);
339 // Our encode escapes NULLs for safety, so we need to check that too.
340 WTF::String input("\x00\x01", 2);
341 WTF::String reference("%00%01");
343 WTF::String output = WebCore::encodeWithURLEscapeSequences(input);
344 EXPECT_EQ(reference, output);
346 // Also test that it gets converted to UTF-8 properly.
347 char16 wideInputHelper[3] = { 0x4f60, 0x597d, 0 };
348 WTF::String wideInput(
349 reinterpret_cast<const ::UChar*>(wideInputHelper), 2);
350 WTF::String wideReference("%E4%BD%A0%E5%A5%BD");
351 WTF::String wideOutput =
352 WebCore::encodeWithURLEscapeSequences(wideInput);
353 EXPECT_EQ(wideReference, wideOutput);
356 TEST(KURLTest, ResolveEmpty)
358 WebCore::KURL emptyBase;
360 // WebKit likes to be able to resolve absolute input agains empty base URLs,
361 // which would normally be invalid since the base URL is invalid.
362 const char abs[] = "http://www.google.com/";
363 WebCore::KURL resolveAbs(emptyBase, abs);
364 EXPECT_TRUE(resolveAbs.isValid());
365 EXPECT_STREQ(abs, resolveAbs.string().utf8().data());
367 // Resolving a non-relative URL agains the empty one should still error.
368 const char rel[] = "foo.html";
369 WebCore::KURL resolveErr(emptyBase, rel);
370 EXPECT_FALSE(resolveErr.isValid());
373 // WebKit will make empty URLs and set components on them. kurl doesn't allow
374 // replacements on invalid URLs, but here we do.
375 TEST(KURLTest, ReplaceInvalid)
379 EXPECT_FALSE(kurl.isValid());
380 EXPECT_TRUE(kurl.isEmpty());
381 EXPECT_STREQ("", kurl.string().utf8().data());
383 kurl.setProtocol("http");
384 // GKURL will say that a URL with just a scheme is invalid, KURL will not.
386 EXPECT_FALSE(kurl.isValid());
388 EXPECT_TRUE(kurl.isValid());
390 EXPECT_FALSE(kurl.isEmpty());
391 // At this point, we do things slightly differently if there is only a scheme.
392 // We check the results here to make it more obvious what is going on, but it
393 // shouldn't be a big deal if these change.
395 EXPECT_STREQ("http:", kurl.string().utf8().data());
397 EXPECT_STREQ("http:/", kurl.string().utf8().data());
400 kurl.setHost("www.google.com");
401 EXPECT_TRUE(kurl.isValid());
402 EXPECT_FALSE(kurl.isEmpty());
403 EXPECT_STREQ("http://www.google.com/", kurl.string().utf8().data());
406 EXPECT_TRUE(kurl.isValid());
407 EXPECT_FALSE(kurl.isEmpty());
408 EXPECT_STREQ("http://www.google.com:8000/", kurl.string().utf8().data());
410 kurl.setPath("/favicon.ico");
411 EXPECT_TRUE(kurl.isValid());
412 EXPECT_FALSE(kurl.isEmpty());
413 EXPECT_STREQ("http://www.google.com:8000/favicon.ico", kurl.string().utf8().data());
415 // Now let's test that giving an invalid replacement fails. Invalid
416 // protocols fail without modifying the URL, which should remain valid.
418 EXPECT_FALSE(kurl.setProtocol("f/sj#@"));
419 EXPECT_TRUE(kurl.isValid());
425 const char initial[] = "http://www.google.com/path/foo";
426 WebCore::KURL kurl(WebCore::ParsedURLString, initial);
428 // Clear by setting a null string.
429 WTF::String nullString;
430 EXPECT_TRUE(nullString.isNull());
431 kurl.setPath(nullString);
432 EXPECT_STREQ("http://www.google.com/", kurl.string().utf8().data());
435 // Test that setting the query to different things works. Thq query is handled
436 // a littler differently than some of the other components.
437 TEST(KURLTest, Query)
439 const char initial[] = "http://www.google.com/search?q=awesome";
440 WebCore::KURL kurl(WebCore::ParsedURLString, initial);
442 // Clear by setting a null string.
443 WTF::String nullString;
444 EXPECT_TRUE(nullString.isNull());
445 kurl.setQuery(nullString);
446 EXPECT_STREQ("http://www.google.com/search", kurl.string().utf8().data());
448 // Clear by setting an empty string.
449 kurl = WebCore::KURL(WebCore::ParsedURLString, initial);
450 WTF::String emptyString("");
451 EXPECT_FALSE(emptyString.isNull());
452 kurl.setQuery(emptyString);
453 EXPECT_STREQ("http://www.google.com/search?", kurl.string().utf8().data());
455 // Set with something that begins in a question mark.
456 const char question[] = "?foo=bar";
457 kurl.setQuery(question);
458 EXPECT_STREQ("http://www.google.com/search?foo=bar",
459 kurl.string().utf8().data());
461 // Set with something that doesn't begin in a question mark.
462 const char query[] = "foo=bar";
463 kurl.setQuery(query);
464 EXPECT_STREQ("http://www.google.com/search?foo=bar",
465 kurl.string().utf8().data());
470 WebCore::KURL kurl(WebCore::ParsedURLString, "http://foo/bar#baz");
472 // Basic ref setting.
473 WebCore::KURL cur(WebCore::ParsedURLString, "http://foo/bar");
474 cur.setFragmentIdentifier("asdf");
475 EXPECT_STREQ("http://foo/bar#asdf", cur.string().utf8().data());
477 cur.setFragmentIdentifier("asdf");
478 EXPECT_STREQ("http://foo/bar#asdf", cur.string().utf8().data());
480 // Setting a ref to the empty string will set it to "#".
481 cur = WebCore::KURL(WebCore::ParsedURLString, "http://foo/bar");
482 cur.setFragmentIdentifier("");
483 EXPECT_STREQ("http://foo/bar#", cur.string().utf8().data());
485 cur.setFragmentIdentifier("");
486 EXPECT_STREQ("http://foo/bar#", cur.string().utf8().data());
488 // Setting the ref to the null string will clear it altogether.
489 cur = WebCore::KURL(WebCore::ParsedURLString, "http://foo/bar");
490 cur.setFragmentIdentifier(WTF::String());
491 EXPECT_STREQ("http://foo/bar", cur.string().utf8().data());
493 cur.setFragmentIdentifier(WTF::String());
494 EXPECT_STREQ("http://foo/bar", cur.string().utf8().data());
497 TEST(KURLTest, Empty)
501 // First test that regular empty URLs are the same.
502 EXPECT_TRUE(kurl.isEmpty());
503 EXPECT_FALSE(kurl.isValid());
504 EXPECT_TRUE(kurl.isNull());
505 EXPECT_TRUE(kurl.string().isNull());
506 EXPECT_TRUE(kurl.string().isEmpty());
508 // Test resolving a null URL on an empty string.
509 WebCore::KURL kurl2(kurl, "");
510 EXPECT_FALSE(kurl2.isNull());
511 EXPECT_TRUE(kurl2.isEmpty());
512 EXPECT_FALSE(kurl2.isValid());
513 EXPECT_FALSE(kurl2.string().isNull());
514 EXPECT_TRUE(kurl2.string().isEmpty());
515 EXPECT_FALSE(kurl2.string().isNull());
516 EXPECT_TRUE(kurl2.string().isEmpty());
518 // Resolve the null URL on a null string.
519 WebCore::KURL kurl22(kurl, WTF::String());
520 EXPECT_FALSE(kurl22.isNull());
521 EXPECT_TRUE(kurl22.isEmpty());
522 EXPECT_FALSE(kurl22.isValid());
523 EXPECT_FALSE(kurl22.string().isNull());
524 EXPECT_TRUE(kurl22.string().isEmpty());
525 EXPECT_FALSE(kurl22.string().isNull());
526 EXPECT_TRUE(kurl22.string().isEmpty());
528 // Test non-hierarchical schemes resolving. The actual URLs will be different.
529 // WebKit's one will set the string to "something.gif" and we'll set it to an
530 // empty string. I think either is OK, so we just check our behavior.
532 WebCore::KURL kurl3(WebCore::KURL(WebCore::ParsedURLString, "data:foo"),
534 EXPECT_TRUE(kurl3.isEmpty());
535 EXPECT_FALSE(kurl3.isValid());
538 // Test for weird isNull string input,
539 // see: http://bugs.webkit.org/show_bug.cgi?id=16487
540 WebCore::KURL kurl4(WebCore::ParsedURLString, kurl.string());
541 EXPECT_TRUE(kurl4.isEmpty());
542 EXPECT_FALSE(kurl4.isValid());
543 EXPECT_TRUE(kurl4.string().isNull());
544 EXPECT_TRUE(kurl4.string().isEmpty());
546 // Resolving an empty URL on an invalid string.
547 WebCore::KURL kurl5(WebCore::KURL(), "foo.js");
548 // We'll be empty in this case, but KURL won't be. Should be OK.
549 // EXPECT_EQ(kurl5.isEmpty(), kurl5.isEmpty());
550 // EXPECT_EQ(kurl5.string().isEmpty(), kurl5.string().isEmpty());
551 EXPECT_FALSE(kurl5.isValid());
552 EXPECT_FALSE(kurl5.string().isNull());
554 // Empty string as input
555 WebCore::KURL kurl6(WebCore::ParsedURLString, "");
556 EXPECT_TRUE(kurl6.isEmpty());
557 EXPECT_FALSE(kurl6.isValid());
558 EXPECT_FALSE(kurl6.string().isNull());
559 EXPECT_TRUE(kurl6.string().isEmpty());
561 // Non-empty but invalid C string as input.
562 WebCore::KURL kurl7(WebCore::ParsedURLString, "foo.js");
563 // WebKit will actually say this URL has the string "foo.js" but is invalid.
565 // EXPECT_EQ(kurl7.isEmpty(), kurl7.isEmpty());
566 EXPECT_FALSE(kurl7.isValid());
567 EXPECT_FALSE(kurl7.string().isNull());
570 TEST(KURLTest, UserPass)
572 const char* src = "http://user:pass@google.com/";
573 WebCore::KURL kurl(WebCore::ParsedURLString, src);
575 // Clear just the username.
577 EXPECT_EQ("http://:pass@google.com/", kurl.string());
579 // Clear just the password.
580 kurl = WebCore::KURL(WebCore::ParsedURLString, src);
582 EXPECT_EQ("http://user@google.com/", kurl.string());
586 EXPECT_EQ("http://google.com/", kurl.string());
589 TEST(KURLTest, Offsets)
591 const char* src1 = "http://user:pass@google.com/foo/bar.html?baz=query#ref";
592 WebCore::KURL kurl1(WebCore::ParsedURLString, src1);
594 EXPECT_EQ(17u, kurl1.hostStart());
595 EXPECT_EQ(27u, kurl1.hostEnd());
596 EXPECT_EQ(27u, kurl1.pathStart());
597 EXPECT_EQ(40u, kurl1.pathEnd());
598 EXPECT_EQ(32u, kurl1.pathAfterLastSlash());
600 const char* src2 = "http://google.com/foo/";
601 WebCore::KURL kurl2(WebCore::ParsedURLString, src2);
603 EXPECT_EQ(7u, kurl2.hostStart());
604 EXPECT_EQ(17u, kurl2.hostEnd());
605 EXPECT_EQ(17u, kurl2.pathStart());
606 EXPECT_EQ(22u, kurl2.pathEnd());
607 EXPECT_EQ(22u, kurl2.pathAfterLastSlash());
609 const char* src3 = "javascript:foobar";
610 WebCore::KURL kurl3(WebCore::ParsedURLString, src3);
612 EXPECT_EQ(11u, kurl3.hostStart());
613 EXPECT_EQ(11u, kurl3.hostEnd());
614 EXPECT_EQ(11u, kurl3.pathStart());
615 EXPECT_EQ(17u, kurl3.pathEnd());
616 EXPECT_EQ(11u, kurl3.pathAfterLastSlash());
619 TEST(KURLTest, DeepCopy)
621 const char url[] = "http://www.google.com/";
622 WebCore::KURL src(WebCore::ParsedURLString, url);
623 EXPECT_TRUE(src.string() == url); // This really just initializes the cache.
624 WebCore::KURL dest = src.copy();
625 EXPECT_TRUE(dest.string() == url); // This really just initializes the cache.
627 // The pointers should be different for both UTF-8 and UTF-16.
628 EXPECT_NE(dest.string().characters(), src.string().characters());
629 EXPECT_NE(dest.utf8String().data(), src.utf8String().data());
632 TEST(KURLTest, ProtocolIs)
634 WebCore::KURL url1(WebCore::ParsedURLString, "foo://bar");
635 EXPECT_TRUE(url1.protocolIs("foo"));
636 EXPECT_FALSE(url1.protocolIs("foo-bar"));
638 WebCore::KURL url2(WebCore::ParsedURLString, "foo-bar:");
639 EXPECT_TRUE(url2.protocolIs("foo-bar"));
640 EXPECT_FALSE(url2.protocolIs("foo"));