use of okhttp3.Connection in project okhttp by square.
the class Http2ConnectionTest method sendGoAway.
@Test
public void sendGoAway() throws Exception {
// write the mocking script
peer.sendFrame().settings(new Settings());
// ACK
peer.acceptFrame();
// SYN_STREAM 1
peer.acceptFrame();
// GOAWAY
peer.acceptFrame();
// PING
peer.acceptFrame();
// Should be ignored!
peer.sendFrame().synStream(false, 2, 0, headerEntries("b", "b"));
peer.sendFrame().ping(true, 1, 0);
peer.play();
// play it back
Http2Connection connection = connect(peer);
connection.newStream(headerEntries("a", "android"), false);
Ping ping = connection.ping();
connection.shutdown(ErrorCode.PROTOCOL_ERROR);
assertEquals(1, connection.openStreamCount());
// Prevent the peer from exiting prematurely.
ping.roundTripTime();
// verify the peer received what was expected
InFrame synStream1 = peer.takeFrame();
assertEquals(Http2.TYPE_HEADERS, synStream1.type);
InFrame pingFrame = peer.takeFrame();
assertEquals(Http2.TYPE_PING, pingFrame.type);
InFrame goaway = peer.takeFrame();
assertEquals(Http2.TYPE_GOAWAY, goaway.type);
assertEquals(0, goaway.streamId);
assertEquals(ErrorCode.PROTOCOL_ERROR, goaway.errorCode);
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method testResponseRedirectedWithPost.
private void testResponseRedirectedWithPost(int redirectCode, TransferKind transferKind) throws Exception {
server.enqueue(new MockResponse().setResponseCode(redirectCode).addHeader("Location: /page2").setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("Page 2"));
connection = urlFactory.open(server.url("/page1").url());
connection.setDoOutput(true);
transferKind.setForRequest(connection, 4);
byte[] requestBody = { 'A', 'B', 'C', 'D' };
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBody);
outputStream.close();
assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
assertTrue(connection.getDoOutput());
RecordedRequest page1 = server.takeRequest();
assertEquals("POST /page1 HTTP/1.1", page1.getRequestLine());
assertEquals("ABCD", page1.getBody().readUtf8());
RecordedRequest page2 = server.takeRequest();
assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method connectionCloseInRequest.
@Test
public void connectionCloseInRequest() throws IOException, InterruptedException {
// server doesn't honor the connection: close header!
server.enqueue(new MockResponse());
server.enqueue(new MockResponse());
HttpURLConnection a = urlFactory.open(server.url("/").url());
a.setRequestProperty("Connection", "close");
assertEquals(200, a.getResponseCode());
HttpURLConnection b = urlFactory.open(server.url("/").url());
assertEquals(200, b.getResponseCode());
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals("When connection: close is used, each request should get its own connection", 0, server.takeRequest().getSequenceNumber());
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method redirectedPostStripsRequestBodyHeaders.
@Test
public void redirectedPostStripsRequestBodyHeaders() throws Exception {
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /page2"));
server.enqueue(new MockResponse().setBody("Page 2"));
connection = urlFactory.open(server.url("/page1").url());
connection.setDoOutput(true);
connection.addRequestProperty("Content-Length", "4");
connection.addRequestProperty("Content-Type", "text/plain; charset=utf-8");
connection.addRequestProperty("Transfer-Encoding", "identity");
OutputStream outputStream = connection.getOutputStream();
outputStream.write("ABCD".getBytes("UTF-8"));
outputStream.close();
assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
assertEquals("POST /page1 HTTP/1.1", server.takeRequest().getRequestLine());
RecordedRequest page2 = server.takeRequest();
assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
assertNull(page2.getHeader("Content-Length"));
assertNull(page2.getHeader("Content-Type"));
assertNull(page2.getHeader("Transfer-Encoding"));
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method connectViaHttpsReusingConnectionsDifferentFactories.
@Test
public void connectViaHttpsReusingConnectionsDifferentFactories() throws Exception {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
server.enqueue(new MockResponse().setBody("another response via HTTPS"));
// install a custom SSL socket factory so the server can be authorized
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
assertContent("this response comes via HTTPS", connection1);
SSLContext sslContext2 = SSLContext.getInstance("TLS");
sslContext2.init(null, null, null);
SSLSocketFactory sslSocketFactory2 = sslContext2.getSocketFactory();
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
X509TrustManager trustManager = (X509TrustManager) trustManagerFactory.getTrustManagers()[0];
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslSocketFactory2, trustManager).build());
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
try {
readAscii(connection2.getInputStream(), Integer.MAX_VALUE);
fail("without an SSL socket factory, the connection should fail");
} catch (SSLException expected) {
}
}
Aggregations