use of mockwebserver3.RecordedRequest in project sonarlint-core by SonarSource.
the class MockWebServerExtension method beforeEach.
@Override
public void beforeEach(ExtensionContext context) throws Exception {
server = new MockWebServer();
responsesByPath.clear();
final Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (responsesByPath.containsKey(request.getPath())) {
return responsesByPath.get(request.getPath());
}
return new MockResponse().setResponseCode(404);
}
};
server.setDispatcher(dispatcher);
server.start();
}
use of mockwebserver3.RecordedRequest 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.RecordedRequest 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);
}
use of mockwebserver3.RecordedRequest in project okhttp by square.
the class CacheTest method assertConditionallyCached.
/**
* @return the request with the conditional get headers.
*/
private RecordedRequest assertConditionallyCached(MockResponse response) throws Exception {
// scenario 1: condition succeeds
server.enqueue(response.setBody("A").setStatus("HTTP/1.1 200 A-OK"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
// scenario 2: condition fails
server.enqueue(response.setBody("B").setStatus("HTTP/1.1 200 B-OK"));
server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 C-OK").setBody("C"));
HttpUrl valid = server.url("/valid");
Response response1 = get(valid);
assertThat(response1.body().string()).isEqualTo("A");
assertThat(response1.code()).isEqualTo(HttpURLConnection.HTTP_OK);
assertThat(response1.message()).isEqualTo("A-OK");
Response response2 = get(valid);
assertThat(response2.body().string()).isEqualTo("A");
assertThat(response2.code()).isEqualTo(HttpURLConnection.HTTP_OK);
assertThat(response2.message()).isEqualTo("A-OK");
HttpUrl invalid = server.url("/invalid");
Response response3 = get(invalid);
assertThat(response3.body().string()).isEqualTo("B");
assertThat(response3.code()).isEqualTo(HttpURLConnection.HTTP_OK);
assertThat(response3.message()).isEqualTo("B-OK");
Response response4 = get(invalid);
assertThat(response4.body().string()).isEqualTo("C");
assertThat(response4.code()).isEqualTo(HttpURLConnection.HTTP_OK);
assertThat(response4.message()).isEqualTo("C-OK");
// regular get
server.takeRequest();
// conditional get
return server.takeRequest();
}
use of mockwebserver3.RecordedRequest 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