From ec0cdf2970aa6104a481e35f4e2734448ade28bc Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Mon, 6 Aug 2018 03:29:36 +0300 Subject: [PATCH] Test for file not found in post body (#4151) * test for FNFE * tighten up test behaviour --- .../src/test/java/okhttp3/CallTest.java | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/okhttp-tests/src/test/java/okhttp3/CallTest.java b/okhttp-tests/src/test/java/okhttp3/CallTest.java index b879072c0..9bbcce07d 100644 --- a/okhttp-tests/src/test/java/okhttp3/CallTest.java +++ b/okhttp-tests/src/test/java/okhttp3/CallTest.java @@ -16,6 +16,7 @@ package okhttp3; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; @@ -44,10 +45,12 @@ import java.util.concurrent.Future; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; +import javax.annotation.Nullable; import javax.net.ssl.SSLException; import javax.net.ssl.SSLHandshakeException; import javax.net.ssl.SSLPeerUnverifiedException; @@ -66,8 +69,8 @@ import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; import okhttp3.mockwebserver.SocketPolicy; -import okhttp3.tls.HeldCertificate; import okhttp3.tls.HandshakeCertificates; +import okhttp3.tls.HeldCertificate; import okio.Buffer; import okio.BufferedSink; import okio.BufferedSource; @@ -84,7 +87,6 @@ import org.junit.rules.Timeout; import static java.net.CookiePolicy.ACCEPT_ORIGINAL_SERVER; import static okhttp3.TestUtil.awaitGarbageCollection; import static okhttp3.TestUtil.defaultClient; -import static okhttp3.internal.platform.PlatformTest.getPlatform; import static okhttp3.tls.internal.TlsUtil.localhost; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; @@ -3262,6 +3264,31 @@ public final class CallTest { assertEquals(localIpAddress + ":" + server.getPort(), recordedRequest.getHeader("Host")); } + @Test public void postWithFileNotFound() throws Exception { + final AtomicInteger called = new AtomicInteger(0); + + RequestBody body = new RequestBody() { + @Nullable @Override public MediaType contentType() { + return MediaType.get("application/octet-stream"); + } + + @Override public void writeTo(BufferedSink sink) throws IOException { + called.incrementAndGet(); + throw new FileNotFoundException(); + } + }; + + Request request = new Request.Builder() + .url(server.url("/")) + .post(body) + .build(); + + executeSynchronously(request) + .assertFailure(FileNotFoundException.class); + + assertEquals(1L, called.get()); + } + private void makeFailingCall() { RequestBody requestBody = new RequestBody() { @Override public MediaType contentType() {