public final class MockWebServer extends org.junit.rules.ExternalResource implements Closeable
Constructor and Description |
---|
MockWebServer() |
Modifier and Type | Method and Description |
---|---|
protected void |
after() |
protected void |
before() |
void |
close() |
void |
enqueue(MockResponse response)
Scripts
response to be returned to a request made in sequence. |
String |
getHostName() |
int |
getPort() |
int |
getRequestCount()
Returns the number of HTTP requests received thus far by this server.
|
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(List<Protocol> protocols)
Indicates the protocols supported by ALPN on incoming HTTPS connections.
|
void |
setServerSocketFactory(ServerSocketFactory serverSocketFactory) |
void |
shutdown() |
void |
start()
Equivalent to
start(0) . |
void |
start(InetAddress inetAddress,
int port)
Starts the server on the given address and port.
|
void |
start(int port)
Starts the server on the loopback interface for the given port.
|
RecordedRequest |
takeRequest()
Awaits the next HTTP request, removes it, and returns it.
|
RecordedRequest |
takeRequest(long timeout,
TimeUnit unit)
Awaits the next HTTP request (waiting up to the specified wait time if necessary), removes it,
and returns it.
|
Proxy |
toProxyAddress() |
String |
toString() |
HttpUrl |
url(String path)
Returns a URL for connecting to this server.
|
void |
useHttps(SSLSocketFactory sslSocketFactory,
boolean tunnelProxy)
Serve requests with HTTPS rather than otherwise.
|
protected void before()
before
in class org.junit.rules.ExternalResource
public int getPort()
public String getHostName()
public Proxy toProxyAddress()
public void setServerSocketFactory(ServerSocketFactory serverSocketFactory)
public HttpUrl url(String path)
path
- the request path, such as "/".public void setBodyLimit(long maxBodyLength)
public void setProtocolNegotiationEnabled(boolean protocolNegotiationEnabled)
public void setProtocols(List<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 void useHttps(SSLSocketFactory sslSocketFactory, boolean tunnelProxy)
tunnelProxy
- true to expect the HTTP CONNECT method before negotiating TLS.public RecordedRequest takeRequest() throws InterruptedException
InterruptedException
public RecordedRequest takeRequest(long timeout, TimeUnit unit) throws InterruptedException
timeout
- how long to wait before giving up, in units of unit
unit
- a TimeUnit
determining how to interpret the timeout
parameterInterruptedException
public 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.ClassCastException
- if the default dispatcher has been replaced with setDispatcher(Dispatcher)
.public void start() throws IOException
start(0)
.IOException
public void start(int port) throws 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.IOException
public void start(InetAddress inetAddress, int port) throws 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.IOException
public void shutdown() throws IOException
IOException
protected void after()
after
in class org.junit.rules.ExternalResource
public void setDispatcher(Dispatcher dispatcher)
queue
; custom
dispatchers can vary the response based on timing or the content of the request.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
Copyright © 2017. All Rights Reserved.