use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CallTest method uploadBodySmallChunkedEncoding.
@Test
public void uploadBodySmallChunkedEncoding() throws Exception {
upload(true, 1048576, 256);
RecordedRequest recordedRequest = server.takeRequest();
assertEquals(1048576, recordedRequest.getBodySize());
assertFalse(recordedRequest.getChunkSizes().isEmpty());
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CallTest method canceledBeforeIOSignalsOnFailure.
/**
* This test puts a request in front of one that is to be canceled, so that it is canceled before
* I/O takes place.
*/
@Test
public void canceledBeforeIOSignalsOnFailure() throws Exception {
// Force requests to be executed serially.
okhttp3.Dispatcher dispatcher = new okhttp3.Dispatcher(client.dispatcher().executorService());
dispatcher.setMaxRequests(1);
client = client.newBuilder().dispatcher(dispatcher).build();
Request requestA = new Request.Builder().url(server.url("/a")).build();
Request requestB = new Request.Builder().url(server.url("/b")).build();
final Call callA = client.newCall(requestA);
final Call callB = client.newCall(requestB);
server.setDispatcher(new Dispatcher() {
char nextResponse = 'A';
@Override
public MockResponse dispatch(RecordedRequest request) {
callB.cancel();
return new MockResponse().setBody(Character.toString(nextResponse++));
}
});
callA.enqueue(callback);
callB.enqueue(callback);
assertEquals("/a", server.takeRequest().getPath());
callback.await(requestA.url()).assertBody("A");
// At this point we know the callback is ready, and that it will receive a cancel failure.
callback.await(requestB.url()).assertFailure("Canceled", "Socket closed");
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CacheTest method redirectToCachedResult.
@Test
public void redirectToCachedResult() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("ABC"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_PERM).addHeader("Location: /foo"));
server.enqueue(new MockResponse().setBody("DEF"));
Request request1 = new Request.Builder().url(server.url("/foo")).build();
Response response1 = client.newCall(request1).execute();
assertEquals("ABC", response1.body().string());
RecordedRequest recordedRequest1 = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", recordedRequest1.getRequestLine());
assertEquals(0, recordedRequest1.getSequenceNumber());
Request request2 = new Request.Builder().url(server.url("/bar")).build();
Response response2 = client.newCall(request2).execute();
assertEquals("ABC", response2.body().string());
RecordedRequest recordedRequest2 = server.takeRequest();
assertEquals("GET /bar HTTP/1.1", recordedRequest2.getRequestLine());
assertEquals(1, recordedRequest2.getSequenceNumber());
// an unrelated request should reuse the pooled connection
Request request3 = new Request.Builder().url(server.url("/baz")).build();
Response response3 = client.newCall(request3).execute();
assertEquals("DEF", response3.body().string());
RecordedRequest recordedRequest3 = server.takeRequest();
assertEquals("GET /baz HTTP/1.1", recordedRequest3.getRequestLine());
assertEquals(2, recordedRequest3.getSequenceNumber());
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CacheTest method pragmaNoCacheAndExpirationDateInTheFuture.
@Test
public void pragmaNoCacheAndExpirationDateInTheFuture() throws Exception {
String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).addHeader("Pragma: no-cache"));
assertEquals(lastModifiedDate, conditionalRequest.getHeader("If-Modified-Since"));
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class UrlConnectionCacheTest method setIfModifiedSince.
/**
* Confirm that {@link URLConnection#setIfModifiedSince} causes an If-Modified-Since header with a
* GMT timestamp.
*
* https://code.google.com/p/android/issues/detail?id=66135
*/
@Test
public void setIfModifiedSince() throws Exception {
server.enqueue(new MockResponse().setBody("A"));
URL url = server.url("/").url();
URLConnection connection = urlFactory.open(url);
connection.setIfModifiedSince(1393666200000L);
assertEquals("A", readAscii(connection));
RecordedRequest request = server.takeRequest();
String ifModifiedSinceHeader = request.getHeader("If-Modified-Since");
assertEquals("Sat, 01 Mar 2014 09:30:00 GMT", ifModifiedSinceHeader);
}
Aggregations