use of mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method requestBodyFail.
private void requestBodyFail(@Nullable Protocol expectedProtocol) {
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_DURING_REQUEST_BODY));
NonCompletingRequestBody request = new NonCompletingRequestBody();
Call call = client.newCall(new Request.Builder().url(server.url("/")).post(request).build());
try {
call.execute();
fail();
} catch (IOException expected) {
}
if (expectedProtocol != null) {
ConnectionAcquired connectionAcquired = listener.removeUpToEvent(ConnectionAcquired.class);
assertThat(connectionAcquired.getConnection().protocol()).isEqualTo(expectedProtocol);
}
CallFailed callFailed = listener.removeUpToEvent(CallFailed.class);
assertThat(callFailed.getIoe()).isNotNull();
assertThat(request.ioe).isNotNull();
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method successfulCallEventSequenceForEnqueue.
@Test
public void successfulCallEventSequenceForEnqueue() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
final CountDownLatch completionLatch = new CountDownLatch(1);
Callback callback = new Callback() {
@Override
public void onFailure(Call call, IOException e) {
completionLatch.countDown();
}
@Override
public void onResponse(Call call, Response response) {
response.close();
completionLatch.countDown();
}
};
call.enqueue(callback);
completionLatch.await();
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "CallEnd");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method cacheHit.
@Test
public void cacheHit() throws IOException {
enableCache();
server.enqueue(new MockResponse().setBody("abc").addHeader("cache-control: public, max-age=300"));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertThat(response.code()).isEqualTo(200);
assertThat(response.body().string()).isEqualTo("abc");
response.close();
listener.clearAllEvents();
call = call.clone();
response = call.execute();
assertThat(response.code()).isEqualTo(200);
assertThat(response.body().string()).isEqualTo("abc");
response.close();
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "CacheHit", "CallEnd");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method successfulConnect.
@Test
public void successfulConnect() throws IOException {
server.enqueue(new MockResponse());
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertThat(response.code()).isEqualTo(200);
response.body().close();
InetAddress address = client.dns().lookup(server.getHostName()).get(0);
InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort());
ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class);
assertThat(connectStart.getCall()).isSameAs(call);
assertThat(connectStart.getInetSocketAddress()).isEqualTo(expectedAddress);
assertThat(connectStart.getProxy()).isEqualTo(Proxy.NO_PROXY);
ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class);
assertThat(connectEnd.getCall()).isSameAs(call);
assertThat(connectEnd.getInetSocketAddress()).isEqualTo(expectedAddress);
assertThat(connectEnd.getProtocol()).isEqualTo(Protocol.HTTP_1_1);
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method immutableIsNotCachedBeyondFreshnessLifetime.
@Test
public void immutableIsNotCachedBeyondFreshnessLifetime() throws Exception {
// last modified: 115 seconds ago
// served: 15 seconds ago
// default lifetime: (115 - 15) / 10 = 10 seconds
// expires: 10 seconds from served date = 5 seconds ago
String lastModifiedDate = formatDate(-115, TimeUnit.SECONDS);
RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("Cache-Control: immutable").addHeader("Last-Modified: " + lastModifiedDate).addHeader("Date: " + formatDate(-15, TimeUnit.SECONDS)));
assertThat(conditionalRequest.getHeader("If-Modified-Since")).isEqualTo(lastModifiedDate);
}
Aggregations