output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
// Expect 407 from the CONNECT request
SimpleHttpResponse response = readResponse(input);
Assert.assertEquals("407", response.getCode());
Assert.assertTrue(response.getHeaders().containsKey("Proxy-Authenticate".toLowerCase(Locale.ENGLISH)));
// Socket should be closed
Assert.assertEquals(-1, input.read());
}
// Try with authentication
String credentials = "Basic " + B64Code.encode("test:test");
request = "" +
"CONNECT " + hostPort + " HTTP/1.1\r\n" +
"Host: " + hostPort + "\r\n" +
"Proxy-Authorization: " + credentials + "\r\n" +
"\r\n";
try (Socket socket = newSocket())
{
OutputStream output = socket.getOutputStream();
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
// Expect 200 from the CONNECT request
SimpleHttpResponse response = readResponse(input);
Assert.assertEquals("200", response.getCode());
request = "" +
"GET /echo" + " HTTP/1.1\r\n" +
"Host: " + hostPort + "\r\n" +
"\r\n";
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
response = readResponse(input);
Assert.assertEquals("200", response.getCode());
Assert.assertEquals("GET /echo", response.getBody());
}
}