use of mockwebserver3.RecordedRequest in project okhttp by square.
the class CacheTest method retainServedDateFormat.
/**
* For Last-Modified and Date headers, we should echo the date back in the exact format we were
* served.
*/
@Test
public void retainServedDateFormat() throws Exception {
// Serve a response with a non-standard date format that OkHttp supports.
Date lastModifiedDate = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(-1));
Date servedDate = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(-2));
DateFormat dateFormat = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String lastModifiedString = dateFormat.format(lastModifiedDate);
String servedString = dateFormat.format(servedDate);
// This response should be conditionally cached.
server.enqueue(new MockResponse().addHeader("Last-Modified: " + lastModifiedString).addHeader("Expires: " + servedString).setBody("A"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
assertThat(get(server.url("/")).body().string()).isEqualTo("A");
assertThat(get(server.url("/")).body().string()).isEqualTo("A");
// The first request has no conditions.
RecordedRequest request1 = server.takeRequest();
assertThat(request1.getHeader("If-Modified-Since")).isNull();
// The 2nd request uses the server's date format.
RecordedRequest request2 = server.takeRequest();
assertThat(request2.getHeader("If-Modified-Since")).isEqualTo(lastModifiedString);
}
use of mockwebserver3.RecordedRequest in project okhttp by square.
the class WebSocketHttpTest method successfulExtensions.
private void successfulExtensions(String extensionsHeader) throws Exception {
webServer.enqueue(new MockResponse().addHeader("Sec-WebSocket-Extensions", extensionsHeader).withWebSocketUpgrade(serverListener));
WebSocket client = newWebSocket();
clientListener.assertOpen();
WebSocket server = serverListener.assertOpen();
// Server to client message big enough to be compressed.
String message1 = TestUtil.repeat('a', (int) RealWebSocket.DEFAULT_MINIMUM_DEFLATE_SIZE);
server.send(message1);
clientListener.assertTextMessage(message1);
// Client to server message big enough to be compressed.
String message2 = TestUtil.repeat('b', (int) RealWebSocket.DEFAULT_MINIMUM_DEFLATE_SIZE);
client.send(message2);
serverListener.assertTextMessage(message2);
// Empty server to client message.
String message3 = "";
server.send(message3);
clientListener.assertTextMessage(message3);
// Empty client to server message.
String message4 = "";
client.send(message4);
serverListener.assertTextMessage(message4);
// Server to client message that shares context with message1.
String message5 = message1 + message1;
server.send(message5);
clientListener.assertTextMessage(message5);
// Client to server message that shares context with message2.
String message6 = message2 + message2;
client.send(message6);
serverListener.assertTextMessage(message6);
closeWebSockets(client, server);
RecordedRequest upgradeRequest = webServer.takeRequest();
assertThat(upgradeRequest.getHeader("Sec-WebSocket-Extensions")).isEqualTo("permessage-deflate");
}
use of mockwebserver3.RecordedRequest in project okhttp by square.
the class CookiesTest method testCookiesSentIgnoresCase.
@Test
public void testCookiesSentIgnoresCase() throws Exception {
client = client.newBuilder().cookieJar(new JavaNetCookieJar(new CookieManager() {
@Override
public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) {
Map<String, List<String>> result = new LinkedHashMap<>();
result.put("COOKIE", Collections.singletonList("Bar=bar"));
result.put("cooKIE2", Collections.singletonList("Baz=baz"));
return result;
}
})).build();
server.enqueue(new MockResponse());
get(server.url("/"));
RecordedRequest request = server.takeRequest();
assertThat(request.getHeader("Cookie")).isEqualTo("Bar=bar; Baz=baz");
assertThat(request.getHeader("Cookie2")).isNull();
assertThat(request.getHeader("Quux")).isNull();
}
use of mockwebserver3.RecordedRequest in project okhttp by square.
the class CookiesTest method cookieHandlerLikeAndroid.
@Test
public void cookieHandlerLikeAndroid() throws Exception {
server.enqueue(new MockResponse());
final HttpUrl serverUrl = urlWithIpAddress(server, "/");
CookieHandler androidCookieHandler = new CookieHandler() {
@Override
public Map<String, List<String>> get(URI uri, Map<String, List<String>> map) {
return Collections.singletonMap("Cookie", Collections.singletonList("$Version=\"1\"; " + "a=\"android\";$Path=\"/\";$Domain=\"" + serverUrl.host() + "\"; " + "b=\"banana\";$Path=\"/\";$Domain=\"" + serverUrl.host() + "\""));
}
@Override
public void put(URI uri, Map<String, List<String>> map) throws IOException {
}
};
client = client.newBuilder().cookieJar(new JavaNetCookieJar(androidCookieHandler)).build();
get(serverUrl);
RecordedRequest request = server.takeRequest();
assertThat(request.getHeader("Cookie")).isEqualTo("a=android; b=banana");
}
use of mockwebserver3.RecordedRequest in project okhttp by square.
the class CookiesTest method receiveAndSendMultipleCookies.
@Test
public void receiveAndSendMultipleCookies() throws Exception {
server.enqueue(new MockResponse().addHeader("Set-Cookie", "a=android").addHeader("Set-Cookie", "b=banana"));
server.enqueue(new MockResponse());
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
get(urlWithIpAddress(server, "/"));
RecordedRequest request1 = server.takeRequest();
assertThat(request1.getHeader("Cookie")).isNull();
get(urlWithIpAddress(server, "/"));
RecordedRequest request2 = server.takeRequest();
assertThat(request2.getHeader("Cookie")).isEqualTo("a=android; b=banana");
}
Aggregations