Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(475)

Side by Side Diff: Source/weborigin/KURLTest.cpp

Issue 54053006: Move weborigin/ under platform/ so that it may someday call platform APIs (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Stale refernence to weboriginexport in .gpyi Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/weborigin/KURLHash.h ('k') | Source/weborigin/KnownPorts.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2010 Google Inc. 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
31 // Basic tests that verify our KURL's interface behaves the same as the
32 // original KURL's.
33
34 #include "config.h"
35 #include "weborigin/KURL.h"
36
37 #include "wtf/testing/WTFTestHelpers.h"
38 #include <gtest/gtest.h>
39
40 namespace {
41
42 struct ComponentCase {
43 const char* url;
44 const char* protocol;
45 const char* host;
46 const int port;
47 const char* user;
48 const char* pass;
49 const char* path;
50 const char* lastPath;
51 const char* query;
52 const char* ref;
53 };
54
55 // Test the cases where we should be the same as WebKit's old KURL.
56 TEST(KURLTest, SameGetters)
57 {
58 struct GetterCase {
59 const char* url;
60 const char* protocol;
61 const char* host;
62 int port;
63 const char* user;
64 const char* pass;
65 const char* lastPathComponent;
66 const char* query;
67 const char* ref;
68 bool hasRef;
69 } cases[] = {
70 {"http://www.google.com/foo/blah?bar=baz#ref", "http", "www.google.com", 0, "", 0, "blah", "bar=baz", "ref", true},
71 {"http://foo.com:1234/foo/bar/", "http", "foo.com", 1234, "", 0, "bar", 0, 0, false},
72 {"http://www.google.com?#", "http", "www.google.com", 0, "", 0, 0, "", " ", true},
73 {"https://me:pass@google.com:23#foo", "https", "google.com", 23, "me", " pass", 0, 0, "foo", true},
74 {"javascript:hello!//world", "javascript", "", 0, "", 0, "world", 0, 0, false},
75 };
76
77 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
78 // UTF-8
79 WebCore::KURL kurl(WebCore::ParsedURLString, cases[i].url);
80
81 EXPECT_EQ(cases[i].protocol, kurl.protocol());
82 EXPECT_EQ(cases[i].host, kurl.host());
83 EXPECT_EQ(cases[i].port, kurl.port());
84 EXPECT_EQ(cases[i].user, kurl.user());
85 EXPECT_EQ(cases[i].pass, kurl.pass());
86 EXPECT_EQ(cases[i].lastPathComponent, kurl.lastPathComponent());
87 EXPECT_EQ(cases[i].query, kurl.query());
88 EXPECT_EQ(cases[i].ref, kurl.fragmentIdentifier());
89 EXPECT_EQ(cases[i].hasRef, kurl.hasFragmentIdentifier());
90
91 // UTF-16
92 WTF::String utf16(cases[i].url);
93 kurl = WebCore::KURL(WebCore::ParsedURLString, utf16);
94
95 EXPECT_EQ(cases[i].protocol, kurl.protocol());
96 EXPECT_EQ(cases[i].host, kurl.host());
97 EXPECT_EQ(cases[i].port, kurl.port());
98 EXPECT_EQ(cases[i].user, kurl.user());
99 EXPECT_EQ(cases[i].pass, kurl.pass());
100 EXPECT_EQ(cases[i].lastPathComponent, kurl.lastPathComponent());
101 EXPECT_EQ(cases[i].query, kurl.query());
102 EXPECT_EQ(cases[i].ref, kurl.fragmentIdentifier());
103 EXPECT_EQ(cases[i].hasRef, kurl.hasFragmentIdentifier());
104 }
105 }
106
107 // Test a few cases where we're different just to make sure we give reasonable
108 // output.
109 TEST(KURLTest, DISABLED_DifferentGetters)
110 {
111 ComponentCase cases[] = {
112 // url protocol host port user pass path lastPath query ref
113
114 // Old WebKit allows references and queries in what we call "path" URLs
115 // like javascript, so the path here will only consist of "hello!".
116 {"javascript:hello!?#/\\world", "javascript", "", 0, "", 0, "hello!?#/\\world", "world", 0, 0},
117
118 // Old WebKit doesn't handle "parameters" in paths, so will
119 // disagree with us about where the path is for this URL.
120 {"http://a.com/hello;world", "http", "a.com", 0, "", 0, "/hello;world", "hello", 0, 0},
121
122 // WebKit doesn't like UTF-8 or UTF-16 input.
123 {"http://\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd/", "http", "xn --6qqa088eba", 0, "", 0, "/", 0, 0, 0},
124
125 // WebKit %-escapes non-ASCII characters in reference, but we don't.
126 {"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"},
127 };
128
129 for (size_t i = 0; i < arraysize(cases); i++) {
130 WebCore::KURL kurl(WebCore::ParsedURLString, cases[i].url);
131
132 EXPECT_EQ(cases[i].protocol, kurl.protocol());
133 EXPECT_EQ(cases[i].host, kurl.host());
134 EXPECT_EQ(cases[i].port, kurl.port());
135 EXPECT_EQ(cases[i].user, kurl.user());
136 EXPECT_EQ(cases[i].pass, kurl.pass());
137 EXPECT_EQ(cases[i].lastPath, kurl.lastPathComponent());
138 EXPECT_EQ(cases[i].query, kurl.query());
139 // Want to compare UCS-16 refs (or to null).
140 if (cases[i].ref)
141 EXPECT_EQ(WTF::String::fromUTF8(cases[i].ref), kurl.fragmentIdentifi er());
142 else
143 EXPECT_TRUE(kurl.fragmentIdentifier().isNull());
144 }
145 }
146
147 // Ensures that both ASCII and UTF-8 canonical URLs are handled properly and we
148 // get the correct string object out.
149 TEST(KURLTest, DISABLED_UTF8)
150 {
151 const char asciiURL[] = "http://foo/bar#baz";
152 WebCore::KURL asciiKURL(WebCore::ParsedURLString, asciiURL);
153 EXPECT_TRUE(asciiKURL.string() == WTF::String(asciiURL));
154
155 // When the result is ASCII, we should get an ASCII String. Some
156 // code depends on being able to compare the result of the .string()
157 // getter with another String, and the isASCIIness of the two
158 // strings must match for these functions (like equalIgnoringCase).
159 EXPECT_TRUE(WTF::equalIgnoringCase(asciiKURL, WTF::String(asciiURL)));
160
161 // Reproduce code path in FrameLoader.cpp -- equalIgnoringCase implicitly
162 // expects gkurl.protocol() to have been created as ascii.
163 WebCore::KURL mailto(WebCore::ParsedURLString, "mailto:foo@foo.com");
164 EXPECT_TRUE(WTF::equalIgnoringCase(mailto.protocol(), "mailto"));
165
166 const char utf8URL[] = "http://foo/bar#\xe4\xbd\xa0\xe5\xa5\xbd";
167 WebCore::KURL utf8KURL(WebCore::ParsedURLString, utf8URL);
168
169 EXPECT_TRUE(utf8KURL.string() == WTF::String::fromUTF8(utf8URL));
170 }
171
172 TEST(KURLTest, Setters)
173 {
174 // Replace the starting URL with the given components one at a time and
175 // verify that we're always the same as the old KURL.
176 //
177 // Note that old KURL won't canonicalize the default port away, so we
178 // can't set setting the http port to "80" (or even "0").
179 //
180 // We also can't test clearing the query.
181 //
182 // The format is every other row is a test, and the row that follows it is t he
183 // expected result.
184 struct ExpectedComponentCase {
185 const char* url;
186 const char* protocol;
187 const char* host;
188 const int port;
189 const char* user;
190 const char* pass;
191 const char* path;
192 const char* query;
193 const char* ref;
194
195 // The full expected URL with the given "set" applied.
196 const char* expectedProtocol;
197 const char* expectedHost;
198 const char* expectedPort;
199 const char* expectedUser;
200 const char* expectedPass;
201 const char* expectedPath;
202 const char* expectedQuery;
203 const char* expectedRef;
204 } cases[] = {
205 // url protocol host port user pass path query ref
206 {"http://www.google.com/", "https", "news.google.com ", 8888, "me", "pass", "/foo", "?q=asdf", "heehee",
207 "https://www.google.com/",
208 "https://news.go ogle.com/",
209 "https://news.google.com:8888/",
210 "https://me@news.google.com:8888/",
211 "https://me:pass@news.google.com:8888/",
212 "https://me:pass@news.google.com:8888/foo",
213 "https://me:pass@news.google.com:8888/foo ?q=asdf",
214 "https://me:pass@news.google.c om:8888/foo?q=asdf#heehee"},
215
216 {"https://me:pass@google.com:88/a?f#b", "http", "goo.com", 92, "", "", "/", 0, "",
217 "http://me:pass@google.com:88/ a?f#b",
218 "http://me:pass@ goo.com:88/a?f#b",
219 "http://me:pass@goo.com:92/a?f#b",
220 "http://:pass@goo.com:92/a?f#b",
221 "http://goo.com:92/a?f#b",
222 "http://goo.com:92/?f#b",
223 "http://goo.com:92/#b",
224 "https://goo.com:92/"},
225 };
226
227 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
228 WebCore::KURL kurl(WebCore::ParsedURLString, cases[i].url);
229
230 kurl.setProtocol(cases[i].protocol);
231 EXPECT_STREQ(cases[i].expectedProtocol, kurl.string().utf8().data());
232
233 kurl.setHost(cases[i].host);
234 EXPECT_STREQ(cases[i].expectedHost, kurl.string().utf8().data());
235
236 kurl.setPort(cases[i].port);
237 EXPECT_STREQ(cases[i].expectedPort, kurl.string().utf8().data());
238
239 kurl.setUser(cases[i].user);
240 EXPECT_STREQ(cases[i].expectedUser, kurl.string().utf8().data());
241
242 kurl.setPass(cases[i].pass);
243 EXPECT_STREQ(cases[i].expectedPass, kurl.string().utf8().data());
244
245 kurl.setPath(cases[i].path);
246 EXPECT_STREQ(cases[i].expectedPath, kurl.string().utf8().data());
247
248 kurl.setQuery(cases[i].query);
249 EXPECT_STREQ(cases[i].expectedQuery, kurl.string().utf8().data());
250
251 // Refs are tested below. On the Safari 3.1 branch, we don't match their
252 // KURL since we integrated a fix from their trunk.
253 }
254 }
255
256 // Tests that KURL::decodeURLEscapeSequences works as expected
257 TEST(KURLTest, Decode)
258 {
259 struct DecodeCase {
260 const char* input;
261 const char* output;
262 } decodeCases[] = {
263 {"hello, world", "hello, world"},
264 {"%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/"},
265 {"%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/"},
266 {"%20%21%22%23%24%25%26%27%28%29%2a%2B%2C%2D%2e%2f/", " !\"#$%&'()*+,-./ /"},
267 {"%30%31%32%33%34%35%36%37%38%39%3a%3B%3C%3D%3e%3f/", "0123456789:;<=>?/ "},
268 {"%40%41%42%43%44%45%46%47%48%49%4a%4B%4C%4D%4e%4f/", "@ABCDEFGHIJKLMNO/ "},
269 {"%50%51%52%53%54%55%56%57%58%59%5a%5B%5C%5D%5e%5f/", "PQRSTUVWXYZ[\\]^_ /"},
270 {"%60%61%62%63%64%65%66%67%68%69%6a%6B%6C%6D%6e%6f/", "`abcdefghijklmno/ "},
271 {"%70%71%72%73%74%75%76%77%78%79%7a%7B%7C%7D%7e%7f/", "pqrstuvwxyz{|}~\x 7f/"},
272 // Test un-UTF-8-ization.
273 {"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd"},
274 };
275
276 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(decodeCases); i++) {
277 WTF::String input(decodeCases[i].input);
278 WTF::String str = WebCore::decodeURLEscapeSequences(input);
279 EXPECT_STREQ(decodeCases[i].output, str.utf8().data());
280 }
281
282 // Our decode should decode %00
283 WTF::String zero = WebCore::decodeURLEscapeSequences("%00");
284 EXPECT_STRNE("%00", zero.utf8().data());
285
286 // Test the error behavior for invalid UTF-8 (we differ from WebKit here).
287 WTF::String invalid = WebCore::decodeURLEscapeSequences(
288 "%e4%a0%e5%a5%bd");
289 char16 invalidExpectedHelper[4] = { 0x00e4, 0x00a0, 0x597d, 0 };
290 WTF::String invalidExpected(
291 reinterpret_cast<const ::UChar*>(invalidExpectedHelper),
292 3);
293 EXPECT_EQ(invalidExpected, invalid);
294 }
295
296 TEST(KURLTest, Encode)
297 {
298 struct EncodeCase {
299 const char* input;
300 const char* output;
301 } encode_cases[] = {
302 {"hello, world", "hello%2C%20world"},
303 {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
304 "%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"},
305 {"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
306 "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"},
307 {" !\"#$%&'()*+,-./", "%20!%22%23%24%25%26%27()*%2B%2C-./"},
308 {"0123456789:;<=>?",
309 "0123456789%3A%3B%3C%3D%3E%3F"},
310 {"@ABCDEFGHIJKLMNO",
311 "%40ABCDEFGHIJKLMNO"},
312 {"PQRSTUVWXYZ[\\]^_",
313 "PQRSTUVWXYZ%5B%5C%5D%5E_"},
314 {"`abcdefghijklmno",
315 "%60abcdefghijklmno"},
316 {"pqrstuvwxyz{|}~\x7f",
317 "pqrstuvwxyz%7B%7C%7D~%7F"},
318 };
319
320 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(encode_cases); i++) {
321 WTF::String input(encode_cases[i].input);
322 WTF::String expectedOutput(encode_cases[i].output);
323 WTF::String output = WebCore::encodeWithURLEscapeSequences(input);
324 EXPECT_EQ(expectedOutput, output);
325 }
326
327 // Our encode escapes NULLs for safety, so we need to check that too.
328 WTF::String input("\x00\x01", 2);
329 WTF::String reference("%00%01");
330
331 WTF::String output = WebCore::encodeWithURLEscapeSequences(input);
332 EXPECT_EQ(reference, output);
333
334 // Also test that it gets converted to UTF-8 properly.
335 char16 wideInputHelper[3] = { 0x4f60, 0x597d, 0 };
336 WTF::String wideInput(
337 reinterpret_cast<const ::UChar*>(wideInputHelper), 2);
338 WTF::String wideReference("%E4%BD%A0%E5%A5%BD");
339 WTF::String wideOutput =
340 WebCore::encodeWithURLEscapeSequences(wideInput);
341 EXPECT_EQ(wideReference, wideOutput);
342 }
343
344 TEST(KURLTest, ResolveEmpty)
345 {
346 WebCore::KURL emptyBase;
347
348 // WebKit likes to be able to resolve absolute input agains empty base URLs,
349 // which would normally be invalid since the base URL is invalid.
350 const char abs[] = "http://www.google.com/";
351 WebCore::KURL resolveAbs(emptyBase, abs);
352 EXPECT_TRUE(resolveAbs.isValid());
353 EXPECT_STREQ(abs, resolveAbs.string().utf8().data());
354
355 // Resolving a non-relative URL agains the empty one should still error.
356 const char rel[] = "foo.html";
357 WebCore::KURL resolveErr(emptyBase, rel);
358 EXPECT_FALSE(resolveErr.isValid());
359 }
360
361 // WebKit will make empty URLs and set components on them. kurl doesn't allow
362 // replacements on invalid URLs, but here we do.
363 TEST(KURLTest, ReplaceInvalid)
364 {
365 WebCore::KURL kurl;
366
367 EXPECT_FALSE(kurl.isValid());
368 EXPECT_TRUE(kurl.isEmpty());
369 EXPECT_STREQ("", kurl.string().utf8().data());
370
371 kurl.setProtocol("http");
372 // GKURL will say that a URL with just a scheme is invalid, KURL will not.
373 EXPECT_FALSE(kurl.isValid());
374 EXPECT_FALSE(kurl.isEmpty());
375 // At this point, we do things slightly differently if there is only a schem e.
376 // We check the results here to make it more obvious what is going on, but i t
377 // shouldn't be a big deal if these change.
378 EXPECT_STREQ("http:", kurl.string().utf8().data());
379
380 kurl.setHost("www.google.com");
381 EXPECT_TRUE(kurl.isValid());
382 EXPECT_FALSE(kurl.isEmpty());
383 EXPECT_STREQ("http://www.google.com/", kurl.string().utf8().data());
384
385 kurl.setPort(8000);
386 EXPECT_TRUE(kurl.isValid());
387 EXPECT_FALSE(kurl.isEmpty());
388 EXPECT_STREQ("http://www.google.com:8000/", kurl.string().utf8().data());
389
390 kurl.setPath("/favicon.ico");
391 EXPECT_TRUE(kurl.isValid());
392 EXPECT_FALSE(kurl.isEmpty());
393 EXPECT_STREQ("http://www.google.com:8000/favicon.ico", kurl.string().utf8(). data());
394
395 // Now let's test that giving an invalid replacement fails. Invalid
396 // protocols fail without modifying the URL, which should remain valid.
397 EXPECT_FALSE(kurl.setProtocol("f/sj#@"));
398 EXPECT_TRUE(kurl.isValid());
399 }
400
401 TEST(KURLTest, Path)
402 {
403 const char initial[] = "http://www.google.com/path/foo";
404 WebCore::KURL kurl(WebCore::ParsedURLString, initial);
405
406 // Clear by setting a null string.
407 WTF::String nullString;
408 EXPECT_TRUE(nullString.isNull());
409 kurl.setPath(nullString);
410 EXPECT_STREQ("http://www.google.com/", kurl.string().utf8().data());
411 }
412
413 // Test that setting the query to different things works. Thq query is handled
414 // a littler differently than some of the other components.
415 TEST(KURLTest, Query)
416 {
417 const char initial[] = "http://www.google.com/search?q=awesome";
418 WebCore::KURL kurl(WebCore::ParsedURLString, initial);
419
420 // Clear by setting a null string.
421 WTF::String nullString;
422 EXPECT_TRUE(nullString.isNull());
423 kurl.setQuery(nullString);
424 EXPECT_STREQ("http://www.google.com/search", kurl.string().utf8().data());
425
426 // Clear by setting an empty string.
427 kurl = WebCore::KURL(WebCore::ParsedURLString, initial);
428 WTF::String emptyString("");
429 EXPECT_FALSE(emptyString.isNull());
430 kurl.setQuery(emptyString);
431 EXPECT_STREQ("http://www.google.com/search?", kurl.string().utf8().data());
432
433 // Set with something that begins in a question mark.
434 const char question[] = "?foo=bar";
435 kurl.setQuery(question);
436 EXPECT_STREQ("http://www.google.com/search?foo=bar",
437 kurl.string().utf8().data());
438
439 // Set with something that doesn't begin in a question mark.
440 const char query[] = "foo=bar";
441 kurl.setQuery(query);
442 EXPECT_STREQ("http://www.google.com/search?foo=bar",
443 kurl.string().utf8().data());
444 }
445
446 TEST(KURLTest, Ref)
447 {
448 WebCore::KURL kurl(WebCore::ParsedURLString, "http://foo/bar#baz");
449
450 // Basic ref setting.
451 WebCore::KURL cur(WebCore::ParsedURLString, "http://foo/bar");
452 cur.setFragmentIdentifier("asdf");
453 EXPECT_STREQ("http://foo/bar#asdf", cur.string().utf8().data());
454 cur = kurl;
455 cur.setFragmentIdentifier("asdf");
456 EXPECT_STREQ("http://foo/bar#asdf", cur.string().utf8().data());
457
458 // Setting a ref to the empty string will set it to "#".
459 cur = WebCore::KURL(WebCore::ParsedURLString, "http://foo/bar");
460 cur.setFragmentIdentifier("");
461 EXPECT_STREQ("http://foo/bar#", cur.string().utf8().data());
462 cur = kurl;
463 cur.setFragmentIdentifier("");
464 EXPECT_STREQ("http://foo/bar#", cur.string().utf8().data());
465
466 // Setting the ref to the null string will clear it altogether.
467 cur = WebCore::KURL(WebCore::ParsedURLString, "http://foo/bar");
468 cur.setFragmentIdentifier(WTF::String());
469 EXPECT_STREQ("http://foo/bar", cur.string().utf8().data());
470 cur = kurl;
471 cur.setFragmentIdentifier(WTF::String());
472 EXPECT_STREQ("http://foo/bar", cur.string().utf8().data());
473 }
474
475 TEST(KURLTest, Empty)
476 {
477 WebCore::KURL kurl;
478
479 // First test that regular empty URLs are the same.
480 EXPECT_TRUE(kurl.isEmpty());
481 EXPECT_FALSE(kurl.isValid());
482 EXPECT_TRUE(kurl.isNull());
483 EXPECT_TRUE(kurl.string().isNull());
484 EXPECT_TRUE(kurl.string().isEmpty());
485
486 // Test resolving a null URL on an empty string.
487 WebCore::KURL kurl2(kurl, "");
488 EXPECT_TRUE(kurl2.isNull());
489 EXPECT_TRUE(kurl2.isEmpty());
490 EXPECT_FALSE(kurl2.isValid());
491 EXPECT_TRUE(kurl2.string().isNull());
492 EXPECT_TRUE(kurl2.string().isEmpty());
493 EXPECT_TRUE(kurl2.string().isNull());
494 EXPECT_TRUE(kurl2.string().isEmpty());
495
496 // Resolve the null URL on a null string.
497 WebCore::KURL kurl22(kurl, WTF::String());
498 EXPECT_TRUE(kurl22.isNull());
499 EXPECT_TRUE(kurl22.isEmpty());
500 EXPECT_FALSE(kurl22.isValid());
501 EXPECT_TRUE(kurl22.string().isNull());
502 EXPECT_TRUE(kurl22.string().isEmpty());
503 EXPECT_TRUE(kurl22.string().isNull());
504 EXPECT_TRUE(kurl22.string().isEmpty());
505
506 // Test non-hierarchical schemes resolving. The actual URLs will be differen t.
507 // WebKit's one will set the string to "something.gif" and we'll set it to a n
508 // empty string. I think either is OK, so we just check our behavior.
509 WebCore::KURL kurl3(WebCore::KURL(WebCore::ParsedURLString, "data:foo"),
510 "something.gif");
511 EXPECT_TRUE(kurl3.isEmpty());
512 EXPECT_FALSE(kurl3.isValid());
513
514 // Test for weird isNull string input,
515 // see: http://bugs.webkit.org/show_bug.cgi?id=16487
516 WebCore::KURL kurl4(WebCore::ParsedURLString, kurl.string());
517 EXPECT_TRUE(kurl4.isEmpty());
518 EXPECT_FALSE(kurl4.isValid());
519 EXPECT_TRUE(kurl4.string().isNull());
520 EXPECT_TRUE(kurl4.string().isEmpty());
521
522 // Resolving an empty URL on an invalid string.
523 WebCore::KURL kurl5(WebCore::KURL(), "foo.js");
524 // We'll be empty in this case, but KURL won't be. Should be OK.
525 // EXPECT_EQ(kurl5.isEmpty(), kurl5.isEmpty());
526 // EXPECT_EQ(kurl5.string().isEmpty(), kurl5.string().isEmpty());
527 EXPECT_FALSE(kurl5.isValid());
528 EXPECT_TRUE(kurl5.string().isNull());
529
530 // Empty string as input
531 WebCore::KURL kurl6(WebCore::ParsedURLString, "");
532 EXPECT_TRUE(kurl6.isEmpty());
533 EXPECT_FALSE(kurl6.isValid());
534 EXPECT_TRUE(kurl6.string().isNull());
535 EXPECT_TRUE(kurl6.string().isEmpty());
536
537 // Non-empty but invalid C string as input.
538 WebCore::KURL kurl7(WebCore::ParsedURLString, "foo.js");
539 // WebKit will actually say this URL has the string "foo.js" but is invalid.
540 // We don't do that.
541 // EXPECT_EQ(kurl7.isEmpty(), kurl7.isEmpty());
542 EXPECT_FALSE(kurl7.isValid());
543 EXPECT_TRUE(kurl7.string().isNull());
544 }
545
546 TEST(KURLTest, UserPass)
547 {
548 const char* src = "http://user:pass@google.com/";
549 WebCore::KURL kurl(WebCore::ParsedURLString, src);
550
551 // Clear just the username.
552 kurl.setUser("");
553 EXPECT_EQ("http://:pass@google.com/", kurl.string());
554
555 // Clear just the password.
556 kurl = WebCore::KURL(WebCore::ParsedURLString, src);
557 kurl.setPass("");
558 EXPECT_EQ("http://user@google.com/", kurl.string());
559
560 // Now clear both.
561 kurl.setUser("");
562 EXPECT_EQ("http://google.com/", kurl.string());
563 }
564
565 TEST(KURLTest, Offsets)
566 {
567 const char* src1 = "http://user:pass@google.com/foo/bar.html?baz=query#ref";
568 WebCore::KURL kurl1(WebCore::ParsedURLString, src1);
569
570 EXPECT_EQ(17u, kurl1.hostStart());
571 EXPECT_EQ(27u, kurl1.hostEnd());
572 EXPECT_EQ(27u, kurl1.pathStart());
573 EXPECT_EQ(40u, kurl1.pathEnd());
574 EXPECT_EQ(32u, kurl1.pathAfterLastSlash());
575
576 const char* src2 = "http://google.com/foo/";
577 WebCore::KURL kurl2(WebCore::ParsedURLString, src2);
578
579 EXPECT_EQ(7u, kurl2.hostStart());
580 EXPECT_EQ(17u, kurl2.hostEnd());
581 EXPECT_EQ(17u, kurl2.pathStart());
582 EXPECT_EQ(22u, kurl2.pathEnd());
583 EXPECT_EQ(22u, kurl2.pathAfterLastSlash());
584
585 const char* src3 = "javascript:foobar";
586 WebCore::KURL kurl3(WebCore::ParsedURLString, src3);
587
588 EXPECT_EQ(11u, kurl3.hostStart());
589 EXPECT_EQ(11u, kurl3.hostEnd());
590 EXPECT_EQ(11u, kurl3.pathStart());
591 EXPECT_EQ(17u, kurl3.pathEnd());
592 EXPECT_EQ(11u, kurl3.pathAfterLastSlash());
593 }
594
595 TEST(KURLTest, DeepCopy)
596 {
597 const char url[] = "http://www.google.com/";
598 WebCore::KURL src(WebCore::ParsedURLString, url);
599 EXPECT_TRUE(src.string() == url); // This really just initializes the cache.
600 WebCore::KURL dest = src.copy();
601 EXPECT_TRUE(dest.string() == url); // This really just initializes the cache .
602
603 // The pointers should be different for both UTF-8 and UTF-16.
604 EXPECT_NE(dest.string().impl(), src.string().impl());
605 }
606
607 TEST(KURLTest, LastPathComponent)
608 {
609 WebCore::KURL url1(WebCore::ParsedURLString, "http://host/path/to/file.txt") ;
610 EXPECT_EQ("file.txt", url1.lastPathComponent());
611
612 WebCore::KURL invalidUTF8(WebCore::ParsedURLString, "http://a@9%aa%:/path/to /file.txt");
613 EXPECT_EQ(String(), invalidUTF8.lastPathComponent());
614 }
615
616 TEST(KURLTest, IsHierarchical)
617 {
618 WebCore::KURL url1(WebCore::ParsedURLString, "http://host/path/to/file.txt") ;
619 EXPECT_TRUE(url1.isHierarchical());
620
621 WebCore::KURL invalidUTF8(WebCore::ParsedURLString, "http://a@9%aa%:/path/to /file.txt");
622 EXPECT_FALSE(invalidUTF8.isHierarchical());
623 }
624
625 TEST(KURLTest, PathAfterLastSlash)
626 {
627 WebCore::KURL url1(WebCore::ParsedURLString, "http://host/path/to/file.txt") ;
628 EXPECT_EQ(20u, url1.pathAfterLastSlash());
629
630 WebCore::KURL invalidUTF8(WebCore::ParsedURLString, "http://a@9%aa%:/path/to /file.txt");
631 EXPECT_EQ(0u, invalidUTF8.pathAfterLastSlash());
632 }
633
634 TEST(KURLTest, ProtocolIsInHTTPFamily)
635 {
636 WebCore::KURL url1(WebCore::ParsedURLString, "http://host/path/to/file.txt") ;
637 EXPECT_TRUE(url1.protocolIsInHTTPFamily());
638
639 WebCore::KURL invalidUTF8(WebCore::ParsedURLString, "http://a@9%aa%:/path/to /file.txt");
640 EXPECT_FALSE(invalidUTF8.protocolIsInHTTPFamily());
641 }
642
643 TEST(KURLTest, ProtocolIs)
644 {
645 WebCore::KURL url1(WebCore::ParsedURLString, "foo://bar");
646 EXPECT_TRUE(url1.protocolIs("foo"));
647 EXPECT_FALSE(url1.protocolIs("foo-bar"));
648
649 WebCore::KURL url2(WebCore::ParsedURLString, "foo-bar:");
650 EXPECT_TRUE(url2.protocolIs("foo-bar"));
651 EXPECT_FALSE(url2.protocolIs("foo"));
652
653 WebCore::KURL invalidUTF8(WebCore::ParsedURLString, "http://a@9%aa%:");
654 EXPECT_FALSE(invalidUTF8.protocolIs("http"));
655 EXPECT_TRUE(invalidUTF8.protocolIs(""));
656 }
657
658 } // namespace
OLDNEW
« no previous file with comments | « Source/weborigin/KURLHash.h ('k') | Source/weborigin/KnownPorts.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698