Skip to content

Commit

Permalink
Test for file not found in post body (square#4151)
Browse files Browse the repository at this point in the history
* test for FNFE

* tighten up test behaviour
  • Loading branch information
yschimke authored and swankjesse committed Aug 6, 2018
1 parent 80bb3b1 commit ec0cdf2
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions okhttp-tests/src/test/java/okhttp3/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,10 +45,12 @@
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;
Expand All @@ -66,8 +69,8 @@
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;
Expand All @@ -84,7 +87,6 @@
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;
Expand Down Expand Up @@ -3262,6 +3264,31 @@ private RequestBody requestBody(final boolean chunked, final long size, final in
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() {
Expand Down

0 comments on commit ec0cdf2

Please sign in to comment.