1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-17 08:42:25 +03:00

Cleanup some HttpUrl tests.

We don't need to compare our results against major browsers anymore;
we're passing more than they are.

The list of failing tests is still too large, but should be easy to
churn through.
This commit is contained in:
jwilson
2015-05-09 00:23:29 -04:00
parent abbc724a97
commit 68c41f169a
6 changed files with 74 additions and 4204 deletions

View File

@@ -226,6 +226,39 @@ public final class HttpUrlTest {
assertEquals(null, HttpUrl.parse("http://%20/"));
}
@Test public void hostnameLowercaseCharactersMappedDirectly() throws Exception {
assertEquals("abcd", HttpUrl.parse("http://abcd").host());
assertEquals("xn--4xa", HttpUrl.parse("http://σ").host());
}
@Test public void hostnameUppercaseCharactersConvertedToLowercase() throws Exception {
assertEquals("abcd", HttpUrl.parse("http://ABCD").host());
assertEquals("xn--4xa", HttpUrl.parse("http://Σ").host());
}
@Test public void hostnameIgnoredCharacters() throws Exception {
// The soft hyphen (­) should be ignored.
assertEquals("abcd", HttpUrl.parse("http://AB\u00adCD").host());
}
@Test public void hostnameMultipleCharacterMapping() throws Exception {
// Map the single character telephone symbol (℡) to the string "tel".
assertEquals("tel", HttpUrl.parse("http://\u2121").host());
}
@Test public void hostnameMappingLastMappedCodePoint() throws Exception {
assertEquals("xn--pu5l", HttpUrl.parse("http://\uD87E\uDE1D").host());
}
@Ignore // The java.net.IDN implementation doesn't ignore characters that it should.
@Test public void hostnameMappingLastIgnoredCodePoint() throws Exception {
assertEquals("abcd", HttpUrl.parse("http://ab\uDB40\uDDEFcd").host());
}
@Test public void hostnameMappingLastDisallowedCodePoint() throws Exception {
assertEquals(null, HttpUrl.parse("http://\uDBFF\uDFFF"));
}
@Test public void hostIpv6() throws Exception {
// Square braces are absent from host()...
String address = "0:0:0:0:0:0:0:1";
@@ -275,7 +308,7 @@ public final class HttpUrlTest {
assertEquals(a2, HttpUrl.parse("http://[1::]").host());
}
@Ignore
@Ignore // java.net.InetAddress isn't as strict as it should be.
@Test public void hostIpv6AddressTooManyDigitsInGroup() throws Exception {
assertEquals(null, HttpUrl.parse("http://[00000:0000:0000:0000:0000:0000:0000:0001]"));
assertEquals(null, HttpUrl.parse("http://[::00001]"));

View File

@@ -1,77 +0,0 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.okhttp;
import com.google.gson.Gson;
import com.squareup.okhttp.internal.Util;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
/**
* The result of a test run from the <a href="https://github.com/w3c/web-platform-tests">W3C web
* platform tests</a>. This class serves as a Gson model for browser test results.
*
* <p><strong>Note:</strong> When extracting the .json file from the browser after a test run, be
* careful to avoid text encoding problems. In one environment, Safari was corrupting UTF-8 data
* for download (but the clipboard was fine), and Firefox was corrupting UTF-8 data copied to the
* clipboard (but the download was fine).
*/
public final class WebPlatformTestRun {
List<TestResult> results;
public SubtestResult get(String testName, String subtestName) {
for (TestResult result : results) {
if (testName.equals(result.test)) {
for (SubtestResult subtestResult : result.subtests) {
if (subtestName.equals(subtestResult.name)) {
return subtestResult;
}
}
}
}
return null;
}
public static WebPlatformTestRun load(InputStream in) throws IOException {
try {
return new Gson().getAdapter(WebPlatformTestRun.class)
.fromJson(new InputStreamReader(in, Util.UTF_8));
} finally {
Util.closeQuietly(in);
}
}
public static class TestResult {
String test;
List<SubtestResult> subtests;
}
public static class SubtestResult {
String name;
Status status;
String message;
public boolean isPass() {
return status == Status.PASS;
}
}
public enum Status {
PASS, FAIL
}
}

View File

@@ -15,16 +15,12 @@
*/
package com.squareup.okhttp;
import com.squareup.okhttp.WebPlatformTestRun.SubtestResult;
import com.squareup.okhttp.internal.Util;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import okio.BufferedSource;
import okio.Okio;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -40,25 +36,9 @@ public final class WebPlatformUrlTest {
@Parameterized.Parameters(name = "{0}")
public static List<Object[]> parameters() {
try {
List<WebPlatformUrlTestData> tests = loadTests();
// The web platform tests are run in both HTML and XHTML variants. Major browsers pass more
// tests in HTML mode, so that's what we'll attempt to match.
String testName = "/url/a-element.html";
WebPlatformTestRun firefoxTestRun
= loadTestRun("/web-platform-test-results-url-firefox-37.0.json");
WebPlatformTestRun chromeTestRun
= loadTestRun("/web-platform-test-results-url-chrome-42.0.json");
WebPlatformTestRun safariTestRun
= loadTestRun("/web-platform-test-results-url-safari-7.1.json");
List<Object[]> result = new ArrayList<>();
for (WebPlatformUrlTestData urlTestData : tests) {
String subtestName = urlTestData.toString();
SubtestResult firefoxResult = firefoxTestRun.get(testName, subtestName);
SubtestResult chromeResult = chromeTestRun.get(testName, subtestName);
SubtestResult safariResult = safariTestRun.get(testName, subtestName);
result.add(new Object[] { urlTestData, firefoxResult, chromeResult, safariResult });
for (WebPlatformUrlTestData urlTestData : loadTests()) {
result.add(new Object[] { urlTestData });
}
return result;
} catch (IOException e) {
@@ -69,89 +49,60 @@ public final class WebPlatformUrlTest {
@Parameter(0)
public WebPlatformUrlTestData testData;
@Parameter(1)
public SubtestResult firefoxResult;
@Parameter(2)
public SubtestResult chromeResultResult;
@Parameter(3)
public SubtestResult safariResult;
private static final List<String> JAVA_NET_URL_SCHEMES
= Util.immutableList("file", "ftp", "http", "https", "mailto");
private static final List<String> HTTP_URL_SCHEMES
= Util.immutableList("http", "https");
/** Test how {@link URL} does against the web platform test suite. */
@Ignore // java.net.URL is broken. Not much we can do about that.
@Test public void javaNetUrl() throws Exception {
if (!testData.scheme.isEmpty() && !JAVA_NET_URL_SCHEMES.contains(testData.scheme)) {
System.out.println("Ignoring unsupported scheme " + testData.scheme);
return;
}
try {
testJavaNetUrl();
} catch (AssertionError e) {
if (tolerateFailure()) {
System.out.println("Tolerable failure: " + e.getMessage());
return;
}
throw e;
}
}
private void testJavaNetUrl() {
URL url = null;
String failureMessage = "";
try {
if (testData.base.equals("about:blank")) {
url = new URL(testData.input);
} else {
URL baseUrl = new URL(testData.base);
url = new URL(baseUrl, testData.input);
}
} catch (MalformedURLException e) {
failureMessage = e.getMessage();
}
if (testData.expectParseFailure()) {
assertNull("Expected URL to fail parsing", url);
} else {
assertNotNull("Expected URL to parse successfully, but was " + failureMessage, url);
String effectivePort = url.getPort() != -1 ? Integer.toString(url.getPort()) : "";
String effectiveQuery = url.getQuery() != null ? "?" + url.getQuery() : "";
String effectiveFragment = url.getRef() != null ? "#" + url.getRef() : "";
assertEquals("scheme", testData.scheme, url.getProtocol());
assertEquals("host", testData.host, url.getHost());
assertEquals("port", testData.port, effectivePort);
assertEquals("path", testData.path, url.getPath());
assertEquals("query", testData.query, effectiveQuery);
assertEquals("fragment", testData.fragment, effectiveFragment);
}
}
private static final List<String> KNOWN_FAILURES = Util.immutableList(
"Parsing: <http://example\t.\norg> against <http://example.org/foo/bar>",
"Parsing: <http://f:0/c> against <http://example.org/foo/bar>",
"Parsing: <http://f:00000000000000/c> against <http://example.org/foo/bar>",
"Parsing: <http://f:\n/c> against <http://example.org/foo/bar>",
"Parsing: <http://f:999999/c> against <http://example.org/foo/bar>",
"Parsing: <#β> against <http://example.org/foo/bar>",
"Parsing: <http://www.google.com/foo?bar=baz# »> against <about:blank>",
"Parsing: <http://[www.google.com]/> against <about:blank>",
"Parsing: <http://192.0x00A80001> against <about:blank>",
"Parsing: <http://user:pass@/> against <about:blank>",
"Parsing: <http:/:@/www.example.com> against <about:blank>",
"Parsing: <http://%30%78%63%30%2e%30%32%35%30.01%2e> against <http://other.com/>",
"Parsing: <http://user@/www.example.com> against <about:blank>",
"Parsing: <http:@/www.example.com> against <about:blank>",
"Parsing: <http:/@/www.example.com> against <about:blank>",
"Parsing: <http://@/www.example.com> against <about:blank>",
"Parsing: <https:@/www.example.com> against <about:blank>",
"Parsing: <http:a:b@/www.example.com> against <about:blank>",
"Parsing: <http:/a:b@/www.example.com> against <about:blank>",
"Parsing: <http://a:b@/www.example.com> against <about:blank>",
"Parsing: <http::@/www.example.com> against <about:blank>",
"Parsing: <http://%30%78%63%30%2e%30%32%35%30.01> against <http://other.com/>",
"Parsing: <http://192.168.0.257> against <http://other.com/>",
"Parsing: <http://> against <http://other.com/>",
"Parsing: <http://[2001::1]> against <http://example.org/foo/bar>",
"Parsing: <http://[2001::1]:80> against <http://example.org/foo/bar>",
"Parsing: <http://[google.com]> against <http://other.com/>"
);
/** Test how {@link HttpUrl} does against the web platform test suite. */
@Ignore // TODO(jwilson): implement character encoding.
@Test public void httpUrl() throws Exception {
if (!testData.scheme.isEmpty() && !HTTP_URL_SCHEMES.contains(testData.scheme)) {
System.out.println("Ignoring unsupported scheme " + testData.scheme);
return;
}
if (!testData.base.startsWith("https:") && !testData.base.startsWith("http:")) {
if (!testData.base.startsWith("https:")
&& !testData.base.startsWith("http:")
&& !testData.base.equals("about:blank")) {
System.out.println("Ignoring unsupported base " + testData.base);
return;
}
try {
testHttpUrl();
} catch (AssertionError e) {
if (tolerateFailure()) {
System.out.println("Tolerable failure: " + e.getMessage());
return;
} catch (Throwable e) {
if (KNOWN_FAILURES.contains(testData.toString())) {
System.out.println("Ignoring known failure: " + testData);
e.printStackTrace();
} else {
throw e;
}
throw e;
}
}
@@ -185,23 +136,9 @@ public final class WebPlatformUrlTest {
}
}
/**
* Returns true if several major browsers also fail this test, in which case the test itself is
* questionable.
*/
private boolean tolerateFailure() {
return !firefoxResult.isPass()
&& !chromeResultResult.isPass()
&& !safariResult.isPass();
}
private static List<WebPlatformUrlTestData> loadTests() throws IOException {
BufferedSource source = Okio.buffer(Okio.source(
WebPlatformUrlTest.class.getResourceAsStream("/web-platform-test-urltestdata.txt")));
return WebPlatformUrlTestData.load(source);
}
private static WebPlatformTestRun loadTestRun(String name) throws IOException {
return WebPlatformTestRun.load(WebPlatformUrlTest.class.getResourceAsStream(name));
}
}