/** * Scripts {@code 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. * * @throws ClassCastException if the default dispatcher has been replaced with {@link * #setDispatcher(Dispatcher)}. */ public void enqueue(MockResponse response) { ((QueueDispatcher) dispatcher).enqueueResponse(response.clone()); }
public void setFailFast(boolean failFast) { MockResponse failFastResponse = failFast ? new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND) : null; setFailFast(failFastResponse); }
/** * Returns an early guess of the next response, used for policy on how an incoming request should * be received. The default implementation returns an empty response. Mischievous implementations * can return other values to test HTTP edge cases, such as unhappy socket policies or throttled * request bodies. */ public MockResponse peek() { return new MockResponse().setSocketPolicy(SocketPolicy.KEEP_OPEN); }
private void prepareResponse(Consumer<MockResponse> consumer) { MockResponse response = new MockResponse(); consumer.accept(response); this.server.enqueue(response); }
@Override public void setBody( MockResponse mockResponse, DuplexResponseBody duplexResponseBody) { mockResponse.setBody(duplexResponseBody); } };
private HttpUrl redirectUrl() { return mockWebServer.url("/oauth/"); }
/** Creates a new mock response with an empty body. */ public MockResponse() { setResponseCode(200); setHeader("Content-Length", 0); }
/** Equivalent to {@code start(0)}. */ public void start() throws IOException { start(0); }
@Override public void close() throws IOException { shutdown(); }
/** * Removes all headers named {@code name}, then adds a new header with the name and value. */ public MockResponse setHeader(String name, Object value) { removeHeader(name); return addHeader(name, value); }
/** * Attempts to perform a web socket upgrade on the connection. This will overwrite any previously * set status or body. */ public MockResponse withWebSocketUpgrade(WebSocketListener listener) { setStatus("HTTP/1.1 101 Switching Protocols"); setHeader("Connection", "Upgrade"); setHeader("Upgrade", "websocket"); body = null; webSocketListener = listener; return this; }
public MockResponse setBody(Buffer body) { setHeader("Content-Length", body.size()); this.body = body.clone(); // Defensive copy. return this; }
public MockResponse setResponseCode(int code) { String reason = "Mock Response"; if (code >= 100 && code < 200) { reason = "Informational"; } else if (code >= 200 && code < 300) { reason = "OK"; } else if (code >= 300 && code < 400) { reason = "Redirection"; } else if (code >= 400 && code < 500) { reason = "Client Error"; } else if (code >= 500 && code < 600) { reason = "Server Error"; } return setStatus("HTTP/1.1 " + code + " " + reason); }
/** * Respond to CONNECT requests until a SWITCH_TO_SSL_AT_END response is * dispatched. */ private void createTunnel() throws IOException, InterruptedException { BufferedSource source = Okio.buffer(Okio.source(raw)); BufferedSink sink = Okio.buffer(Okio.sink(raw)); while (true) { SocketPolicy socketPolicy = dispatcher.peek().getSocketPolicy(); if (!processOneRequest(raw, source, sink)) { throw new IllegalStateException("Tunnel without any CONNECT!"); } if (socketPolicy == UPGRADE_TO_SSL_AT_END) return; } }
private void dispatchBookkeepingRequest(int sequenceNumber, Socket socket) throws InterruptedException { RecordedRequest request = new RecordedRequest( null, null, null, -1, null, sequenceNumber, socket); requestCount.incrementAndGet(); requestQueue.add(request); dispatcher.dispatch(request); }
/** * Sets the response body to the UTF-8 encoded bytes of {@code body}, chunked every {@code * maxChunkSize} bytes. */ public MockResponse setChunkedBody(String body, int maxChunkSize) { return setChunkedBody(new Buffer().writeUtf8(body), maxChunkSize); }
public int getPort() { before(); return port; }
/** * Starts the server on the given address and port. * * @param inetAddress the address to create the server socket on * @param 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. */ public void start(InetAddress inetAddress, int port) throws IOException { start(new InetSocketAddress(inetAddress, port)); }
@Override protected synchronized void after() { try { shutdown(); } catch (IOException e) { logger.log(Level.WARNING, "MockWebServer shutdown failed", e); } }
/** * Starts the server on the loopback interface for the given port. * * @param 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. */ public void start(int port) throws IOException { start(InetAddress.getByName("localhost"), port); }