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

Implement Closeable on MockWebServer.

Enables use via try-with-resources.
This commit is contained in:
Jake Wharton
2016-09-13 13:56:03 -04:00
parent 413323f10d
commit 404f63a20c
2 changed files with 12 additions and 1 deletions

View File

@@ -17,6 +17,7 @@
package okhttp3.mockwebserver;
import java.io.Closeable;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.InetAddress;
@@ -99,7 +100,7 @@ import static okhttp3.mockwebserver.SocketPolicy.UPGRADE_TO_SSL_AT_END;
* A scriptable web server. Callers supply canned responses and the server replays them upon request
* in sequence.
*/
public final class MockWebServer implements TestRule {
public final class MockWebServer implements TestRule, Closeable {
static {
Internal.initializeInstanceForTests();
}
@@ -807,6 +808,10 @@ public final class MockWebServer implements TestRule {
return "MockWebServer[" + port + "]";
}
@Override public void close() throws IOException {
shutdown();
}
/** A buffer wrapper that drops data after {@code bodyLimit} bytes. */
private static class TruncatingBuffer implements Sink {
private final Buffer buffer = new Buffer();

View File

@@ -16,6 +16,7 @@
package okhttp3.mockwebserver;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -339,6 +340,11 @@ public final class MockWebServerTest {
server.shutdown();
}
@Test public void closeViaClosable() throws IOException {
Closeable server = new MockWebServer();
server.close();
}
@Test public void shutdownWithoutEnqueue() throws IOException {
MockWebServer server = new MockWebServer();
server.start();