use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CallTest method rangeHeaderPreventsAutomaticGzip.
@Test
public void rangeHeaderPreventsAutomaticGzip() throws Exception {
Buffer gzippedBody = gzip("abcabcabc");
// Enqueue a gzipped response. Our request isn't expecting it, but that's okay.
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_PARTIAL).setBody(gzippedBody).addHeader("Content-Encoding: gzip").addHeader("Content-Range: bytes 0-" + (gzippedBody.size() - 1)));
// Make a range request.
Request request = new Request.Builder().url(server.url("/")).header("Range", "bytes=0-").build();
Call call = client.newCall(request);
// The response is not decompressed.
Response response = call.execute();
assertEquals("gzip", response.header("Content-Encoding"));
assertEquals(gzippedBody.snapshot(), response.body().source().readByteString());
// The request did not offer gzip support.
RecordedRequest recordedRequest = server.takeRequest();
assertNull(recordedRequest.getHeader("Accept-Encoding"));
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CallTest method canceledBeforeResponseReadSignalsOnFailure.
@Test
public void canceledBeforeResponseReadSignalsOnFailure() throws Exception {
Request requestA = new Request.Builder().url(server.url("/a")).build();
final Call call = client.newCall(requestA);
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
call.cancel();
return new MockResponse().setBody("A");
}
});
call.enqueue(callback);
assertEquals("/a", server.takeRequest().getPath());
callback.await(requestA.url()).assertFailure("Canceled", "stream was reset: CANCEL", "Socket closed");
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CallTest method proxyAuthenticateOnConnect.
/** Respond to a proxy authorization challenge. */
@Test
public void proxyAuthenticateOnConnect() throws Exception {
server.useHttps(sslClient.socketFactory, true);
server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
server.enqueue(new MockResponse().setBody("response body"));
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).proxy(server.toProxyAddress()).proxyAuthenticator(new RecordingOkAuthenticator("password")).hostnameVerifier(new RecordingHostnameVerifier()).build();
Request request = new Request.Builder().url("https://android.com/foo").build();
Response response = client.newCall(request).execute();
assertEquals("response body", response.body().string());
RecordedRequest connect1 = server.takeRequest();
assertEquals("CONNECT android.com:443 HTTP/1.1", connect1.getRequestLine());
assertNull(connect1.getHeader("Proxy-Authorization"));
RecordedRequest connect2 = server.takeRequest();
assertEquals("CONNECT android.com:443 HTTP/1.1", connect2.getRequestLine());
assertEquals("password", connect2.getHeader("Proxy-Authorization"));
RecordedRequest get = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
assertNull(get.getHeader("Proxy-Authorization"));
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CallTest method put.
@Test
public void put() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));
Request request = new Request.Builder().url(server.url("/")).put(RequestBody.create(MediaType.parse("text/plain"), "def")).build();
executeSynchronously(request).assertCode(200).assertBody("abc");
RecordedRequest recordedRequest = server.takeRequest();
assertEquals("PUT", recordedRequest.getMethod());
assertEquals("def", recordedRequest.getBody().readUtf8());
assertEquals("3", recordedRequest.getHeader("Content-Length"));
assertEquals("text/plain; charset=utf-8", recordedRequest.getHeader("Content-Type"));
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CallTest method postBodyRetransmittedOnFailureRecovery.
@Test
public void postBodyRetransmittedOnFailureRecovery() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AFTER_REQUEST));
server.enqueue(new MockResponse().setBody("def"));
// Seed the connection pool so we have something that can fail.
Request request1 = new Request.Builder().url(server.url("/")).build();
Response response1 = client.newCall(request1).execute();
assertEquals("abc", response1.body().string());
Request request2 = new Request.Builder().url(server.url("/")).post(RequestBody.create(MediaType.parse("text/plain"), "body!")).build();
Response response2 = client.newCall(request2).execute();
assertEquals("def", response2.body().string());
RecordedRequest get = server.takeRequest();
assertEquals(0, get.getSequenceNumber());
RecordedRequest post1 = server.takeRequest();
assertEquals("body!", post1.getBody().readUtf8());
assertEquals(1, post1.getSequenceNumber());
RecordedRequest post2 = server.takeRequest();
assertEquals("body!", post2.getBody().readUtf8());
assertEquals(0, post2.getSequenceNumber());
}
Aggregations