use of mockwebserver3.MockResponse in project okhttp by square.
the class WebSocketHttpTest method overflowOutgoingQueue.
@Test
public void overflowOutgoingQueue() {
webServer.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
// Send messages until the client's outgoing buffer overflows!
ByteString message = ByteString.of(new byte[1024 * 1024]);
long messageCount = 0;
while (true) {
boolean success = webSocket.send(message);
if (!success)
break;
messageCount++;
long queueSize = webSocket.queueSize();
assertThat(queueSize).isBetween(0L, messageCount * message.size());
// Expect to fail before enqueueing 32 MiB.
assertThat(messageCount).isLessThan(32L);
}
// Confirm all sent messages were received, followed by a client-initiated close.
WebSocket server = serverListener.assertOpen();
for (int i = 0; i < messageCount; i++) {
serverListener.assertBinaryMessage(message);
}
serverListener.assertClosing(1001, "");
// When the server acknowledges the close the connection shuts down gracefully.
server.close(1000, null);
clientListener.assertClosing(1000, "");
clientListener.assertClosed(1000, "");
serverListener.assertClosed(1001, "");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class InterceptorTest method networkInterceptorsCannotChangeServerAddress.
@Test
public void networkInterceptorsCannotChangeServerAddress() throws Exception {
server.enqueue(new MockResponse().setResponseCode(500));
Interceptor interceptor = chain -> {
Address address = chain.connection().route().address();
String sameHost = address.url().host();
int differentPort = address.url().port() + 1;
return chain.proceed(chain.request().newBuilder().url("http://" + sameHost + ":" + differentPort + "/").build());
};
client = client.newBuilder().addNetworkInterceptor(interceptor).build();
Request request = new Request.Builder().url(server.url("/")).build();
try {
client.newCall(request).execute();
fail();
} catch (IllegalStateException expected) {
assertThat(expected.getMessage()).isEqualTo(("network interceptor " + interceptor + " must retain the same host and port"));
}
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class InterceptorTest method asyncInterceptors.
private void asyncInterceptors(boolean network) throws Exception {
server.enqueue(new MockResponse());
addInterceptor(network, chain -> {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().addHeader("OkHttp-Intercepted", "yep").build();
});
Request request = new Request.Builder().url(server.url("/")).build();
client.newCall(request).enqueue(callback);
callback.await(request.url()).assertCode(200).assertHeader("OkHttp-Intercepted", "yep");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class InterceptorTest method multipleInterceptors.
private void multipleInterceptors(boolean network) throws Exception {
server.enqueue(new MockResponse());
addInterceptor(network, chain -> {
Request originalRequest = chain.request();
Response originalResponse = chain.proceed(originalRequest.newBuilder().addHeader("Request-Interceptor", // 1. Added first.
"Android").build());
return originalResponse.newBuilder().addHeader("Response-Interceptor", // 4. Added last.
"Donut").build();
});
addInterceptor(network, chain -> {
Request originalRequest = chain.request();
Response originalResponse = chain.proceed(originalRequest.newBuilder().addHeader("Request-Interceptor", // 2. Added second.
"Bob").build());
return originalResponse.newBuilder().addHeader("Response-Interceptor", // 3. Added third.
"Cupcake").build();
});
Request request = new Request.Builder().url(server.url("/")).build();
Response response = client.newCall(request).execute();
assertThat(response.headers("Response-Interceptor")).containsExactly("Cupcake", "Donut");
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getHeaders().values("Request-Interceptor")).containsExactly("Android", "Bob");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class InterceptorTest method applicationInterceptorReturnsNull.
@Test
public void applicationInterceptorReturnsNull() throws Exception {
server.enqueue(new MockResponse());
Interceptor interceptor = chain -> {
chain.proceed(chain.request());
return null;
};
client = client.newBuilder().addInterceptor(interceptor).build();
ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
client = client.newBuilder().dispatcher(new Dispatcher(executor)).build();
Request request = new Request.Builder().url(server.url("/")).build();
try {
client.newCall(request).execute();
fail();
} catch (NullPointerException expected) {
assertThat(expected.getMessage()).isEqualTo(("interceptor " + interceptor + " returned null"));
}
}
Aggregations