use of mockwebserver3.MockResponse in project okhttp by square.
the class EventSourceHttpTest method cancelInEventShortCircuits.
@Test
public void cancelInEventShortCircuits() throws IOException {
server.enqueue(new MockResponse().setBody("" + "data: hey\n" + "\n").setHeader("content-type", "text/event-stream"));
// Will cancel in onOpen().
listener.enqueueCancel();
newEventSource();
listener.assertOpen();
listener.assertFailure("canceled");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class EventSourcesHttpTest method processResponse.
@Test
public void processResponse() throws IOException {
server.enqueue(new MockResponse().setBody("" + "data: hey\n" + "\n").setHeader("content-type", "text/event-stream"));
Request request = new Request.Builder().url(server.url("/")).build();
Response response = client.newCall(request).execute();
EventSources.processResponse(response, listener);
listener.assertOpen();
listener.assertEvent(null, null, "hey");
listener.assertClose();
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method clientSuppliedIfNoneMatchSinceWithCachedResult.
@Test
public void clientSuppliedIfNoneMatchSinceWithCachedResult() throws Exception {
String lastModifiedDate = formatDate(-3, TimeUnit.MINUTES);
MockResponse response = new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Date: " + formatDate(-2, TimeUnit.MINUTES)).addHeader("Cache-Control: max-age=0");
RecordedRequest request = assertClientSuppliedCondition(response, "If-None-Match", "v1");
assertThat(request.getHeader("If-None-Match")).isEqualTo("v1");
assertThat(request.getHeader("If-Modified-Since")).isNull();
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method conditionalHitHeadersCombined.
@Test
public void conditionalHitHeadersCombined() throws Exception {
server.enqueue(new MockResponse().addHeader("Etag", "a").addHeader("Cache-Control: max-age=0").addHeader("A: a1").addHeader("B: b2").addHeader("B: b3").setBody("abcd"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED).addHeader("B: b4").addHeader("B: b5").addHeader("C: c6"));
Response response1 = get(server.url("/"));
assertThat(response1.body().string()).isEqualTo("abcd");
assertThat(response1.headers()).isEqualTo(Headers.of("Etag", "a", "Cache-Control", "max-age=0", "A", "a1", "B", "b2", "B", "b3", "Content-Length", "4"));
// The original 'A' header is retained because the network response doesn't have one.
// The original 'B' headers are replaced by the network response.
// The network's 'C' header is added.
Response response2 = get(server.url("/"));
assertThat(response2.body().string()).isEqualTo("abcd");
assertThat(response2.headers()).isEqualTo(Headers.of("Etag", "a", "Cache-Control", "max-age=0", "A", "a1", "Content-Length", "4", "B", "b4", "B", "b5", "C", "c6"));
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method cacheControlNoCacheAndExpirationDateInTheFuture.
@Test
public void cacheControlNoCacheAndExpirationDateInTheFuture() throws Exception {
String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).addHeader("Cache-Control: no-cache"));
assertThat(conditionalRequest.getHeader("If-Modified-Since")).isEqualTo(lastModifiedDate);
}
Aggregations