use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class URLConnectionTest method clientConfiguredCustomContentEncoding.
@Test
public void clientConfiguredCustomContentEncoding() throws Exception {
server.enqueue(new MockResponse().setBody("ABCDE").addHeader("Content-Encoding: custom"));
URLConnection connection = urlFactory.open(server.url("/").url());
connection.addRequestProperty("Accept-Encoding", "custom");
assertEquals("ABCDE", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest request = server.takeRequest();
assertEquals("custom", request.getHeader("Accept-Encoding"));
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class URLConnectionTest method noTransparentGzipFor304NotModified.
/**
* We had a bug where we attempted to gunzip responses that didn't have a body. This only came up
* with 304s since that response code can include headers (like "Content-Encoding") without any
* content to go along with it. https://github.com/square/okhttp/issues/358
*/
@Test
public void noTransparentGzipFor304NotModified() throws Exception {
server.enqueue(new MockResponse().clearHeaders().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED).addHeader("Content-Encoding: gzip"));
server.enqueue(new MockResponse().setBody("b"));
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, connection1.getResponseCode());
assertContent("", connection1);
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
assertEquals(HttpURLConnection.HTTP_OK, connection2.getResponseCode());
assertContent("b", connection2);
RecordedRequest requestA = server.takeRequest();
assertEquals(0, requestA.getSequenceNumber());
RecordedRequest requestB = server.takeRequest();
assertEquals(1, requestB.getSequenceNumber());
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class URLConnectionTest method postBodyRetransmittedAfterAuthorizationFail.
private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exception {
server.enqueue(new MockResponse().setResponseCode(401));
server.enqueue(new MockResponse());
String credential = Credentials.basic("jesse", "secret");
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build());
connection = urlFactory.open(server.url("/").url());
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(body.getBytes("UTF-8"));
outputStream.close();
assertEquals(200, connection.getResponseCode());
connection.getInputStream().close();
RecordedRequest recordedRequest1 = server.takeRequest();
assertEquals("POST", recordedRequest1.getMethod());
assertEquals(body, recordedRequest1.getBody().readUtf8());
assertNull(recordedRequest1.getHeader("Authorization"));
RecordedRequest recordedRequest2 = server.takeRequest();
assertEquals("POST", recordedRequest2.getMethod());
assertEquals(body, recordedRequest2.getBody().readUtf8());
assertEquals(credential, recordedRequest2.getHeader("Authorization"));
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class URLConnectionTest method testRedirect.
private void testRedirect(boolean temporary, String method) throws Exception {
MockResponse response1 = new MockResponse().setResponseCode(temporary ? HTTP_TEMP_REDIRECT : HTTP_PERM_REDIRECT).addHeader("Location: /page2");
if (!method.equals("HEAD")) {
response1.setBody("This page has moved!");
}
server.enqueue(response1);
server.enqueue(new MockResponse().setBody("Page 2"));
connection = urlFactory.open(server.url("/page1").url());
connection.setRequestMethod(method);
byte[] requestBody = { 'A', 'B', 'C', 'D' };
if (method.equals("POST")) {
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBody);
outputStream.close();
}
String response = readAscii(connection.getInputStream(), Integer.MAX_VALUE);
RecordedRequest page1 = server.takeRequest();
assertEquals(method + " /page1 HTTP/1.1", page1.getRequestLine());
if (method.equals("GET")) {
assertEquals("Page 2", response);
} else if (method.equals("HEAD")) {
assertEquals("", response);
} else {
// Methods other than GET/HEAD shouldn't follow the redirect
if (method.equals("POST")) {
assertTrue(connection.getDoOutput());
assertEquals("ABCD", page1.getBody().readUtf8());
}
assertEquals(1, server.getRequestCount());
assertEquals("This page has moved!", response);
return;
}
// GET/HEAD requests should have followed the redirect with the same method
assertFalse(connection.getDoOutput());
assertEquals(2, server.getRequestCount());
RecordedRequest page2 = server.takeRequest();
assertEquals(method + " /page2 HTTP/1.1", page2.getRequestLine());
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class URLConnectionTest method redirectWithAuthentication.
@Test
public void redirectWithAuthentication() throws Exception {
server2.enqueue(new MockResponse().setBody("Page 2"));
server.enqueue(new MockResponse().setResponseCode(401));
server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location: " + server2.url("/b").url()));
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(Credentials.basic("jesse", "secret"))).build());
assertContent("Page 2", urlFactory.open(server.url("/a").url()));
RecordedRequest redirectRequest = server2.takeRequest();
assertNull(redirectRequest.getHeader("Authorization"));
assertEquals("/b", redirectRequest.getPath());
}
Aggregations