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:
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user