public final class MockWebServer extends Object
| Constructor and Description |
|---|
MockWebServer() |
| Modifier and Type | Method and Description |
|---|---|
void |
enqueue(MockResponse response)
Scripts
response to be returned to a request made in sequence. |
String |
getCookieDomain()
Returns a cookie domain for this server.
|
String |
getHostName() |
int |
getPort() |
int |
getRequestCount()
Returns the number of HTTP requests received thus far by this server.
|
URL |
getUrl(String path)
Returns a URL for connecting to this server.
|
void |
play()
Equivalent to
play(0). |
void |
play(int port)
Starts the server, serves all enqueued requests, and shuts the server down.
|
void |
setBodyLimit(int 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 |
setNpnEnabled(boolean npnEnabled)
Sets whether NPN is used on incoming HTTPS connections to negotiate a
protocol like HTTP/1.1 or SPDY/3.
|
void |
setNpnProtocols(List<Protocol> protocols)
Indicates the protocols supported by NPN on incoming HTTPS connections.
|
void |
shutdown() |
RecordedRequest |
takeRequest()
Awaits the next HTTP request, removes it, and returns it.
|
Proxy |
toProxyAddress() |
void |
useHttps(SSLSocketFactory sslSocketFactory,
boolean tunnelProxy)
Serve requests with HTTPS rather than otherwise.
|
public int getPort()
public String getHostName()
public Proxy toProxyAddress()
public URL getUrl(String path)
path - the request path, such as "/".public String getCookieDomain()
public void setBodyLimit(int maxBodyLength)
public void setNpnEnabled(boolean npnEnabled)
public void setNpnProtocols(List<Protocol> protocols)
protocols - the protocols to use, in order of preference. The list
must contain "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
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.ClassCastException - if the default dispatcher has been replaced
with setDispatcher(Dispatcher).public void play()
throws IOException
play(0).IOExceptionpublic void play(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.IOExceptionpublic void shutdown()
throws IOException
IOExceptionpublic void setDispatcher(Dispatcher dispatcher)
queue; custom dispatchers can vary the
response based on timing or the content of the request.Copyright © 2014. All Rights Reserved.