use of java.net.ProtocolException in project okhttp by square.
the class Http2Server method run.
private void run() throws Exception {
ServerSocket serverSocket = new ServerSocket(8888);
serverSocket.setReuseAddress(true);
while (true) {
Socket socket = null;
try {
socket = serverSocket.accept();
SSLSocket sslSocket = doSsl(socket);
String protocolString = Platform.get().getSelectedProtocol(sslSocket);
Protocol protocol = protocolString != null ? Protocol.get(protocolString) : null;
if (protocol != Protocol.HTTP_2) {
throw new ProtocolException("Protocol " + protocol + " unsupported");
}
Http2Connection connection = new Http2Connection.Builder(false).socket(sslSocket).listener(this).build();
connection.start();
} catch (IOException e) {
logger.log(Level.INFO, "Http2Server connection failure: " + e);
Util.closeQuietly(socket);
} catch (Exception e) {
logger.log(Level.WARNING, "Http2Server unexpected failure", e);
Util.closeQuietly(socket);
}
}
}
use of java.net.ProtocolException in project okhttp by square.
the class URLConnectionTest method doesNotAttemptAuthorization21Times.
@Test
public void doesNotAttemptAuthorization21Times() throws Exception {
for (int i = 0; i < 21; i++) {
server.enqueue(new MockResponse().setResponseCode(401));
}
String credential = Credentials.basic("jesse", "peanutbutter");
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build());
connection = urlFactory.open(server.url("/").url());
try {
connection.getInputStream();
fail();
} catch (ProtocolException expected) {
assertEquals(401, connection.getResponseCode());
assertEquals("Too many follow-up requests: 21", expected.getMessage());
}
}
use of java.net.ProtocolException in project okhttp by square.
the class URLConnectionTest method contentDisagreesWithChunkedHeaderBodyTooShort.
@Test
public void contentDisagreesWithChunkedHeaderBodyTooShort() throws IOException {
MockResponse mockResponse = new MockResponse();
mockResponse.setChunkedBody("abcde", 5);
Buffer truncatedBody = new Buffer();
Buffer fullBody = mockResponse.getBody();
truncatedBody.write(fullBody, fullBody.indexOf((byte) 'e'));
mockResponse.setBody(truncatedBody);
mockResponse.clearHeaders();
mockResponse.addHeader("Transfer-encoding: chunked");
mockResponse.setSocketPolicy(DISCONNECT_AT_END);
server.enqueue(mockResponse);
try {
readAscii(urlFactory.open(server.url("/").url()).getInputStream(), 5);
fail();
} catch (ProtocolException expected) {
}
}
use of java.net.ProtocolException in project okhttp by square.
the class URLConnectionTest method getResponseCodeNoResponseBody.
@Test
public void getResponseCodeNoResponseBody() throws Exception {
server.enqueue(new MockResponse().addHeader("abc: def"));
URL url = server.url("/").url();
HttpURLConnection conn = urlFactory.open(url);
conn.setDoInput(false);
assertEquals("def", conn.getHeaderField("abc"));
assertEquals(200, conn.getResponseCode());
try {
conn.getInputStream();
fail();
} catch (ProtocolException expected) {
}
}
use of java.net.ProtocolException in project okhttp by square.
the class URLConnectionTest method getOutputStreamOnGetFails.
@Test
public void getOutputStreamOnGetFails() throws Exception {
server.enqueue(new MockResponse());
connection = urlFactory.open(server.url("/").url());
try {
connection.getOutputStream();
fail();
} catch (ProtocolException expected) {
}
connection.getInputStream().close();
}
Aggregations