java.io.Closeable, java.lang.AutoCloseablepublic final class MockWebServer
extends org.junit.rules.ExternalResource
implements java.io.Closeable
| Constructor | Description |
|---|---|
MockWebServer() |
| Modifier and Type | Method | Description |
|---|---|---|
protected void |
after() |
|
protected void |
before() |
|
void |
close() |
|
void |
enqueue(MockResponse response) |
Scripts
response to be returned to a request made in sequence. |
java.lang.String |
getHostName() |
|
int |
getPort() |
|
int |
getRequestCount() |
Returns the number of HTTP requests received thus far by this server.
|
void |
noClientAuth() |
Configure the server to not perform SSL authentication of the client.
|
java.util.List<okhttp3.Protocol> |
protocols() |
|
void |
requestClientAuth() |
Configure the server to want client auth.
|
void |
requireClientAuth() |
Configure the server to need client auth.
|
void |
setBodyLimit(long maxBodyLength) |
Sets the number of bytes of the POST body to keep in memory to the given limit.
|
void |
setDispatcher(Dispatcher dispatcher) |
Sets the dispatcher used to match incoming requests to mock responses.
|
void |
setProtocolNegotiationEnabled(boolean protocolNegotiationEnabled) |
Sets whether ALPN is used on incoming HTTPS connections to negotiate a protocol like HTTP/1.1
or HTTP/2.
|
void |
setProtocols(java.util.List<okhttp3.Protocol> protocols) |
Indicates the protocols supported by ALPN on incoming HTTPS connections.
|
void |
setServerSocketFactory(javax.net.ServerSocketFactory serverSocketFactory) |
|
void |
shutdown() |
|
void |
start() |
Equivalent to
start(0). |
void |
start(int port) |
Starts the server on the loopback interface for the given port.
|
void |
start(java.net.InetAddress inetAddress,
int port) |
Starts the server on the given address and port.
|
RecordedRequest |
takeRequest() |
Awaits the next HTTP request, removes it, and returns it.
|
RecordedRequest |
takeRequest(long timeout,
java.util.concurrent.TimeUnit unit) |
Awaits the next HTTP request (waiting up to the specified wait time if necessary), removes it,
and returns it.
|
java.net.Proxy |
toProxyAddress() |
|
java.lang.String |
toString() |
|
okhttp3.HttpUrl |
url(java.lang.String path) |
Returns a URL for connecting to this server.
|
void |
useHttps(javax.net.ssl.SSLSocketFactory sslSocketFactory,
boolean tunnelProxy) |
Serve requests with HTTPS rather than otherwise.
|
protected void before()
before in class org.junit.rules.ExternalResourcepublic int getPort()
public java.lang.String getHostName()
public java.net.Proxy toProxyAddress()
public void setServerSocketFactory(javax.net.ServerSocketFactory serverSocketFactory)
public okhttp3.HttpUrl url(java.lang.String path)
path - the request path, such as "/".public void setBodyLimit(long maxBodyLength)
public void setProtocolNegotiationEnabled(boolean protocolNegotiationEnabled)
public void setProtocols(java.util.List<okhttp3.Protocol> protocols)
negotiation is disabled.protocols - the protocols to use, in order of preference. The list must contain
Protocol.HTTP_1_1. It must not contain null.public java.util.List<okhttp3.Protocol> protocols()
public void useHttps(javax.net.ssl.SSLSocketFactory sslSocketFactory,
boolean tunnelProxy)
tunnelProxy - true to expect the HTTP CONNECT method before negotiating TLS.public void noClientAuth()
public void requestClientAuth()
public void requireClientAuth()
public RecordedRequest takeRequest() throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic RecordedRequest takeRequest(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
timeout - how long to wait before giving up, in units of unitunit - a TimeUnit determining how to interpret the timeout parameterjava.lang.InterruptedExceptionpublic int getRequestCount()
public void enqueue(MockResponse response)
response to be returned to a request made in sequence. The first request is
served by the first enqueued response; the second request by the second enqueued response; and
so on.java.lang.ClassCastException - if the default dispatcher has been replaced with setDispatcher(Dispatcher).public void start()
throws java.io.IOException
start(0).java.io.IOExceptionpublic void start(int port)
throws java.io.IOException
port - the port to listen to, or 0 for any available port. Automated tests should always
use port 0 to avoid flakiness when a specific port is unavailable.java.io.IOExceptionpublic void start(java.net.InetAddress inetAddress,
int port)
throws java.io.IOException
inetAddress - the address to create the server socket onport - the port to listen to, or 0 for any available port. Automated tests should always
use port 0 to avoid flakiness when a specific port is unavailable.java.io.IOExceptionpublic void shutdown()
throws java.io.IOException
java.io.IOExceptionprotected void after()
after in class org.junit.rules.ExternalResourcepublic void setDispatcher(Dispatcher dispatcher)
queue; custom
dispatchers can vary the response based on timing or the content of the request.public java.lang.String toString()
toString in class java.lang.Objectpublic void close()
throws java.io.IOException
close in interface java.lang.AutoCloseableclose in interface java.io.Closeablejava.io.IOExceptionCopyright © 2020. All rights reserved.