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.internal.Util; 19 import java.io.IOException; 20 import java.util.ArrayList; 21 import java.util.List; 22 import okio.BufferedSource; 23 import okio.Okio; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.Parameterized; 27 import org.junit.runners.Parameterized.Parameter; 28 29 import static org.junit.Assert.assertEquals; 30 import static org.junit.Assert.assertNotNull; 31 import static org.junit.Assert.assertNull; 32 33 /** Runs the web platform URL tests against Java URL models. */ 34 @RunWith(Parameterized.class) 35 public final class WebPlatformUrlTest { 36 @Parameterized.Parameters(name = "{0}") parameters()37 public static List<Object[]> parameters() { 38 try { 39 List<Object[]> result = new ArrayList<>(); 40 for (WebPlatformUrlTestData urlTestData : loadTests()) { 41 result.add(new Object[] { urlTestData }); 42 } 43 return result; 44 } catch (IOException e) { 45 throw new AssertionError(); 46 } 47 } 48 49 @Parameter(0) 50 public WebPlatformUrlTestData testData; 51 52 private static final List<String> HTTP_URL_SCHEMES 53 = Util.immutableList("http", "https"); 54 private static final List<String> KNOWN_FAILURES = Util.immutableList( 55 "Parsing: <http://example\t.\norg> against <http://example.org/foo/bar>", 56 "Parsing: <http://f:0/c> against <http://example.org/foo/bar>", 57 "Parsing: <http://f:00000000000000/c> against <http://example.org/foo/bar>", 58 "Parsing: <http://f:\n/c> against <http://example.org/foo/bar>", 59 "Parsing: <http://f:999999/c> against <http://example.org/foo/bar>", 60 "Parsing: <http://192.0x00A80001> against <about:blank>", 61 // This test fails on Java 7 but passes on Java 8. See HttpUrlTest.hostWithTrailingDot(). 62 "Parsing: <http://%30%78%63%30%2e%30%32%35%30.01%2e> against <http://other.com/>", 63 "Parsing: <http://%30%78%63%30%2e%30%32%35%30.01> against <http://other.com/>", 64 "Parsing: <http://192.168.0.257> against <http://other.com/>", 65 "Parsing: <http://0Xc0.0250.01> against <http://other.com/>" 66 ); 67 68 /** Test how {@link HttpUrl} does against the web platform test suite. */ httpUrl()69 @Test public void httpUrl() throws Exception { 70 if (!testData.scheme.isEmpty() && !HTTP_URL_SCHEMES.contains(testData.scheme)) { 71 System.err.println("Ignoring unsupported scheme " + testData.scheme); 72 return; 73 } 74 if (!testData.base.startsWith("https:") 75 && !testData.base.startsWith("http:") 76 && !testData.base.equals("about:blank")) { 77 System.err.println("Ignoring unsupported base " + testData.base); 78 return; 79 } 80 81 try { 82 testHttpUrl(); 83 if (KNOWN_FAILURES.contains(testData.toString())) { 84 System.err.println("Expected failure but was success: " + testData); 85 } 86 } catch (Throwable e) { 87 if (KNOWN_FAILURES.contains(testData.toString())) { 88 System.err.println("Ignoring known failure: " + testData); 89 e.printStackTrace(); 90 } else { 91 throw e; 92 } 93 } 94 } 95 testHttpUrl()96 private void testHttpUrl() { 97 HttpUrl url; 98 if (testData.base.equals("about:blank")) { 99 url = HttpUrl.parse(testData.input); 100 } else { 101 HttpUrl baseUrl = HttpUrl.parse(testData.base); 102 url = baseUrl.resolve(testData.input); 103 } 104 105 if (testData.expectParseFailure()) { 106 assertNull("Expected URL to fail parsing", url); 107 } else { 108 assertNotNull("Expected URL to parse successfully, but was null", url); 109 String effectivePort = url.port() != HttpUrl.defaultPort(url.scheme()) 110 ? Integer.toString(url.port()) 111 : ""; 112 String effectiveQuery = url.encodedQuery() != null ? "?" + url.encodedQuery() : ""; 113 String effectiveFragment = url.encodedFragment() != null ? "#" + url.encodedFragment() : ""; 114 String effectiveHost = url.host().contains(":") 115 ? ("[" + url.host() + "]") 116 : url.host(); 117 assertEquals("scheme", testData.scheme, url.scheme()); 118 assertEquals("host", testData.host, effectiveHost); 119 assertEquals("port", testData.port, effectivePort); 120 assertEquals("path", testData.path, url.encodedPath()); 121 assertEquals("query", testData.query, effectiveQuery); 122 assertEquals("fragment", testData.fragment, effectiveFragment); 123 } 124 } 125 loadTests()126 private static List<WebPlatformUrlTestData> loadTests() throws IOException { 127 BufferedSource source = Okio.buffer(Okio.source( 128 WebPlatformUrlTest.class.getResourceAsStream("/web-platform-test-urltestdata.txt"))); 129 return WebPlatformUrlTestData.load(source); 130 } 131 } 132