use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class URLConnectionTest method reusedConnectionFailsWithPost.
private void reusedConnectionFailsWithPost(TransferKind transferKind, int requestSize) throws Exception {
server.enqueue(new MockResponse().setBody("A").setSocketPolicy(DISCONNECT_AT_END));
server.enqueue(new MockResponse().setBody("B"));
server.enqueue(new MockResponse().setBody("C"));
assertContent("A", urlFactory.open(server.url("/a").url()));
// Give the server time to disconnect.
Thread.sleep(500);
// If the request body is larger than OkHttp's replay buffer, the failure may still occur.
byte[] requestBody = new byte[requestSize];
new Random(0).nextBytes(requestBody);
for (int j = 0; j < 2; j++) {
try {
connection = urlFactory.open(server.url("/b").url());
connection.setRequestMethod("POST");
transferKind.setForRequest(connection, requestBody.length);
for (int i = 0; i < requestBody.length; i += 1024) {
connection.getOutputStream().write(requestBody, i, 1024);
}
connection.getOutputStream().close();
assertContent("B", connection);
break;
} catch (IOException socketException) {
// If there's a socket exception, this must have a streamed request body.
assertEquals(0, j);
assertTrue(transferKind == TransferKind.CHUNKED || transferKind == TransferKind.FIXED_LENGTH);
}
}
RecordedRequest requestA = server.takeRequest();
assertEquals("/a", requestA.getPath());
RecordedRequest requestB = server.takeRequest();
assertEquals("/b", requestB.getPath());
assertEquals(Arrays.toString(requestBody), Arrays.toString(requestB.getBody().readByteArray()));
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class URLConnectionTest method proxyAuthenticateOnConnect.
@Test
public void proxyAuthenticateOnConnect() throws Exception {
Authenticator.setDefault(new RecordingAuthenticator());
server.useHttps(sslClient.socketFactory, true);
server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
server.enqueue(new MockResponse().setSocketPolicy(UPGRADE_TO_SSL_AT_END).clearHeaders());
server.enqueue(new MockResponse().setBody("A"));
urlFactory.setClient(urlFactory.client().newBuilder().proxyAuthenticator(new JavaNetAuthenticator()).proxy(server.toProxyAddress()).sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
URL url = new URL("https://android.com/foo");
connection = urlFactory.open(url);
assertContent("A", connection);
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("Basic " + RecordingAuthenticator.BASE_64_CREDENTIALS, 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 URLConnectionTest method testServerClosesOutput.
private void testServerClosesOutput(SocketPolicy socketPolicy) throws Exception {
server.enqueue(new MockResponse().setBody("This connection won't pool properly").setSocketPolicy(socketPolicy));
MockResponse responseAfter = new MockResponse().setBody("This comes after a busted connection");
server.enqueue(responseAfter);
// Enqueue 2x because the broken connection may be reused.
server.enqueue(responseAfter);
HttpURLConnection connection1 = urlFactory.open(server.url("/a").url());
connection1.setReadTimeout(100);
assertContent("This connection won't pool properly", connection1);
assertEquals(0, server.takeRequest().getSequenceNumber());
// Give the server time to enact the socket policy if it's one that could happen after the
// client has received the response.
Thread.sleep(500);
HttpURLConnection connection2 = urlFactory.open(server.url("/b").url());
connection2.setReadTimeout(100);
assertContent("This comes after a busted connection", connection2);
// Check that a fresh connection was created, either immediately or after attempting reuse.
RecordedRequest requestAfter = server.takeRequest();
if (server.getRequestCount() == 3) {
// The failure consumed a response.
requestAfter = server.takeRequest();
}
// sequence number 0 means the HTTP socket connection was not reused
assertEquals(0, requestAfter.getSequenceNumber());
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class OAuthSessionFactory method dispatch.
/** When the browser hits the redirect URL, use the provided code to ask Slack for a session. */
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
HttpUrl requestUrl = mockWebServer.url(request.getPath());
String code = requestUrl.queryParameter("code");
String stateString = requestUrl.queryParameter("state");
ByteString state = stateString != null ? ByteString.decodeBase64(stateString) : null;
Listener listener;
synchronized (this) {
listener = listeners.get(state);
}
if (code == null || listener == null) {
return new MockResponse().setResponseCode(404).setBody("unexpected request");
}
try {
OAuthSession session = slackApi.exchangeCode(code, redirectUrl());
listener.sessionGranted(session);
} catch (IOException e) {
return new MockResponse().setResponseCode(400).setBody("code exchange failed: " + e.getMessage());
}
synchronized (this) {
listeners.remove(state);
}
// Success!
return new MockResponse().setResponseCode(302).addHeader("Location", "https://twitter.com/CuteEmergency/status/789457462864863232");
}
use of okhttp3.mockwebserver.RecordedRequest in project retrofit by square.
the class JacksonConverterFactoryTest method anInterface.
@Test
public void anInterface() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setBody("{\"name\":\"value\"}"));
Call<AnInterface> call = service.anInterface(new AnImplementation("value"));
Response<AnInterface> response = call.execute();
AnInterface body = response.body();
assertThat(body.getName()).isEqualTo("value");
RecordedRequest request = server.takeRequest();
assertThat(request.getBody().readUtf8()).isEqualTo("{\"name\":\"value\"}");
assertThat(request.getHeader("Content-Type")).isEqualTo("application/json; charset=UTF-8");
}
Aggregations