1 /* 2 * Copyright (C) 2015 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.squareup.okhttp; 17 18 import com.squareup.okhttp.UrlComponentEncodingTester.Component; 19 import com.squareup.okhttp.UrlComponentEncodingTester.Encoding; 20 import java.net.MalformedURLException; 21 import java.net.URI; 22 import java.net.URL; 23 import java.net.UnknownHostException; 24 import java.util.Arrays; 25 import java.util.Collections; 26 import java.util.LinkedHashSet; 27 import org.junit.Ignore; 28 import org.junit.Test; 29 30 import static java.util.Collections.singletonList; 31 import static org.junit.Assert.assertEquals; 32 import static org.junit.Assert.assertFalse; 33 import static org.junit.Assert.assertNull; 34 import static org.junit.Assert.fail; 35 36 public final class HttpUrlTest { parseTrimsAsciiWhitespace()37 @Test public void parseTrimsAsciiWhitespace() throws Exception { 38 HttpUrl expected = HttpUrl.parse("http://host/"); 39 assertEquals(expected, HttpUrl.parse("http://host/\f\n\t \r")); // Leading. 40 assertEquals(expected, HttpUrl.parse("\r\n\f \thttp://host/")); // Trailing. 41 assertEquals(expected, HttpUrl.parse(" http://host/ ")); // Both. 42 assertEquals(expected, HttpUrl.parse(" http://host/ ")); // Both. 43 assertEquals(expected, HttpUrl.parse("http://host/").resolve(" ")); 44 assertEquals(expected, HttpUrl.parse("http://host/").resolve(" . ")); 45 } 46 parseHostAsciiNonPrintable()47 @Test public void parseHostAsciiNonPrintable() throws Exception { 48 String host = "host\u0001"; 49 assertNull(HttpUrl.parse("http://" + host + "/")); 50 } 51 parseDoesNotTrimOtherWhitespaceCharacters()52 @Test public void parseDoesNotTrimOtherWhitespaceCharacters() throws Exception { 53 // Whitespace characters list from Google's Guava team: http://goo.gl/IcR9RD 54 assertEquals("/%0B", HttpUrl.parse("http://h/\u000b").encodedPath()); // line tabulation 55 assertEquals("/%1C", HttpUrl.parse("http://h/\u001c").encodedPath()); // information separator 4 56 assertEquals("/%1D", HttpUrl.parse("http://h/\u001d").encodedPath()); // information separator 3 57 assertEquals("/%1E", HttpUrl.parse("http://h/\u001e").encodedPath()); // information separator 2 58 assertEquals("/%1F", HttpUrl.parse("http://h/\u001f").encodedPath()); // information separator 1 59 assertEquals("/%C2%85", HttpUrl.parse("http://h/\u0085").encodedPath()); // next line 60 assertEquals("/%C2%A0", HttpUrl.parse("http://h/\u00a0").encodedPath()); // non-breaking space 61 assertEquals("/%E1%9A%80", HttpUrl.parse("http://h/\u1680").encodedPath()); // ogham space mark 62 assertEquals("/%E1%A0%8E", HttpUrl.parse("http://h/\u180e").encodedPath()); // mongolian vowel separator 63 assertEquals("/%E2%80%80", HttpUrl.parse("http://h/\u2000").encodedPath()); // en quad 64 assertEquals("/%E2%80%81", HttpUrl.parse("http://h/\u2001").encodedPath()); // em quad 65 assertEquals("/%E2%80%82", HttpUrl.parse("http://h/\u2002").encodedPath()); // en space 66 assertEquals("/%E2%80%83", HttpUrl.parse("http://h/\u2003").encodedPath()); // em space 67 assertEquals("/%E2%80%84", HttpUrl.parse("http://h/\u2004").encodedPath()); // three-per-em space 68 assertEquals("/%E2%80%85", HttpUrl.parse("http://h/\u2005").encodedPath()); // four-per-em space 69 assertEquals("/%E2%80%86", HttpUrl.parse("http://h/\u2006").encodedPath()); // six-per-em space 70 assertEquals("/%E2%80%87", HttpUrl.parse("http://h/\u2007").encodedPath()); // figure space 71 assertEquals("/%E2%80%88", HttpUrl.parse("http://h/\u2008").encodedPath()); // punctuation space 72 assertEquals("/%E2%80%89", HttpUrl.parse("http://h/\u2009").encodedPath()); // thin space 73 assertEquals("/%E2%80%8A", HttpUrl.parse("http://h/\u200a").encodedPath()); // hair space 74 assertEquals("/%E2%80%8B", HttpUrl.parse("http://h/\u200b").encodedPath()); // zero-width space 75 assertEquals("/%E2%80%8C", HttpUrl.parse("http://h/\u200c").encodedPath()); // zero-width non-joiner 76 assertEquals("/%E2%80%8D", HttpUrl.parse("http://h/\u200d").encodedPath()); // zero-width joiner 77 assertEquals("/%E2%80%8E", HttpUrl.parse("http://h/\u200e").encodedPath()); // left-to-right mark 78 assertEquals("/%E2%80%8F", HttpUrl.parse("http://h/\u200f").encodedPath()); // right-to-left mark 79 assertEquals("/%E2%80%A8", HttpUrl.parse("http://h/\u2028").encodedPath()); // line separator 80 assertEquals("/%E2%80%A9", HttpUrl.parse("http://h/\u2029").encodedPath()); // paragraph separator 81 assertEquals("/%E2%80%AF", HttpUrl.parse("http://h/\u202f").encodedPath()); // narrow non-breaking space 82 assertEquals("/%E2%81%9F", HttpUrl.parse("http://h/\u205f").encodedPath()); // medium mathematical space 83 assertEquals("/%E3%80%80", HttpUrl.parse("http://h/\u3000").encodedPath()); // ideographic space 84 } 85 scheme()86 @Test public void scheme() throws Exception { 87 assertEquals(HttpUrl.parse("http://host/"), HttpUrl.parse("http://host/")); 88 assertEquals(HttpUrl.parse("http://host/"), HttpUrl.parse("Http://host/")); 89 assertEquals(HttpUrl.parse("http://host/"), HttpUrl.parse("http://host/")); 90 assertEquals(HttpUrl.parse("http://host/"), HttpUrl.parse("HTTP://host/")); 91 assertEquals(HttpUrl.parse("https://host/"), HttpUrl.parse("https://host/")); 92 assertEquals(HttpUrl.parse("https://host/"), HttpUrl.parse("HTTPS://host/")); 93 assertEquals(HttpUrl.Builder.ParseResult.UNSUPPORTED_SCHEME, 94 new HttpUrl.Builder().parse(null, "image640://480.png")); 95 assertEquals(null, HttpUrl.parse("httpp://host/")); 96 assertEquals(null, HttpUrl.parse("0ttp://host/")); 97 assertEquals(null, HttpUrl.parse("ht+tp://host/")); 98 assertEquals(null, HttpUrl.parse("ht.tp://host/")); 99 assertEquals(null, HttpUrl.parse("ht-tp://host/")); 100 assertEquals(null, HttpUrl.parse("ht1tp://host/")); 101 assertEquals(null, HttpUrl.parse("httpss://host/")); 102 } 103 parseNoScheme()104 @Test public void parseNoScheme() throws Exception { 105 assertEquals(null, HttpUrl.parse("//host")); 106 assertEquals(null, HttpUrl.parse("/path")); 107 assertEquals(null, HttpUrl.parse("path")); 108 assertEquals(null, HttpUrl.parse("?query")); 109 assertEquals(null, HttpUrl.parse("#fragment")); 110 } 111 resolveNoScheme()112 @Test public void resolveNoScheme() throws Exception { 113 HttpUrl base = HttpUrl.parse("http://host/a/b"); 114 // ANDROID-BEGIN: http://b/29983827 115 // assertEquals(HttpUrl.parse("http://host2/"), base.resolve("//host2")); 116 assertEquals(HttpUrl.parse("http://host2"), base.resolve("//host2")); 117 // ANDROID-END: http://b/29983827 118 assertEquals(HttpUrl.parse("http://host2"), base.resolve("//host2")); 119 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("/path")); 120 assertEquals(HttpUrl.parse("http://host/a/path"), base.resolve("path")); 121 assertEquals(HttpUrl.parse("http://host/a/b?query"), base.resolve("?query")); 122 assertEquals(HttpUrl.parse("http://host/a/b#fragment"), base.resolve("#fragment")); 123 assertEquals(HttpUrl.parse("http://host/a/b"), base.resolve("")); 124 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("\\path")); 125 } 126 127 // ANDROID-BEGIN: http://b/29983827 encodedPath_pathVariations()128 @Test public void encodedPath_pathVariations() throws Exception { 129 assertEquals("", HttpUrl.parse("http://example.com").encodedPath()); 130 assertEquals("", HttpUrl.parse("http://example.com#fragment").encodedPath()); 131 assertEquals("", HttpUrl.parse("http://example.com?arg=value").encodedPath()); 132 assertEquals("/", HttpUrl.parse("http://example.com/").encodedPath()); 133 assertEquals("/", HttpUrl.parse("http://example.com/#fragment").encodedPath()); 134 assertEquals("/", HttpUrl.parse("http://example.com/?arg=value").encodedPath()); 135 assertEquals("/foo", HttpUrl.parse("http://example.com/foo").encodedPath()); 136 assertEquals("/foo/", HttpUrl.parse("http://example.com/foo/").encodedPath()); 137 } 138 newBuilder_pathVariations()139 @Test public void newBuilder_pathVariations() throws Exception { 140 assertNewBuilderRoundtrip("http://example.com"); 141 assertNewBuilderRoundtrip("http://example.com/"); 142 assertNewBuilderRoundtrip("http://example.com/foo"); 143 assertNewBuilderRoundtrip("http://example.com/foo/"); 144 } 145 assertNewBuilderRoundtrip(String urlString)146 private void assertNewBuilderRoundtrip(String urlString) { 147 HttpUrl url = HttpUrl.parse(urlString); 148 assertEquals(url, url.newBuilder().build()); 149 } 150 equals_emptyPathNotSameAsSlash()151 @Test public void equals_emptyPathNotSameAsSlash() throws Exception { 152 assertFalse(HttpUrl.parse("http://example.com").equals(HttpUrl.parse("http://example.com/"))); 153 } 154 parse_pathVariations()155 @Test public void parse_pathVariations() throws Exception { 156 parseThenAssertToStringEquals("http://example.com"); 157 parseThenAssertToStringEquals("https://example.com"); 158 parseThenAssertToStringEquals("http://example.com?value=42"); 159 parseThenAssertToStringEquals("http://example.com:3434"); 160 parseThenAssertToStringEquals("http://example.com#hello"); 161 162 parseThenAssertToStringEquals("http://example.com/"); 163 parseThenAssertToStringEquals("https://example.com/"); 164 parseThenAssertToStringEquals("http://example.com/foo/bar"); 165 parseThenAssertToStringEquals("http://example.com/foo/bar/"); 166 parseThenAssertToStringEquals("http://example.com/foo/bar?value=100"); 167 parseThenAssertToStringEquals("http://example.com/?value=200"); 168 169 { 170 HttpUrl httpUrl = HttpUrl.parse("http://example.com/foo/.."); 171 assertEquals("http://example.com/", httpUrl.toString()); 172 } 173 174 { 175 HttpUrl httpUrl = HttpUrl.parse("http://example.com/.."); 176 assertEquals("http://example.com/", httpUrl.toString()); 177 } 178 } 179 parseThenAssertToStringEquals(String url)180 private static void parseThenAssertToStringEquals(String url) { 181 HttpUrl httpUrl = HttpUrl.parse(url); 182 assertEquals(url, httpUrl.toString()); 183 } 184 resolve_pathVariations()185 @Test public void resolve_pathVariations() throws Exception { 186 HttpUrl baseWithoutSlash = HttpUrl.parse("http://example.com"); 187 assertResolveResult("http://example.com#section", baseWithoutSlash, "#section"); 188 assertResolveResult("http://example.com?attitude=friendly", baseWithoutSlash, 189 "?attitude=friendly"); 190 assertResolveResult("http://example.com", baseWithoutSlash, ""); 191 assertResolveResult("http://example.com/", baseWithoutSlash, "/"); 192 assertResolveResult("http://example.com/foo", baseWithoutSlash, "/foo"); 193 assertResolveResult("http://example.com/foo", baseWithoutSlash, "foo"); 194 195 assertResolveResult("http://other.com", baseWithoutSlash, "http://other.com"); 196 assertResolveResult("http://other.com/", baseWithoutSlash, "http://other.com/"); 197 assertResolveResult("http://other.com/foo", baseWithoutSlash, "http://other.com/foo"); 198 199 HttpUrl baseWithSlash = HttpUrl.parse("http://example.com/"); 200 assertResolveResult("http://example.com/#section", baseWithSlash, "#section"); 201 assertResolveResult("http://example.com/?attitude=friendly", baseWithSlash, 202 "?attitude=friendly"); 203 assertResolveResult("http://example.com/", baseWithSlash, ""); 204 assertResolveResult("http://example.com/", baseWithSlash, "/"); 205 assertResolveResult("http://example.com/foo", baseWithSlash, "/foo"); 206 assertResolveResult("http://example.com/foo", baseWithSlash, "foo"); 207 208 assertResolveResult("http://other.com", baseWithSlash, "http://other.com"); 209 assertResolveResult("http://other.com/", baseWithSlash, "http://other.com/"); 210 assertResolveResult("http://other.com/foo", baseWithSlash, "http://other.com/foo"); 211 } 212 assertResolveResult(String expected, HttpUrl base, String link)213 private void assertResolveResult(String expected, HttpUrl base, String link) { 214 assertEquals(HttpUrl.parse(expected), base.resolve(link)); 215 assertEquals(expected, base.resolve(link).toString()); 216 } 217 // ANDROID-END: http://b/29983827 218 resolveUnsupportedScheme()219 @Test public void resolveUnsupportedScheme() throws Exception { 220 HttpUrl base = HttpUrl.parse("http://a/"); 221 assertEquals(null, base.resolve("ftp://b")); 222 assertEquals(null, base.resolve("ht+tp://b")); 223 assertEquals(null, base.resolve("ht-tp://b")); 224 assertEquals(null, base.resolve("ht.tp://b")); 225 } 226 resolveSchemeLikePath()227 @Test public void resolveSchemeLikePath() throws Exception { 228 HttpUrl base = HttpUrl.parse("http://a/"); 229 assertEquals(HttpUrl.parse("http://a/http//b/"), base.resolve("http//b/")); 230 assertEquals(HttpUrl.parse("http://a/ht+tp//b/"), base.resolve("ht+tp//b/")); 231 assertEquals(HttpUrl.parse("http://a/ht-tp//b/"), base.resolve("ht-tp//b/")); 232 assertEquals(HttpUrl.parse("http://a/ht.tp//b/"), base.resolve("ht.tp//b/")); 233 } 234 parseAuthoritySlashCountDoesntMatter()235 @Test public void parseAuthoritySlashCountDoesntMatter() throws Exception { 236 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:host/path")); 237 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:/host/path")); 238 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:\\host/path")); 239 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http://host/path")); 240 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:\\/host/path")); 241 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:/\\host/path")); 242 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:\\\\host/path")); 243 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:///host/path")); 244 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:\\//host/path")); 245 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:/\\/host/path")); 246 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http://\\host/path")); 247 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:\\\\/host/path")); 248 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:/\\\\host/path")); 249 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:\\\\\\host/path")); 250 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http:////host/path")); 251 } 252 resolveAuthoritySlashCountDoesntMatterWithDifferentScheme()253 @Test public void resolveAuthoritySlashCountDoesntMatterWithDifferentScheme() throws Exception { 254 HttpUrl base = HttpUrl.parse("https://a/b/c"); 255 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:host/path")); 256 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:/host/path")); 257 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:\\host/path")); 258 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http://host/path")); 259 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:\\/host/path")); 260 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:/\\host/path")); 261 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:\\\\host/path")); 262 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:///host/path")); 263 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:\\//host/path")); 264 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:/\\/host/path")); 265 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http://\\host/path")); 266 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:\\\\/host/path")); 267 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:/\\\\host/path")); 268 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:\\\\\\host/path")); 269 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:////host/path")); 270 } 271 resolveAuthoritySlashCountMattersWithSameScheme()272 @Test public void resolveAuthoritySlashCountMattersWithSameScheme() throws Exception { 273 HttpUrl base = HttpUrl.parse("http://a/b/c"); 274 assertEquals(HttpUrl.parse("http://a/b/host/path"), base.resolve("http:host/path")); 275 assertEquals(HttpUrl.parse("http://a/host/path"), base.resolve("http:/host/path")); 276 assertEquals(HttpUrl.parse("http://a/host/path"), base.resolve("http:\\host/path")); 277 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http://host/path")); 278 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:\\/host/path")); 279 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:/\\host/path")); 280 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:\\\\host/path")); 281 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:///host/path")); 282 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:\\//host/path")); 283 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:/\\/host/path")); 284 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http://\\host/path")); 285 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:\\\\/host/path")); 286 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:/\\\\host/path")); 287 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:\\\\\\host/path")); 288 assertEquals(HttpUrl.parse("http://host/path"), base.resolve("http:////host/path")); 289 } 290 username()291 @Test public void username() throws Exception { 292 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http://@host/path")); 293 assertEquals(HttpUrl.parse("http://user@host/path"), HttpUrl.parse("http://user@host/path")); 294 } 295 296 /** Given multiple '@' characters, the last one is the delimiter. */ authorityWithMultipleAtSigns()297 @Test public void authorityWithMultipleAtSigns() throws Exception { 298 HttpUrl httpUrl = HttpUrl.parse("http://foo@bar@baz/path"); 299 assertEquals("foo@bar", httpUrl.username()); 300 assertEquals("", httpUrl.password()); 301 assertEquals(HttpUrl.parse("http://foo%40bar@baz/path"), httpUrl); 302 } 303 304 /** Given multiple ':' characters, the first one is the delimiter. */ authorityWithMultipleColons()305 @Test public void authorityWithMultipleColons() throws Exception { 306 HttpUrl httpUrl = HttpUrl.parse("http://foo:pass1@bar:pass2@baz/path"); 307 assertEquals("foo", httpUrl.username()); 308 assertEquals("pass1@bar:pass2", httpUrl.password()); 309 assertEquals(HttpUrl.parse("http://foo:pass1%40bar%3Apass2@baz/path"), httpUrl); 310 } 311 usernameAndPassword()312 @Test public void usernameAndPassword() throws Exception { 313 assertEquals(HttpUrl.parse("http://username:password@host/path"), 314 HttpUrl.parse("http://username:password@host/path")); 315 assertEquals(HttpUrl.parse("http://username@host/path"), 316 HttpUrl.parse("http://username:@host/path")); 317 } 318 passwordWithEmptyUsername()319 @Test public void passwordWithEmptyUsername() throws Exception { 320 // Chrome doesn't mind, but Firefox rejects URLs with empty usernames and non-empty passwords. 321 assertEquals(HttpUrl.parse("http://host/path"), HttpUrl.parse("http://:@host/path")); 322 assertEquals("password%40", HttpUrl.parse("http://:password@@host/path").encodedPassword()); 323 } 324 unprintableCharactersArePercentEncoded()325 @Test public void unprintableCharactersArePercentEncoded() throws Exception { 326 assertEquals("/%00", HttpUrl.parse("http://host/\u0000").encodedPath()); 327 assertEquals("/%08", HttpUrl.parse("http://host/\u0008").encodedPath()); 328 assertEquals("/%EF%BF%BD", HttpUrl.parse("http://host/\ufffd").encodedPath()); 329 } 330 usernameCharacters()331 @Test public void usernameCharacters() throws Exception { 332 new UrlComponentEncodingTester() 333 .override(Encoding.PERCENT, '[', ']', '{', '}', '|', '^', '\'', ';', '=', '@') 334 .override(Encoding.SKIP, ':', '/', '\\', '?', '#') 335 .skipForUri('%') 336 .test(Component.USER); 337 } 338 passwordCharacters()339 @Test public void passwordCharacters() throws Exception { 340 new UrlComponentEncodingTester() 341 .override(Encoding.PERCENT, '[', ']', '{', '}', '|', '^', '\'', ':', ';', '=', '@') 342 .override(Encoding.SKIP, '/', '\\', '?', '#') 343 .skipForUri('%') 344 .test(Component.PASSWORD); 345 } 346 hostContainsIllegalCharacter()347 @Test public void hostContainsIllegalCharacter() throws Exception { 348 assertEquals(null, HttpUrl.parse("http://\n/")); 349 assertEquals(null, HttpUrl.parse("http:// /")); 350 assertEquals(null, HttpUrl.parse("http://%20/")); 351 } 352 hostnameLowercaseCharactersMappedDirectly()353 @Test public void hostnameLowercaseCharactersMappedDirectly() throws Exception { 354 assertEquals("abcd", HttpUrl.parse("http://abcd").host()); 355 assertEquals("xn--4xa", HttpUrl.parse("http://σ").host()); 356 } 357 hostnameUppercaseCharactersConvertedToLowercase()358 @Test public void hostnameUppercaseCharactersConvertedToLowercase() throws Exception { 359 assertEquals("abcd", HttpUrl.parse("http://ABCD").host()); 360 assertEquals("xn--4xa", HttpUrl.parse("http://Σ").host()); 361 } 362 hostnameIgnoredCharacters()363 @Test public void hostnameIgnoredCharacters() throws Exception { 364 // The soft hyphen () should be ignored. 365 assertEquals("abcd", HttpUrl.parse("http://AB\u00adCD").host()); 366 } 367 hostnameMultipleCharacterMapping()368 @Test public void hostnameMultipleCharacterMapping() throws Exception { 369 // Map the single character telephone symbol (℡) to the string "tel". 370 assertEquals("tel", HttpUrl.parse("http://\u2121").host()); 371 } 372 hostnameMappingLastMappedCodePoint()373 @Test public void hostnameMappingLastMappedCodePoint() throws Exception { 374 assertEquals("xn--pu5l", HttpUrl.parse("http://\uD87E\uDE1D").host()); 375 } 376 377 @Ignore("The java.net.IDN implementation doesn't ignore characters that it should.") hostnameMappingLastIgnoredCodePoint()378 @Test public void hostnameMappingLastIgnoredCodePoint() throws Exception { 379 assertEquals("abcd", HttpUrl.parse("http://ab\uDB40\uDDEFcd").host()); 380 } 381 hostnameMappingLastDisallowedCodePoint()382 @Test public void hostnameMappingLastDisallowedCodePoint() throws Exception { 383 assertEquals(null, HttpUrl.parse("http://\uDBFF\uDFFF")); 384 } 385 hostIpv6()386 @Test public void hostIpv6() throws Exception { 387 // Square braces are absent from host()... 388 assertEquals("::1", HttpUrl.parse("http://[::1]/").host()); 389 390 // ... but they're included in toString(). 391 assertEquals("http://[::1]/", HttpUrl.parse("http://[::1]/").toString()); 392 393 // IPv6 colons don't interfere with port numbers or passwords. 394 assertEquals(8080, HttpUrl.parse("http://[::1]:8080/").port()); 395 assertEquals("password", HttpUrl.parse("http://user:password@[::1]/").password()); 396 assertEquals("::1", HttpUrl.parse("http://user:password@[::1]:8080/").host()); 397 398 // Permit the contents of IPv6 addresses to be percent-encoded... 399 assertEquals("::1", HttpUrl.parse("http://[%3A%3A%31]/").host()); 400 401 // Including the Square braces themselves! (This is what Chrome does.) 402 assertEquals("::1", HttpUrl.parse("http://%5B%3A%3A1%5D/").host()); 403 } 404 hostIpv6AddressDifferentFormats()405 @Test public void hostIpv6AddressDifferentFormats() throws Exception { 406 // Multiple representations of the same address; see http://tools.ietf.org/html/rfc5952. 407 String a3 = "2001:db8::1:0:0:1"; 408 assertEquals(a3, HttpUrl.parse("http://[2001:db8:0:0:1:0:0:1]").host()); 409 assertEquals(a3, HttpUrl.parse("http://[2001:0db8:0:0:1:0:0:1]").host()); 410 assertEquals(a3, HttpUrl.parse("http://[2001:db8::1:0:0:1]").host()); 411 assertEquals(a3, HttpUrl.parse("http://[2001:db8::0:1:0:0:1]").host()); 412 assertEquals(a3, HttpUrl.parse("http://[2001:0db8::1:0:0:1]").host()); 413 assertEquals(a3, HttpUrl.parse("http://[2001:db8:0:0:1::1]").host()); 414 assertEquals(a3, HttpUrl.parse("http://[2001:db8:0000:0:1::1]").host()); 415 assertEquals(a3, HttpUrl.parse("http://[2001:DB8:0:0:1::1]").host()); 416 } 417 hostIpv6AddressLeadingCompression()418 @Test public void hostIpv6AddressLeadingCompression() throws Exception { 419 assertEquals("::1", HttpUrl.parse("http://[::0001]").host()); 420 assertEquals("::1", HttpUrl.parse("http://[0000::0001]").host()); 421 assertEquals("::1", HttpUrl.parse("http://[0000:0000:0000:0000:0000:0000:0000:0001]").host()); 422 assertEquals("::1", HttpUrl.parse("http://[0000:0000:0000:0000:0000:0000::0001]").host()); 423 } 424 hostIpv6AddressTrailingCompression()425 @Test public void hostIpv6AddressTrailingCompression() throws Exception { 426 assertEquals("1::", HttpUrl.parse("http://[0001:0000::]").host()); 427 assertEquals("1::", HttpUrl.parse("http://[0001::0000]").host()); 428 assertEquals("1::", HttpUrl.parse("http://[0001::]").host()); 429 assertEquals("1::", HttpUrl.parse("http://[1::]").host()); 430 } 431 hostIpv6AddressTooManyDigitsInGroup()432 @Test public void hostIpv6AddressTooManyDigitsInGroup() throws Exception { 433 assertEquals(null, HttpUrl.parse("http://[00000:0000:0000:0000:0000:0000:0000:0001]")); 434 assertEquals(null, HttpUrl.parse("http://[::00001]")); 435 } 436 hostIpv6AddressMisplacedColons()437 @Test public void hostIpv6AddressMisplacedColons() throws Exception { 438 assertEquals(null, HttpUrl.parse("http://[:0000:0000:0000:0000:0000:0000:0000:0001]")); 439 assertEquals(null, HttpUrl.parse("http://[:::0000:0000:0000:0000:0000:0000:0000:0001]")); 440 assertEquals(null, HttpUrl.parse("http://[:1]")); 441 assertEquals(null, HttpUrl.parse("http://[:::1]")); 442 assertEquals(null, HttpUrl.parse("http://[0000:0000:0000:0000:0000:0000:0001:]")); 443 assertEquals(null, HttpUrl.parse("http://[0000:0000:0000:0000:0000:0000:0000:0001:]")); 444 assertEquals(null, HttpUrl.parse("http://[0000:0000:0000:0000:0000:0000:0000:0001::]")); 445 assertEquals(null, HttpUrl.parse("http://[0000:0000:0000:0000:0000:0000:0000:0001:::]")); 446 assertEquals(null, HttpUrl.parse("http://[1:]")); 447 assertEquals(null, HttpUrl.parse("http://[1:::]")); 448 assertEquals(null, HttpUrl.parse("http://[1:::1]")); 449 assertEquals(null, HttpUrl.parse("http://[00000:0000:0000:0000::0000:0000:0000:0001]")); 450 } 451 hostIpv6AddressTooManyGroups()452 @Test public void hostIpv6AddressTooManyGroups() throws Exception { 453 assertEquals(null, HttpUrl.parse("http://[00000:0000:0000:0000:0000:0000:0000:0000:0001]")); 454 } 455 hostIpv6AddressTooMuchCompression()456 @Test public void hostIpv6AddressTooMuchCompression() throws Exception { 457 assertEquals(null, HttpUrl.parse("http://[0000::0000:0000:0000:0000::0001]")); 458 assertEquals(null, HttpUrl.parse("http://[::0000:0000:0000:0000::0001]")); 459 } 460 hostIpv6ScopedAddress()461 @Test public void hostIpv6ScopedAddress() throws Exception { 462 // java.net.InetAddress parses scoped addresses. These aren't valid in URLs. 463 assertEquals(null, HttpUrl.parse("http://[::1%2544]")); 464 } 465 hostIpv6WithIpv4Suffix()466 @Test public void hostIpv6WithIpv4Suffix() throws Exception { 467 assertEquals("::1:ffff:ffff", HttpUrl.parse("http://[::1:255.255.255.255]/").host()); 468 assertEquals("::1:0:0", HttpUrl.parse("http://[0:0:0:0:0:1:0.0.0.0]/").host()); 469 } 470 hostIpv6WithIpv4SuffixWithOctalPrefix()471 @Test public void hostIpv6WithIpv4SuffixWithOctalPrefix() throws Exception { 472 // Chrome interprets a leading '0' as octal; Firefox rejects them. (We reject them.) 473 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:0.0.0.000000]/")); 474 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:0.010.0.010]/")); 475 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:0.0.0.000001]/")); 476 } 477 hostIpv6WithIpv4SuffixWithHexadecimalPrefix()478 @Test public void hostIpv6WithIpv4SuffixWithHexadecimalPrefix() throws Exception { 479 // Chrome interprets a leading '0x' as hexadecimal; Firefox rejects them. (We reject them.) 480 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:0.0x10.0.0x10]/")); 481 } 482 hostIpv6WithMalformedIpv4Suffix()483 @Test public void hostIpv6WithMalformedIpv4Suffix() throws Exception { 484 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:0.0:0.0]/")); 485 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:0.0-0.0]/")); 486 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:.255.255.255]/")); 487 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:255..255.255]/")); 488 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:255.255..255]/")); 489 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:0:1:255.255]/")); 490 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:256.255.255.255]/")); 491 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:ff.255.255.255]/")); 492 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:0:1:255.255.255.255]/")); 493 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:1:255.255.255.255]/")); 494 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:1:0.0.0.0:1]/")); 495 assertEquals(null, HttpUrl.parse("http://[0:0.0.0.0:1:0:0:0:0:1]/")); 496 assertEquals(null, HttpUrl.parse("http://[0.0.0.0:0:0:0:0:0:1]/")); 497 } 498 hostIpv6WithIncompleteIpv4Suffix()499 @Test public void hostIpv6WithIncompleteIpv4Suffix() throws Exception { 500 // To Chrome & Safari these are well-formed; Firefox disagrees. (We're consistent with Firefox). 501 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:255.255.255.]/")); 502 assertEquals(null, HttpUrl.parse("http://[0:0:0:0:0:1:255.255.255]/")); 503 } 504 hostIpv6CanonicalForm()505 @Test public void hostIpv6CanonicalForm() throws Exception { 506 assertEquals("abcd:ef01:2345:6789:abcd:ef01:2345:6789", 507 HttpUrl.parse("http://[abcd:ef01:2345:6789:abcd:ef01:2345:6789]/").host()); 508 assertEquals("a::b:0:0:0", HttpUrl.parse("http://[a:0:0:0:b:0:0:0]/").host()); 509 assertEquals("a:b:0:0:c::", HttpUrl.parse("http://[a:b:0:0:c:0:0:0]/").host()); 510 assertEquals("a:b::c:0:0", HttpUrl.parse("http://[a:b:0:0:0:c:0:0]/").host()); 511 assertEquals("a::b:0:0:0", HttpUrl.parse("http://[a:0:0:0:b:0:0:0]/").host()); 512 assertEquals("::a:b:0:0:0", HttpUrl.parse("http://[0:0:0:a:b:0:0:0]/").host()); 513 assertEquals("::a:0:0:0:b", HttpUrl.parse("http://[0:0:0:a:0:0:0:b]/").host()); 514 assertEquals("::a:b:c:d:e:f:1", HttpUrl.parse("http://[0:a:b:c:d:e:f:1]/").host()); 515 assertEquals("a:b:c:d:e:f:1::", HttpUrl.parse("http://[a:b:c:d:e:f:1:0]/").host()); 516 assertEquals("ff01::101", HttpUrl.parse("http://[FF01:0:0:0:0:0:0:101]/").host()); 517 assertEquals("1::", HttpUrl.parse("http://[1:0:0:0:0:0:0:0]/").host()); 518 assertEquals("::1", HttpUrl.parse("http://[0:0:0:0:0:0:0:1]/").host()); 519 assertEquals("::", HttpUrl.parse("http://[0:0:0:0:0:0:0:0]/").host()); 520 } 521 hostIpv4CanonicalForm()522 @Test public void hostIpv4CanonicalForm() throws Exception { 523 assertEquals("255.255.255.255", HttpUrl.parse("http://255.255.255.255/").host()); 524 assertEquals("1.2.3.4", HttpUrl.parse("http://1.2.3.4/").host()); 525 assertEquals("0.0.0.0", HttpUrl.parse("http://0.0.0.0/").host()); 526 } 527 528 @Ignore("java.net.IDN strips trailing trailing dots on Java 7, but not on Java 8.") hostWithTrailingDot()529 @Test public void hostWithTrailingDot() throws Exception { 530 assertEquals("host.", HttpUrl.parse("http://host./").host()); 531 } 532 port()533 @Test public void port() throws Exception { 534 assertEquals(HttpUrl.parse("http://host/"), HttpUrl.parse("http://host:80/")); 535 assertEquals(HttpUrl.parse("http://host:99/"), HttpUrl.parse("http://host:99/")); 536 assertEquals(HttpUrl.parse("http://host/"), HttpUrl.parse("http://host:/")); 537 assertEquals(65535, HttpUrl.parse("http://host:65535/").port()); 538 assertEquals(null, HttpUrl.parse("http://host:0/")); 539 assertEquals(null, HttpUrl.parse("http://host:65536/")); 540 assertEquals(null, HttpUrl.parse("http://host:-1/")); 541 assertEquals(null, HttpUrl.parse("http://host:a/")); 542 assertEquals(null, HttpUrl.parse("http://host:%39%39/")); 543 } 544 pathCharacters()545 @Test public void pathCharacters() throws Exception { 546 new UrlComponentEncodingTester() 547 .override(Encoding.PERCENT, '^', '{', '}', '|') 548 .override(Encoding.SKIP, '\\', '?', '#') 549 .skipForUri('%', '[', ']') 550 .test(Component.PATH); 551 } 552 queryCharacters()553 @Test public void queryCharacters() throws Exception { 554 new UrlComponentEncodingTester() 555 .override(Encoding.IDENTITY, '?', '`') 556 .override(Encoding.PERCENT, '\'') 557 .override(Encoding.SKIP, '#', '+') 558 .skipForUri('%', '\\', '^', '`', '{', '|', '}') 559 .test(Component.QUERY); 560 } 561 fragmentCharacters()562 @Test public void fragmentCharacters() throws Exception { 563 new UrlComponentEncodingTester() 564 .override(Encoding.IDENTITY, ' ', '"', '#', '<', '>', '?', '`') 565 .skipForUri('%', ' ', '"', '#', '<', '>', '\\', '^', '`', '{', '|', '}') 566 .identityForNonAscii() 567 .test(Component.FRAGMENT); 568 } 569 fragmentNonAscii()570 @Test public void fragmentNonAscii() throws Exception { 571 HttpUrl url = HttpUrl.parse("http://host/#Σ"); 572 assertEquals("http://host/#Σ", url.toString()); 573 assertEquals("Σ", url.fragment()); 574 assertEquals("Σ", url.encodedFragment()); 575 assertEquals("http://host/#Σ", url.uri().toString()); 576 } 577 fragmentNonAsciiThatOffendsJavaNetUri()578 @Test public void fragmentNonAsciiThatOffendsJavaNetUri() throws Exception { 579 HttpUrl url = HttpUrl.parse("http://host/#\u0080"); 580 assertEquals("http://host/#\u0080", url.toString()); 581 assertEquals("\u0080", url.fragment()); 582 assertEquals("\u0080", url.encodedFragment()); 583 assertEquals(new URI("http://host/#"), url.uri()); // Control characters may be stripped! 584 } 585 fragmentPercentEncodedNonAscii()586 @Test public void fragmentPercentEncodedNonAscii() throws Exception { 587 HttpUrl url = HttpUrl.parse("http://host/#%C2%80"); 588 assertEquals("http://host/#%C2%80", url.toString()); 589 assertEquals("\u0080", url.fragment()); 590 assertEquals("%C2%80", url.encodedFragment()); 591 assertEquals("http://host/#%C2%80", url.uri().toString()); 592 } 593 fragmentPercentEncodedPartialCodePoint()594 @Test public void fragmentPercentEncodedPartialCodePoint() throws Exception { 595 HttpUrl url = HttpUrl.parse("http://host/#%80"); 596 assertEquals("http://host/#%80", url.toString()); 597 assertEquals("\ufffd", url.fragment()); // Unicode replacement character. 598 assertEquals("%80", url.encodedFragment()); 599 assertEquals("http://host/#%80", url.uri().toString()); 600 } 601 relativePath()602 @Test public void relativePath() throws Exception { 603 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 604 assertEquals(HttpUrl.parse("http://host/a/b/d/e/f"), base.resolve("d/e/f")); 605 assertEquals(HttpUrl.parse("http://host/d/e/f"), base.resolve("../../d/e/f")); 606 assertEquals(HttpUrl.parse("http://host/a/"), base.resolve("..")); 607 assertEquals(HttpUrl.parse("http://host/"), base.resolve("../..")); 608 assertEquals(HttpUrl.parse("http://host/"), base.resolve("../../..")); 609 assertEquals(HttpUrl.parse("http://host/a/b/"), base.resolve(".")); 610 assertEquals(HttpUrl.parse("http://host/a/"), base.resolve("././..")); 611 assertEquals(HttpUrl.parse("http://host/a/b/c/"), base.resolve("c/d/../e/../")); 612 assertEquals(HttpUrl.parse("http://host/a/b/..e/"), base.resolve("..e/")); 613 assertEquals(HttpUrl.parse("http://host/a/b/e/f../"), base.resolve("e/f../")); 614 assertEquals(HttpUrl.parse("http://host/a/"), base.resolve("%2E.")); 615 assertEquals(HttpUrl.parse("http://host/a/"), base.resolve(".%2E")); 616 assertEquals(HttpUrl.parse("http://host/a/"), base.resolve("%2E%2E")); 617 assertEquals(HttpUrl.parse("http://host/a/"), base.resolve("%2e.")); 618 assertEquals(HttpUrl.parse("http://host/a/"), base.resolve(".%2e")); 619 assertEquals(HttpUrl.parse("http://host/a/"), base.resolve("%2e%2e")); 620 assertEquals(HttpUrl.parse("http://host/a/b/"), base.resolve("%2E")); 621 assertEquals(HttpUrl.parse("http://host/a/b/"), base.resolve("%2e")); 622 } 623 relativePathWithTrailingSlash()624 @Test public void relativePathWithTrailingSlash() throws Exception { 625 HttpUrl base = HttpUrl.parse("http://host/a/b/c/"); 626 assertEquals(HttpUrl.parse("http://host/a/b/"), base.resolve("..")); 627 assertEquals(HttpUrl.parse("http://host/a/b/"), base.resolve("../")); 628 assertEquals(HttpUrl.parse("http://host/a/"), base.resolve("../..")); 629 assertEquals(HttpUrl.parse("http://host/a/"), base.resolve("../../")); 630 assertEquals(HttpUrl.parse("http://host/"), base.resolve("../../..")); 631 assertEquals(HttpUrl.parse("http://host/"), base.resolve("../../../")); 632 assertEquals(HttpUrl.parse("http://host/"), base.resolve("../../../..")); 633 assertEquals(HttpUrl.parse("http://host/"), base.resolve("../../../../")); 634 assertEquals(HttpUrl.parse("http://host/a"), base.resolve("../../../../a")); 635 assertEquals(HttpUrl.parse("http://host/"), base.resolve("../../../../a/..")); 636 assertEquals(HttpUrl.parse("http://host/a/"), base.resolve("../../../../a/b/..")); 637 } 638 pathWithBackslash()639 @Test public void pathWithBackslash() throws Exception { 640 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 641 assertEquals(HttpUrl.parse("http://host/a/b/d/e/f"), base.resolve("d\\e\\f")); 642 assertEquals(HttpUrl.parse("http://host/d/e/f"), base.resolve("../..\\d\\e\\f")); 643 assertEquals(HttpUrl.parse("http://host/"), base.resolve("..\\..")); 644 } 645 relativePathWithSameScheme()646 @Test public void relativePathWithSameScheme() throws Exception { 647 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 648 assertEquals(HttpUrl.parse("http://host/a/b/d/e/f"), base.resolve("http:d/e/f")); 649 assertEquals(HttpUrl.parse("http://host/d/e/f"), base.resolve("http:../../d/e/f")); 650 } 651 decodeUsername()652 @Test public void decodeUsername() { 653 assertEquals("user", HttpUrl.parse("http://user@host/").username()); 654 assertEquals("\uD83C\uDF69", HttpUrl.parse("http://%F0%9F%8D%A9@host/").username()); 655 } 656 decodePassword()657 @Test public void decodePassword() { 658 assertEquals("password", HttpUrl.parse("http://user:password@host/").password()); 659 assertEquals("", HttpUrl.parse("http://user:@host/").password()); 660 assertEquals("\uD83C\uDF69", HttpUrl.parse("http://user:%F0%9F%8D%A9@host/").password()); 661 } 662 decodeSlashCharacterInDecodedPathSegment()663 @Test public void decodeSlashCharacterInDecodedPathSegment() { 664 assertEquals(Arrays.asList("a/b/c"), 665 HttpUrl.parse("http://host/a%2Fb%2Fc").pathSegments()); 666 } 667 decodeEmptyPathSegments()668 @Test public void decodeEmptyPathSegments() { 669 assertEquals(Arrays.asList(""), 670 HttpUrl.parse("http://host/").pathSegments()); 671 } 672 percentDecode()673 @Test public void percentDecode() throws Exception { 674 assertEquals(Arrays.asList("\u0000"), 675 HttpUrl.parse("http://host/%00").pathSegments()); 676 assertEquals(Arrays.asList("a", "\u2603", "c"), 677 HttpUrl.parse("http://host/a/%E2%98%83/c").pathSegments()); 678 assertEquals(Arrays.asList("a", "\uD83C\uDF69", "c"), 679 HttpUrl.parse("http://host/a/%F0%9F%8D%A9/c").pathSegments()); 680 assertEquals(Arrays.asList("a", "b", "c"), 681 HttpUrl.parse("http://host/a/%62/c").pathSegments()); 682 assertEquals(Arrays.asList("a", "z", "c"), 683 HttpUrl.parse("http://host/a/%7A/c").pathSegments()); 684 assertEquals(Arrays.asList("a", "z", "c"), 685 HttpUrl.parse("http://host/a/%7a/c").pathSegments()); 686 } 687 malformedPercentEncoding()688 @Test public void malformedPercentEncoding() { 689 assertEquals(Arrays.asList("a%f", "b"), 690 HttpUrl.parse("http://host/a%f/b").pathSegments()); 691 assertEquals(Arrays.asList("%", "b"), 692 HttpUrl.parse("http://host/%/b").pathSegments()); 693 assertEquals(Arrays.asList("%"), 694 HttpUrl.parse("http://host/%").pathSegments()); 695 } 696 malformedUtf8Encoding()697 @Test public void malformedUtf8Encoding() { 698 // Replace a partial UTF-8 sequence with the Unicode replacement character. 699 assertEquals(Arrays.asList("a", "\ufffdx", "c"), 700 HttpUrl.parse("http://host/a/%E2%98x/c").pathSegments()); 701 } 702 incompleteUrlComposition()703 @Test public void incompleteUrlComposition() throws Exception { 704 try { 705 new HttpUrl.Builder().scheme("http").build(); 706 fail(); 707 } catch (IllegalStateException expected) { 708 assertEquals("host == null", expected.getMessage()); 709 } 710 try { 711 new HttpUrl.Builder().host("host").build(); 712 fail(); 713 } catch (IllegalStateException expected) { 714 assertEquals("scheme == null", expected.getMessage()); 715 } 716 } 717 minimalUrlComposition()718 @Test public void minimalUrlComposition() throws Exception { 719 HttpUrl url = new HttpUrl.Builder().scheme("http").host("host").build(); 720 assertEquals("http://host/", url.toString()); 721 assertEquals("http", url.scheme()); 722 assertEquals("", url.username()); 723 assertEquals("", url.password()); 724 assertEquals("host", url.host()); 725 assertEquals(80, url.port()); 726 assertEquals("/", url.encodedPath()); 727 assertEquals(null, url.query()); 728 assertEquals(null, url.fragment()); 729 } 730 fullUrlComposition()731 @Test public void fullUrlComposition() throws Exception { 732 HttpUrl url = new HttpUrl.Builder() 733 .scheme("http") 734 .username("username") 735 .password("password") 736 .host("host") 737 .port(8080) 738 .addPathSegment("path") 739 .query("query") 740 .fragment("fragment") 741 .build(); 742 assertEquals("http://username:password@host:8080/path?query#fragment", url.toString()); 743 assertEquals("http", url.scheme()); 744 assertEquals("username", url.username()); 745 assertEquals("password", url.password()); 746 assertEquals("host", url.host()); 747 assertEquals(8080, url.port()); 748 assertEquals("/path", url.encodedPath()); 749 assertEquals("query", url.query()); 750 assertEquals("fragment", url.fragment()); 751 } 752 changingSchemeChangesDefaultPort()753 @Test public void changingSchemeChangesDefaultPort() throws Exception { 754 assertEquals(443, HttpUrl.parse("http://example.com") 755 .newBuilder() 756 .scheme("https") 757 .build().port()); 758 759 assertEquals(80, HttpUrl.parse("https://example.com") 760 .newBuilder() 761 .scheme("http") 762 .build().port()); 763 764 assertEquals(1234, HttpUrl.parse("https://example.com:1234") 765 .newBuilder() 766 .scheme("http") 767 .build().port()); 768 } 769 composeEncodesWhitespace()770 @Test public void composeEncodesWhitespace() throws Exception { 771 HttpUrl url = new HttpUrl.Builder() 772 .scheme("http") 773 .username("a\r\n\f\t b") 774 .password("c\r\n\f\t d") 775 .host("host") 776 .addPathSegment("e\r\n\f\t f") 777 .query("g\r\n\f\t h") 778 .fragment("i\r\n\f\t j") 779 .build(); 780 assertEquals("http://a%0D%0A%0C%09%20b:c%0D%0A%0C%09%20d@host" 781 + "/e%0D%0A%0C%09%20f?g%0D%0A%0C%09%20h#i%0D%0A%0C%09 j", url.toString()); 782 assertEquals("a\r\n\f\t b", url.username()); 783 assertEquals("c\r\n\f\t d", url.password()); 784 assertEquals("e\r\n\f\t f", url.pathSegments().get(0)); 785 assertEquals("g\r\n\f\t h", url.query()); 786 assertEquals("i\r\n\f\t j", url.fragment()); 787 } 788 composeFromUnencodedComponents()789 @Test public void composeFromUnencodedComponents() throws Exception { 790 HttpUrl url = new HttpUrl.Builder() 791 .scheme("http") 792 .username("a:\u0001@/\\?#%b") 793 .password("c:\u0001@/\\?#%d") 794 .host("ef") 795 .port(8080) 796 .addPathSegment("g:\u0001@/\\?#%h") 797 .query("i:\u0001@/\\?#%j") 798 .fragment("k:\u0001@/\\?#%l") 799 .build(); 800 assertEquals("http://a%3A%01%40%2F%5C%3F%23%25b:c%3A%01%40%2F%5C%3F%23%25d@ef:8080/" 801 + "g:%01@%2F%5C%3F%23%25h?i:%01@/\\?%23%25j#k:%01@/\\?#%25l", url.toString()); 802 assertEquals("http", url.scheme()); 803 assertEquals("a:\u0001@/\\?#%b", url.username()); 804 assertEquals("c:\u0001@/\\?#%d", url.password()); 805 assertEquals(Arrays.asList("g:\u0001@/\\?#%h"), url.pathSegments()); 806 assertEquals("i:\u0001@/\\?#%j", url.query()); 807 assertEquals("k:\u0001@/\\?#%l", url.fragment()); 808 assertEquals("a%3A%01%40%2F%5C%3F%23%25b", url.encodedUsername()); 809 assertEquals("c%3A%01%40%2F%5C%3F%23%25d", url.encodedPassword()); 810 assertEquals("/g:%01@%2F%5C%3F%23%25h", url.encodedPath()); 811 assertEquals("i:%01@/\\?%23%25j", url.encodedQuery()); 812 assertEquals("k:%01@/\\?#%25l", url.encodedFragment()); 813 } 814 composeFromEncodedComponents()815 @Test public void composeFromEncodedComponents() throws Exception { 816 HttpUrl url = new HttpUrl.Builder() 817 .scheme("http") 818 .encodedUsername("a:\u0001@/\\?#%25b") 819 .encodedPassword("c:\u0001@/\\?#%25d") 820 .host("ef") 821 .port(8080) 822 .addEncodedPathSegment("g:\u0001@/\\?#%25h") 823 .encodedQuery("i:\u0001@/\\?#%25j") 824 .encodedFragment("k:\u0001@/\\?#%25l") 825 .build(); 826 assertEquals("http://a%3A%01%40%2F%5C%3F%23%25b:c%3A%01%40%2F%5C%3F%23%25d@ef:8080/" 827 + "g:%01@%2F%5C%3F%23%25h?i:%01@/\\?%23%25j#k:%01@/\\?#%25l", url.toString()); 828 assertEquals("http", url.scheme()); 829 assertEquals("a:\u0001@/\\?#%b", url.username()); 830 assertEquals("c:\u0001@/\\?#%d", url.password()); 831 assertEquals(Arrays.asList("g:\u0001@/\\?#%h"), url.pathSegments()); 832 assertEquals("i:\u0001@/\\?#%j", url.query()); 833 assertEquals("k:\u0001@/\\?#%l", url.fragment()); 834 assertEquals("a%3A%01%40%2F%5C%3F%23%25b", url.encodedUsername()); 835 assertEquals("c%3A%01%40%2F%5C%3F%23%25d", url.encodedPassword()); 836 assertEquals("/g:%01@%2F%5C%3F%23%25h", url.encodedPath()); 837 assertEquals("i:%01@/\\?%23%25j", url.encodedQuery()); 838 assertEquals("k:%01@/\\?#%25l", url.encodedFragment()); 839 } 840 composeWithEncodedPath()841 @Test public void composeWithEncodedPath() throws Exception { 842 HttpUrl url = new HttpUrl.Builder() 843 .scheme("http") 844 .host("host") 845 .encodedPath("/a%2Fb/c") 846 .build(); 847 assertEquals("http://host/a%2Fb/c", url.toString()); 848 assertEquals("/a%2Fb/c", url.encodedPath()); 849 assertEquals(Arrays.asList("a/b", "c"), url.pathSegments()); 850 } 851 composeMixingPathSegments()852 @Test public void composeMixingPathSegments() throws Exception { 853 HttpUrl url = new HttpUrl.Builder() 854 .scheme("http") 855 .host("host") 856 .encodedPath("/a%2fb/c") 857 .addPathSegment("d%25e") 858 .addEncodedPathSegment("f%25g") 859 .build(); 860 assertEquals("http://host/a%2fb/c/d%2525e/f%25g", url.toString()); 861 assertEquals("/a%2fb/c/d%2525e/f%25g", url.encodedPath()); 862 assertEquals(Arrays.asList("a%2fb", "c", "d%2525e", "f%25g"), url.encodedPathSegments()); 863 assertEquals(Arrays.asList("a/b", "c", "d%25e", "f%g"), url.pathSegments()); 864 } 865 composeWithAddSegment()866 @Test public void composeWithAddSegment() throws Exception { 867 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 868 assertEquals("/a/b/c/", base.newBuilder().addPathSegment("").build().encodedPath()); 869 assertEquals("/a/b/c/d", 870 base.newBuilder().addPathSegment("").addPathSegment("d").build().encodedPath()); 871 assertEquals("/a/b/", base.newBuilder().addPathSegment("..").build().encodedPath()); 872 assertEquals("/a/b/", base.newBuilder().addPathSegment("").addPathSegment("..").build() 873 .encodedPath()); 874 assertEquals("/a/b/c/", base.newBuilder().addPathSegment("").addPathSegment("").build() 875 .encodedPath()); 876 } 877 pathSize()878 @Test public void pathSize() throws Exception { 879 assertEquals(1, HttpUrl.parse("http://host/").pathSize()); 880 assertEquals(3, HttpUrl.parse("http://host/a/b/c").pathSize()); 881 } 882 addPathSegmentDotDoesNothing()883 @Test public void addPathSegmentDotDoesNothing() throws Exception { 884 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 885 assertEquals("/a/b/c", base.newBuilder().addPathSegment(".").build().encodedPath()); 886 } 887 addPathSegmentEncodes()888 @Test public void addPathSegmentEncodes() throws Exception { 889 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 890 assertEquals("/a/b/c/%252e", 891 base.newBuilder().addPathSegment("%2e").build().encodedPath()); 892 assertEquals("/a/b/c/%252e%252e", 893 base.newBuilder().addPathSegment("%2e%2e").build().encodedPath()); 894 } 895 addPathSegmentDotDotPopsDirectory()896 @Test public void addPathSegmentDotDotPopsDirectory() throws Exception { 897 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 898 assertEquals("/a/b/", base.newBuilder().addPathSegment("..").build().encodedPath()); 899 } 900 addPathSegmentDotAndIgnoredCharacter()901 @Test public void addPathSegmentDotAndIgnoredCharacter() throws Exception { 902 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 903 assertEquals("/a/b/c/.%0A", base.newBuilder().addPathSegment(".\n").build().encodedPath()); 904 } 905 addEncodedPathSegmentDotAndIgnoredCharacter()906 @Test public void addEncodedPathSegmentDotAndIgnoredCharacter() throws Exception { 907 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 908 assertEquals("/a/b/c", base.newBuilder().addEncodedPathSegment(".\n").build().encodedPath()); 909 } 910 addEncodedPathSegmentDotDotAndIgnoredCharacter()911 @Test public void addEncodedPathSegmentDotDotAndIgnoredCharacter() throws Exception { 912 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 913 assertEquals("/a/b/", base.newBuilder().addEncodedPathSegment("..\n").build().encodedPath()); 914 } 915 setPathSegment()916 @Test public void setPathSegment() throws Exception { 917 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 918 assertEquals("/d/b/c", base.newBuilder().setPathSegment(0, "d").build().encodedPath()); 919 assertEquals("/a/d/c", base.newBuilder().setPathSegment(1, "d").build().encodedPath()); 920 assertEquals("/a/b/d", base.newBuilder().setPathSegment(2, "d").build().encodedPath()); 921 } 922 setPathSegmentEncodes()923 @Test public void setPathSegmentEncodes() throws Exception { 924 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 925 assertEquals("/%2525/b/c", base.newBuilder().setPathSegment(0, "%25").build().encodedPath()); 926 assertEquals("/.%0A/b/c", base.newBuilder().setPathSegment(0, ".\n").build().encodedPath()); 927 assertEquals("/%252e/b/c", base.newBuilder().setPathSegment(0, "%2e").build().encodedPath()); 928 } 929 setPathSegmentAcceptsEmpty()930 @Test public void setPathSegmentAcceptsEmpty() throws Exception { 931 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 932 assertEquals("//b/c", base.newBuilder().setPathSegment(0, "").build().encodedPath()); 933 assertEquals("/a/b/", base.newBuilder().setPathSegment(2, "").build().encodedPath()); 934 } 935 setPathSegmentRejectsDot()936 @Test public void setPathSegmentRejectsDot() throws Exception { 937 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 938 try { 939 base.newBuilder().setPathSegment(0, "."); 940 fail(); 941 } catch (IllegalArgumentException expected) { 942 } 943 } 944 setPathSegmentRejectsDotDot()945 @Test public void setPathSegmentRejectsDotDot() throws Exception { 946 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 947 try { 948 base.newBuilder().setPathSegment(0, ".."); 949 fail(); 950 } catch (IllegalArgumentException expected) { 951 } 952 } 953 setPathSegmentWithSlash()954 @Test public void setPathSegmentWithSlash() throws Exception { 955 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 956 HttpUrl url = base.newBuilder().setPathSegment(1, "/").build(); 957 assertEquals("/a/%2F/c", url.encodedPath()); 958 } 959 setPathSegmentOutOfBounds()960 @Test public void setPathSegmentOutOfBounds() throws Exception { 961 try { 962 new HttpUrl.Builder().setPathSegment(1, "a"); 963 fail(); 964 } catch (IndexOutOfBoundsException expected) { 965 } 966 } 967 setEncodedPathSegmentEncodes()968 @Test public void setEncodedPathSegmentEncodes() throws Exception { 969 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 970 assertEquals("/%25/b/c", 971 base.newBuilder().setEncodedPathSegment(0, "%25").build().encodedPath()); 972 } 973 setEncodedPathSegmentRejectsDot()974 @Test public void setEncodedPathSegmentRejectsDot() throws Exception { 975 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 976 try { 977 base.newBuilder().setEncodedPathSegment(0, "."); 978 fail(); 979 } catch (IllegalArgumentException expected) { 980 } 981 } 982 setEncodedPathSegmentRejectsDotAndIgnoredCharacter()983 @Test public void setEncodedPathSegmentRejectsDotAndIgnoredCharacter() throws Exception { 984 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 985 try { 986 base.newBuilder().setEncodedPathSegment(0, ".\n"); 987 fail(); 988 } catch (IllegalArgumentException expected) { 989 } 990 } 991 setEncodedPathSegmentRejectsDotDot()992 @Test public void setEncodedPathSegmentRejectsDotDot() throws Exception { 993 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 994 try { 995 base.newBuilder().setEncodedPathSegment(0, ".."); 996 fail(); 997 } catch (IllegalArgumentException expected) { 998 } 999 } 1000 setEncodedPathSegmentRejectsDotDotAndIgnoredCharacter()1001 @Test public void setEncodedPathSegmentRejectsDotDotAndIgnoredCharacter() throws Exception { 1002 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 1003 try { 1004 base.newBuilder().setEncodedPathSegment(0, "..\n"); 1005 fail(); 1006 } catch (IllegalArgumentException expected) { 1007 } 1008 } 1009 setEncodedPathSegmentWithSlash()1010 @Test public void setEncodedPathSegmentWithSlash() throws Exception { 1011 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 1012 HttpUrl url = base.newBuilder().setEncodedPathSegment(1, "/").build(); 1013 assertEquals("/a/%2F/c", url.encodedPath()); 1014 } 1015 setEncodedPathSegmentOutOfBounds()1016 @Test public void setEncodedPathSegmentOutOfBounds() throws Exception { 1017 try { 1018 new HttpUrl.Builder().setEncodedPathSegment(1, "a"); 1019 fail(); 1020 } catch (IndexOutOfBoundsException expected) { 1021 } 1022 } 1023 removePathSegment()1024 @Test public void removePathSegment() throws Exception { 1025 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 1026 HttpUrl url = base.newBuilder() 1027 .removePathSegment(0) 1028 .build(); 1029 assertEquals("/b/c", url.encodedPath()); 1030 } 1031 removePathSegmentDoesntRemovePath()1032 @Test public void removePathSegmentDoesntRemovePath() throws Exception { 1033 HttpUrl base = HttpUrl.parse("http://host/a/b/c"); 1034 HttpUrl url = base.newBuilder() 1035 .removePathSegment(0) 1036 .removePathSegment(0) 1037 .removePathSegment(0) 1038 .build(); 1039 // ANDROID-BEGIN: http://b/29983827 Behavior changed. Test name is now incorrect. 1040 // assertEquals(Arrays.asList(""), url.pathSegments()); 1041 // assertEquals("/", url.encodedPath()); 1042 assertEquals(Collections.emptyList(), url.pathSegments()); 1043 assertEquals("http://host", url.toString()); 1044 // ANDROID-END: http://b/29983827 1045 } 1046 removePathSegmentOutOfBounds()1047 @Test public void removePathSegmentOutOfBounds() throws Exception { 1048 try { 1049 new HttpUrl.Builder().removePathSegment(1); 1050 fail(); 1051 } catch (IndexOutOfBoundsException expected) { 1052 } 1053 } 1054 toJavaNetUrl()1055 @Test public void toJavaNetUrl() throws Exception { 1056 HttpUrl httpUrl = HttpUrl.parse("http://username:password@host/path?query#fragment"); 1057 URL javaNetUrl = httpUrl.url(); 1058 assertEquals("http://username:password@host/path?query#fragment", javaNetUrl.toString()); 1059 } 1060 toUri()1061 @Test public void toUri() throws Exception { 1062 HttpUrl httpUrl = HttpUrl.parse("http://username:password@host/path?query#fragment"); 1063 URI uri = httpUrl.uri(); 1064 assertEquals("http://username:password@host/path?query#fragment", uri.toString()); 1065 } 1066 toUriSpecialQueryCharacters()1067 @Test public void toUriSpecialQueryCharacters() throws Exception { 1068 HttpUrl httpUrl = HttpUrl.parse("http://host/?d=abc!@[]^`{}|\\"); 1069 URI uri = httpUrl.uri(); 1070 assertEquals("http://host/?d=abc!@[]%5E%60%7B%7D%7C%5C", uri.toString()); 1071 } 1072 toUriWithUsernameNoPassword()1073 @Test public void toUriWithUsernameNoPassword() throws Exception { 1074 HttpUrl httpUrl = new HttpUrl.Builder() 1075 .scheme("http") 1076 .username("user") 1077 .host("host") 1078 .build(); 1079 assertEquals("http://user@host/", httpUrl.toString()); 1080 assertEquals("http://user@host/", httpUrl.uri().toString()); 1081 } 1082 toUriUsernameSpecialCharacters()1083 @Test public void toUriUsernameSpecialCharacters() throws Exception { 1084 HttpUrl url = new HttpUrl.Builder() 1085 .scheme("http") 1086 .host("host") 1087 .username("=[]:;\"~|?#@^/$%*") 1088 .build(); 1089 assertEquals("http://%3D%5B%5D%3A%3B%22~%7C%3F%23%40%5E%2F$%25*@host/", url.toString()); 1090 assertEquals("http://%3D%5B%5D%3A%3B%22~%7C%3F%23%40%5E%2F$%25*@host/", url.uri().toString()); 1091 } 1092 toUriPasswordSpecialCharacters()1093 @Test public void toUriPasswordSpecialCharacters() throws Exception { 1094 HttpUrl url = new HttpUrl.Builder() 1095 .scheme("http") 1096 .host("host") 1097 .username("user") 1098 .password("=[]:;\"~|?#@^/$%*") 1099 .build(); 1100 assertEquals("http://user:%3D%5B%5D%3A%3B%22~%7C%3F%23%40%5E%2F$%25*@host/", url.toString()); 1101 assertEquals("http://user:%3D%5B%5D%3A%3B%22~%7C%3F%23%40%5E%2F$%25*@host/", 1102 url.uri().toString()); 1103 } 1104 toUriPathSpecialCharacters()1105 @Test public void toUriPathSpecialCharacters() throws Exception { 1106 HttpUrl url = new HttpUrl.Builder() 1107 .scheme("http") 1108 .host("host") 1109 .addPathSegment("=[]:;\"~|?#@^/$%*") 1110 .build(); 1111 assertEquals("http://host/=[]:;%22~%7C%3F%23@%5E%2F$%25*", url.toString()); 1112 assertEquals("http://host/=%5B%5D:;%22~%7C%3F%23@%5E%2F$%25*", url.uri().toString()); 1113 } 1114 toUriQueryParameterNameSpecialCharacters()1115 @Test public void toUriQueryParameterNameSpecialCharacters() throws Exception { 1116 HttpUrl url = new HttpUrl.Builder() 1117 .scheme("http") 1118 .host("host") 1119 .addQueryParameter("=[]:;\"~|?#@^/$%*", "a") 1120 .build(); 1121 assertEquals("http://host/?%3D[]:;%22~|?%23@^/$%25*=a", url.toString()); 1122 assertEquals("http://host/?%3D[]:;%22~%7C?%23@%5E/$%25*=a", url.uri().toString()); 1123 } 1124 toUriQueryParameterValueSpecialCharacters()1125 @Test public void toUriQueryParameterValueSpecialCharacters() throws Exception { 1126 HttpUrl url = new HttpUrl.Builder() 1127 .scheme("http") 1128 .host("host") 1129 .addQueryParameter("a", "=[]:;\"~|?#@^/$%*") 1130 .build(); 1131 assertEquals("http://host/?a=%3D[]:;%22~|?%23@^/$%25*", url.toString()); 1132 assertEquals("http://host/?a=%3D[]:;%22~%7C?%23@%5E/$%25*", url.uri().toString()); 1133 } 1134 toUriQueryValueSpecialCharacters()1135 @Test public void toUriQueryValueSpecialCharacters() throws Exception { 1136 HttpUrl url = new HttpUrl.Builder() 1137 .scheme("http") 1138 .host("host") 1139 .query("=[]:;\"~|?#@^/$%*") 1140 .build(); 1141 assertEquals("http://host/?=[]:;%22~|?%23@^/$%25*", url.toString()); 1142 assertEquals("http://host/?=[]:;%22~%7C?%23@%5E/$%25*", url.uri().toString()); 1143 } 1144 toUriFragmentSpecialCharacters()1145 @Test public void toUriFragmentSpecialCharacters() throws Exception { 1146 HttpUrl url = new HttpUrl.Builder() 1147 .scheme("http") 1148 .host("host") 1149 .fragment("=[]:;\"~|?#@^/$%*") 1150 .build(); 1151 assertEquals("http://host/#=[]:;\"~|?#@^/$%25*", url.toString()); 1152 assertEquals("http://host/#=[]:;%22~%7C?%23@%5E/$%25*", url.uri().toString()); 1153 } 1154 toUriWithControlCharacters()1155 @Test public void toUriWithControlCharacters() throws Exception { 1156 // Percent-encoded in the path. 1157 assertEquals(new URI("http://host/a%00b"), HttpUrl.parse("http://host/a\u0000b").uri()); 1158 assertEquals(new URI("http://host/a%C2%80b"), HttpUrl.parse("http://host/a\u0080b").uri()); 1159 assertEquals(new URI("http://host/a%C2%9Fb"), HttpUrl.parse("http://host/a\u009fb").uri()); 1160 // Percent-encoded in the query. 1161 assertEquals(new URI("http://host/?a%00b"), HttpUrl.parse("http://host/?a\u0000b").uri()); 1162 assertEquals(new URI("http://host/?a%C2%80b"), HttpUrl.parse("http://host/?a\u0080b").uri()); 1163 assertEquals(new URI("http://host/?a%C2%9Fb"), HttpUrl.parse("http://host/?a\u009fb").uri()); 1164 // Stripped from the fragment. 1165 assertEquals(new URI("http://host/#a%00b"), HttpUrl.parse("http://host/#a\u0000b").uri()); 1166 assertEquals(new URI("http://host/#ab"), HttpUrl.parse("http://host/#a\u0080b").uri()); 1167 assertEquals(new URI("http://host/#ab"), HttpUrl.parse("http://host/#a\u009fb").uri()); 1168 } 1169 toUriWithSpaceCharacters()1170 @Test public void toUriWithSpaceCharacters() throws Exception { 1171 // Percent-encoded in the path. 1172 assertEquals(new URI("http://host/a%0Bb"), HttpUrl.parse("http://host/a\u000bb").uri()); 1173 assertEquals(new URI("http://host/a%20b"), HttpUrl.parse("http://host/a b").uri()); 1174 assertEquals(new URI("http://host/a%E2%80%89b"), HttpUrl.parse("http://host/a\u2009b").uri()); 1175 assertEquals(new URI("http://host/a%E3%80%80b"), HttpUrl.parse("http://host/a\u3000b").uri()); 1176 // Percent-encoded in the query. 1177 assertEquals(new URI("http://host/?a%0Bb"), HttpUrl.parse("http://host/?a\u000bb").uri()); 1178 assertEquals(new URI("http://host/?a%20b"), HttpUrl.parse("http://host/?a b").uri()); 1179 assertEquals(new URI("http://host/?a%E2%80%89b"), HttpUrl.parse("http://host/?a\u2009b").uri()); 1180 assertEquals(new URI("http://host/?a%E3%80%80b"), HttpUrl.parse("http://host/?a\u3000b").uri()); 1181 // Stripped from the fragment. 1182 assertEquals(new URI("http://host/#a%0Bb"), HttpUrl.parse("http://host/#a\u000bb").uri()); 1183 assertEquals(new URI("http://host/#a%20b"), HttpUrl.parse("http://host/#a b").uri()); 1184 assertEquals(new URI("http://host/#ab"), HttpUrl.parse("http://host/#a\u2009b").uri()); 1185 assertEquals(new URI("http://host/#ab"), HttpUrl.parse("http://host/#a\u3000b").uri()); 1186 } 1187 toUriWithNonHexPercentEscape()1188 @Test public void toUriWithNonHexPercentEscape() throws Exception { 1189 assertEquals(new URI("http://host/%25xx"), HttpUrl.parse("http://host/%xx").uri()); 1190 } 1191 toUriWithTruncatedPercentEscape()1192 @Test public void toUriWithTruncatedPercentEscape() throws Exception { 1193 assertEquals(new URI("http://host/%25a"), HttpUrl.parse("http://host/%a").uri()); 1194 assertEquals(new URI("http://host/%25"), HttpUrl.parse("http://host/%").uri()); 1195 } 1196 fromJavaNetUrl()1197 @Test public void fromJavaNetUrl() throws Exception { 1198 URL javaNetUrl = new URL("http://username:password@host/path?query#fragment"); 1199 HttpUrl httpUrl = HttpUrl.get(javaNetUrl); 1200 assertEquals("http://username:password@host/path?query#fragment", httpUrl.toString()); 1201 } 1202 1203 // ANDROID-BEGIN 1204 @Ignore // Android's URL implementation does not support mailto: 1205 // ANDROID-END fromJavaNetUrlUnsupportedScheme()1206 @Test public void fromJavaNetUrlUnsupportedScheme() throws Exception { 1207 URL javaNetUrl = new URL("mailto:user@example.com"); 1208 assertEquals(null, HttpUrl.get(javaNetUrl)); 1209 } 1210 fromUri()1211 @Test public void fromUri() throws Exception { 1212 URI uri = new URI("http://username:password@host/path?query#fragment"); 1213 HttpUrl httpUrl = HttpUrl.get(uri); 1214 assertEquals("http://username:password@host/path?query#fragment", httpUrl.toString()); 1215 } 1216 fromUriUnsupportedScheme()1217 @Test public void fromUriUnsupportedScheme() throws Exception { 1218 URI uri = new URI("mailto:user@example.com"); 1219 assertEquals(null, HttpUrl.get(uri)); 1220 } 1221 fromUriPartial()1222 @Test public void fromUriPartial() throws Exception { 1223 URI uri = new URI("/path"); 1224 assertEquals(null, HttpUrl.get(uri)); 1225 } 1226 fromJavaNetUrl_checked()1227 @Test public void fromJavaNetUrl_checked() throws Exception { 1228 HttpUrl httpUrl = HttpUrl.getChecked("http://username:password@host/path?query#fragment"); 1229 assertEquals("http://username:password@host/path?query#fragment", httpUrl.toString()); 1230 } 1231 fromJavaNetUrlUnsupportedScheme_checked()1232 @Test public void fromJavaNetUrlUnsupportedScheme_checked() throws Exception { 1233 try { 1234 HttpUrl.getChecked("mailto:user@example.com"); 1235 fail(); 1236 } catch (MalformedURLException e) { 1237 } 1238 } 1239 fromJavaNetUrlBadHost_checked()1240 @Test public void fromJavaNetUrlBadHost_checked() throws Exception { 1241 try { 1242 HttpUrl.getChecked("http://hostw ithspace/"); 1243 fail(); 1244 } catch (UnknownHostException expected) { 1245 } 1246 } 1247 composeQueryWithComponents()1248 @Test public void composeQueryWithComponents() throws Exception { 1249 HttpUrl base = HttpUrl.parse("http://host/"); 1250 HttpUrl url = base.newBuilder().addQueryParameter("a+=& b", "c+=& d").build(); 1251 assertEquals("http://host/?a%2B%3D%26%20b=c%2B%3D%26%20d", url.toString()); 1252 assertEquals("c+=& d", url.queryParameterValue(0)); 1253 assertEquals("a+=& b", url.queryParameterName(0)); 1254 assertEquals("c+=& d", url.queryParameter("a+=& b")); 1255 assertEquals(Collections.singleton("a+=& b"), url.queryParameterNames()); 1256 assertEquals(singletonList("c+=& d"), url.queryParameterValues("a+=& b")); 1257 assertEquals(1, url.querySize()); 1258 assertEquals("a+=& b=c+=& d", url.query()); // Ambiguous! (Though working as designed.) 1259 assertEquals("a%2B%3D%26%20b=c%2B%3D%26%20d", url.encodedQuery()); 1260 } 1261 composeQueryWithEncodedComponents()1262 @Test public void composeQueryWithEncodedComponents() throws Exception { 1263 HttpUrl base = HttpUrl.parse("http://host/"); 1264 HttpUrl url = base.newBuilder().addEncodedQueryParameter("a+=& b", "c+=& d").build(); 1265 assertEquals("http://host/?a+%3D%26%20b=c+%3D%26%20d", url.toString()); 1266 assertEquals("c =& d", url.queryParameter("a =& b")); 1267 } 1268 composeQueryRemoveQueryParameter()1269 @Test public void composeQueryRemoveQueryParameter() throws Exception { 1270 HttpUrl url = HttpUrl.parse("http://host/").newBuilder() 1271 .addQueryParameter("a+=& b", "c+=& d") 1272 .removeAllQueryParameters("a+=& b") 1273 .build(); 1274 assertEquals("http://host/", url.toString()); 1275 assertEquals(null, url.queryParameter("a+=& b")); 1276 } 1277 composeQueryRemoveEncodedQueryParameter()1278 @Test public void composeQueryRemoveEncodedQueryParameter() throws Exception { 1279 HttpUrl url = HttpUrl.parse("http://host/").newBuilder() 1280 .addEncodedQueryParameter("a+=& b", "c+=& d") 1281 .removeAllEncodedQueryParameters("a+=& b") 1282 .build(); 1283 assertEquals("http://host/", url.toString()); 1284 assertEquals(null, url.queryParameter("a =& b")); 1285 } 1286 composeQuerySetQueryParameter()1287 @Test public void composeQuerySetQueryParameter() throws Exception { 1288 HttpUrl url = HttpUrl.parse("http://host/").newBuilder() 1289 .addQueryParameter("a+=& b", "c+=& d") 1290 .setQueryParameter("a+=& b", "ef") 1291 .build(); 1292 assertEquals("http://host/?a%2B%3D%26%20b=ef", url.toString()); 1293 assertEquals("ef", url.queryParameter("a+=& b")); 1294 } 1295 composeQuerySetEncodedQueryParameter()1296 @Test public void composeQuerySetEncodedQueryParameter() throws Exception { 1297 HttpUrl url = HttpUrl.parse("http://host/").newBuilder() 1298 .addEncodedQueryParameter("a+=& b", "c+=& d") 1299 .setEncodedQueryParameter("a+=& b", "ef") 1300 .build(); 1301 assertEquals("http://host/?a+%3D%26%20b=ef", url.toString()); 1302 assertEquals("ef", url.queryParameter("a =& b")); 1303 } 1304 composeQueryMultipleEncodedValuesForParameter()1305 @Test public void composeQueryMultipleEncodedValuesForParameter() throws Exception { 1306 HttpUrl url = HttpUrl.parse("http://host/").newBuilder() 1307 .addQueryParameter("a+=& b", "c+=& d") 1308 .addQueryParameter("a+=& b", "e+=& f") 1309 .build(); 1310 assertEquals("http://host/?a%2B%3D%26%20b=c%2B%3D%26%20d&a%2B%3D%26%20b=e%2B%3D%26%20f", 1311 url.toString()); 1312 assertEquals(2, url.querySize()); 1313 assertEquals(Collections.singleton("a+=& b"), url.queryParameterNames()); 1314 assertEquals(Arrays.asList("c+=& d", "e+=& f"), url.queryParameterValues("a+=& b")); 1315 } 1316 absentQueryIsZeroNameValuePairs()1317 @Test public void absentQueryIsZeroNameValuePairs() throws Exception { 1318 HttpUrl url = HttpUrl.parse("http://host/").newBuilder() 1319 .query(null) 1320 .build(); 1321 assertEquals(0, url.querySize()); 1322 } 1323 emptyQueryIsSingleNameValuePairWithEmptyKey()1324 @Test public void emptyQueryIsSingleNameValuePairWithEmptyKey() throws Exception { 1325 HttpUrl url = HttpUrl.parse("http://host/").newBuilder() 1326 .query("") 1327 .build(); 1328 assertEquals(1, url.querySize()); 1329 assertEquals("", url.queryParameterName(0)); 1330 assertEquals(null, url.queryParameterValue(0)); 1331 } 1332 ampersandQueryIsTwoNameValuePairsWithEmptyKeys()1333 @Test public void ampersandQueryIsTwoNameValuePairsWithEmptyKeys() throws Exception { 1334 HttpUrl url = HttpUrl.parse("http://host/").newBuilder() 1335 .query("&") 1336 .build(); 1337 assertEquals(2, url.querySize()); 1338 assertEquals("", url.queryParameterName(0)); 1339 assertEquals(null, url.queryParameterValue(0)); 1340 assertEquals("", url.queryParameterName(1)); 1341 assertEquals(null, url.queryParameterValue(1)); 1342 } 1343 removeAllDoesNotRemoveQueryIfNoParametersWereRemoved()1344 @Test public void removeAllDoesNotRemoveQueryIfNoParametersWereRemoved() throws Exception { 1345 HttpUrl url = HttpUrl.parse("http://host/").newBuilder() 1346 .query("") 1347 .removeAllQueryParameters("a") 1348 .build(); 1349 assertEquals("http://host/?", url.toString()); 1350 } 1351 queryParametersWithoutValues()1352 @Test public void queryParametersWithoutValues() throws Exception { 1353 HttpUrl url = HttpUrl.parse("http://host/?foo&bar&baz"); 1354 assertEquals(3, url.querySize()); 1355 assertEquals(new LinkedHashSet<>(Arrays.asList("foo", "bar", "baz")), 1356 url.queryParameterNames()); 1357 assertEquals(null, url.queryParameterValue(0)); 1358 assertEquals(null, url.queryParameterValue(1)); 1359 assertEquals(null, url.queryParameterValue(2)); 1360 assertEquals(singletonList((String) null), url.queryParameterValues("foo")); 1361 assertEquals(singletonList((String) null), url.queryParameterValues("bar")); 1362 assertEquals(singletonList((String) null), url.queryParameterValues("baz")); 1363 } 1364 queryParametersWithEmptyValues()1365 @Test public void queryParametersWithEmptyValues() throws Exception { 1366 HttpUrl url = HttpUrl.parse("http://host/?foo=&bar=&baz="); 1367 assertEquals(3, url.querySize()); 1368 assertEquals(new LinkedHashSet<>(Arrays.asList("foo", "bar", "baz")), 1369 url.queryParameterNames()); 1370 assertEquals("", url.queryParameterValue(0)); 1371 assertEquals("", url.queryParameterValue(1)); 1372 assertEquals("", url.queryParameterValue(2)); 1373 assertEquals(singletonList(""), url.queryParameterValues("foo")); 1374 assertEquals(singletonList(""), url.queryParameterValues("bar")); 1375 assertEquals(singletonList(""), url.queryParameterValues("baz")); 1376 } 1377 queryParametersWithRepeatedName()1378 @Test public void queryParametersWithRepeatedName() throws Exception { 1379 HttpUrl url = HttpUrl.parse("http://host/?foo[]=1&foo[]=2&foo[]=3"); 1380 assertEquals(3, url.querySize()); 1381 assertEquals(Collections.singleton("foo[]"), url.queryParameterNames()); 1382 assertEquals("1", url.queryParameterValue(0)); 1383 assertEquals("2", url.queryParameterValue(1)); 1384 assertEquals("3", url.queryParameterValue(2)); 1385 assertEquals(Arrays.asList("1", "2", "3"), url.queryParameterValues("foo[]")); 1386 } 1387 queryParameterLookupWithNonCanonicalEncoding()1388 @Test public void queryParameterLookupWithNonCanonicalEncoding() throws Exception { 1389 HttpUrl url = HttpUrl.parse("http://host/?%6d=m&+=%20"); 1390 assertEquals("m", url.queryParameterName(0)); 1391 assertEquals(" ", url.queryParameterName(1)); 1392 assertEquals("m", url.queryParameter("m")); 1393 assertEquals(" ", url.queryParameter(" ")); 1394 } 1395 roundTripBuilder()1396 @Test public void roundTripBuilder() throws Exception { 1397 HttpUrl url = new HttpUrl.Builder() 1398 .scheme("http") 1399 .username("%") 1400 .password("%") 1401 .host("host") 1402 .addPathSegment("%") 1403 .query("%") 1404 .fragment("%") 1405 .build(); 1406 assertEquals("http://%25:%25@host/%25?%25#%25", url.toString()); 1407 assertEquals("http://%25:%25@host/%25?%25#%25", url.newBuilder().build().toString()); 1408 assertEquals("http://%25:%25@host/%25?%25", url.resolve("").toString()); 1409 } 1410 1411 /** 1412 * Although HttpUrl prefers percent-encodings in uppercase, it should preserve the exact 1413 * structure of the original encoding. 1414 */ rawEncodingRetained()1415 @Test public void rawEncodingRetained() throws Exception { 1416 String urlString = "http://%6d%6D:%6d%6D@host/%6d%6D?%6d%6D#%6d%6D"; 1417 HttpUrl url = HttpUrl.parse(urlString); 1418 assertEquals("%6d%6D", url.encodedUsername()); 1419 assertEquals("%6d%6D", url.encodedPassword()); 1420 assertEquals("/%6d%6D", url.encodedPath()); 1421 assertEquals(Arrays.asList("%6d%6D"), url.encodedPathSegments()); 1422 assertEquals("%6d%6D", url.encodedQuery()); 1423 assertEquals("%6d%6D", url.encodedFragment()); 1424 assertEquals(urlString, url.toString()); 1425 assertEquals(urlString, url.newBuilder().build().toString()); 1426 assertEquals("http://%6d%6D:%6d%6D@host/%6d%6D?%6d%6D", url.resolve("").toString()); 1427 } 1428 clearFragment()1429 @Test public void clearFragment() throws Exception { 1430 HttpUrl url = HttpUrl.parse("http://host/#fragment") 1431 .newBuilder() 1432 .fragment(null) 1433 .build(); 1434 assertEquals("http://host/", url.toString()); 1435 assertEquals(null, url.fragment()); 1436 assertEquals(null, url.encodedFragment()); 1437 } 1438 clearEncodedFragment()1439 @Test public void clearEncodedFragment() throws Exception { 1440 HttpUrl url = HttpUrl.parse("http://host/#fragment") 1441 .newBuilder() 1442 .encodedFragment(null) 1443 .build(); 1444 assertEquals("http://host/", url.toString()); 1445 assertEquals(null, url.fragment()); 1446 assertEquals(null, url.encodedFragment()); 1447 } 1448 } 1449