diff --git a/mockwebserver/pom.xml b/mockwebserver/pom.xml index 86d250338..3603a842d 100644 --- a/mockwebserver/pom.xml +++ b/mockwebserver/pom.xml @@ -18,6 +18,12 @@ okhttp ${project.version} + + com.squareup.okhttp + okhttp-testing-support + ${project.version} + test + com.squareup.okhttp okhttp-ws diff --git a/okcurl/pom.xml b/okcurl/pom.xml index 467c17048..d167ce18d 100644 --- a/okcurl/pom.xml +++ b/okcurl/pom.xml @@ -18,6 +18,12 @@ okhttp ${project.version} + + com.squareup.okhttp + okhttp-testing-support + ${project.version} + test + org.bouncycastle bcprov-jdk15on diff --git a/okhttp-android-support/pom.xml b/okhttp-android-support/pom.xml index 74f15fcf0..3bf11e9f4 100644 --- a/okhttp-android-support/pom.xml +++ b/okhttp-android-support/pom.xml @@ -14,6 +14,12 @@ Classes to support the Android platform's use of OkHttp (not required for most developers). + + com.squareup.okhttp + okhttp-testing-support + ${project.version} + test + com.squareup.okhttp okhttp-urlconnection diff --git a/okhttp-apache/pom.xml b/okhttp-apache/pom.xml index 2eafbad44..6bc872bb1 100644 --- a/okhttp-apache/pom.xml +++ b/okhttp-apache/pom.xml @@ -18,6 +18,12 @@ okhttp ${project.version} + + com.squareup.okhttp + okhttp-testing-support + ${project.version} + test + org.apache.httpcomponents httpclient diff --git a/okhttp-hpacktests/pom.xml b/okhttp-hpacktests/pom.xml index 6ae44d5ce..4d299fefd 100644 --- a/okhttp-hpacktests/pom.xml +++ b/okhttp-hpacktests/pom.xml @@ -6,7 +6,7 @@ com.squareup.okhttp parent - 2.3.0-SNAPSHOT + 2.4.0-SNAPSHOT okhttp-hpacktests @@ -22,6 +22,12 @@ okhttp ${project.version} + + com.squareup.okhttp + okhttp-testing-support + ${project.version} + test + junit junit diff --git a/okhttp-testing-support/pom.xml b/okhttp-testing-support/pom.xml new file mode 100644 index 000000000..ad016c870 --- /dev/null +++ b/okhttp-testing-support/pom.xml @@ -0,0 +1,22 @@ + + + + 4.0.0 + + + com.squareup.okhttp + parent + 2.4.0-SNAPSHOT + + + okhttp-testing-support + OkHttp test support classes + + + + junit + junit + true + + + diff --git a/okhttp-testing-support/src/main/java/com/squareup/okhttp/testing/InstallUncaughtExceptionHandlerListener.java b/okhttp-testing-support/src/main/java/com/squareup/okhttp/testing/InstallUncaughtExceptionHandlerListener.java new file mode 100644 index 000000000..4dd4c92e9 --- /dev/null +++ b/okhttp-testing-support/src/main/java/com/squareup/okhttp/testing/InstallUncaughtExceptionHandlerListener.java @@ -0,0 +1,66 @@ +/* + * 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.testing; + +import org.junit.runner.Description; +import org.junit.runner.Result; +import org.junit.runner.notification.RunListener; + +import java.io.PrintWriter; +import java.io.StringWriter; + +/** + * A {@link org.junit.runner.notification.RunListener} used to install an aggressive default + * {@link java.lang.Thread.UncaughtExceptionHandler} similar to the one found on Android. + * No exceptions should escape from OkHttp that might cause apps to be killed or tests to fail on + * Android. + */ +public class InstallUncaughtExceptionHandlerListener extends RunListener { + + private Thread.UncaughtExceptionHandler oldDefaultUncaughtExceptionHandler; + private Description lastTestStarted; + + @Override public void testRunStarted(Description description) throws Exception { + System.err.println("Installing aggressive uncaught exception handler"); + oldDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); + Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { + @Override public void uncaughtException(Thread thread, Throwable throwable) { + StringWriter errorText = new StringWriter(256); + errorText.append("Uncaught exception in OkHttp thread \""); + errorText.append(thread.getName()); + errorText.append("\"\n"); + throwable.printStackTrace(new PrintWriter(errorText)); + errorText.append("\n"); + if (lastTestStarted != null) { + errorText.append("Last test to start was: "); + errorText.append(lastTestStarted.getDisplayName()); + errorText.append("\n"); + } + System.err.print(errorText.toString()); + System.exit(-1); + } + }); + } + + @Override public void testStarted(Description description) throws Exception { + lastTestStarted = description; + } + + @Override public void testRunFinished(Result result) throws Exception { + Thread.setDefaultUncaughtExceptionHandler(oldDefaultUncaughtExceptionHandler); + System.err.println("Uninstalled aggressive uncaught exception handler"); + } +} diff --git a/okhttp-tests/pom.xml b/okhttp-tests/pom.xml index e3a99660e..4430fabe2 100644 --- a/okhttp-tests/pom.xml +++ b/okhttp-tests/pom.xml @@ -22,6 +22,12 @@ okhttp ${project.version} + + com.squareup.okhttp + okhttp-testing-support + ${project.version} + test + com.squareup.okhttp okhttp-urlconnection diff --git a/okhttp-urlconnection/pom.xml b/okhttp-urlconnection/pom.xml index 932a07235..a0938271f 100644 --- a/okhttp-urlconnection/pom.xml +++ b/okhttp-urlconnection/pom.xml @@ -18,6 +18,12 @@ okhttp ${project.version} + + com.squareup.okhttp + okhttp-testing-support + ${project.version} + test + junit diff --git a/okhttp-ws-tests/pom.xml b/okhttp-ws-tests/pom.xml index 424f4a56e..af4ea7edf 100644 --- a/okhttp-ws-tests/pom.xml +++ b/okhttp-ws-tests/pom.xml @@ -13,6 +13,12 @@ OkHttp Web Socket Tests + + com.squareup.okhttp + okhttp-testing-support + ${project.version} + test + com.squareup.okhttp okhttp-ws diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/spdy/SpdyConnection.java b/okhttp/src/main/java/com/squareup/okhttp/internal/spdy/SpdyConnection.java index f86c20e85..2966ce0ef 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/spdy/SpdyConnection.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/spdy/SpdyConnection.java @@ -578,7 +578,7 @@ public final class SpdyConnection implements Closeable { } connectionErrorCode = ErrorCode.NO_ERROR; streamErrorCode = ErrorCode.CANCEL; - } catch (IOException e) { + } catch (RuntimeException | IOException e) { connectionErrorCode = ErrorCode.PROTOCOL_ERROR; streamErrorCode = ErrorCode.PROTOCOL_ERROR; } finally { @@ -643,8 +643,11 @@ public final class SpdyConnection implements Closeable { @Override public void execute() { try { handler.receive(newStream); - } catch (IOException e) { - throw new RuntimeException(e); + } catch (RuntimeException | IOException e) { + try { + newStream.close(ErrorCode.PROTOCOL_ERROR); + } catch (IOException ignored) { + } } } }); diff --git a/pom.xml b/pom.xml index 60c4f01a2..8de06a80f 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,7 @@ okhttp-android-support okhttp-apache + okhttp-testing-support okhttp-urlconnection okhttp-ws @@ -132,6 +133,19 @@ org.apache.maven.plugins maven-surefire-plugin 2.17 + + + + + listener + com.squareup.okhttp.testing.InstallUncaughtExceptionHandlerListener + + + org.apache.maven.surefire