use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testServer401.
@Test
public void testServer401() throws Exception {
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setStatus(HttpStatus.UNAUTHORIZED_401);
response.setHeader(HttpHeader.WWW_AUTHENTICATE.asString(), "Basic realm=\"test\"");
}
});
final AtomicBoolean transformed = new AtomicBoolean();
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new AfterContentTransformer() {
@Override
public boolean transform(Source source, Sink sink) throws IOException {
transformed.set(true);
return false;
}
};
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.UNAUTHORIZED_401, response.getStatus());
Assert.assertFalse(transformed.get());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testAfterContentTransformerClosingFilesOnServerResponseException.
@Test
public void testAfterContentTransformerClosingFilesOnServerResponseException() throws Exception {
final Path targetTestsDir = prepareTargetTestsDir();
final CountDownLatch serviceLatch = new CountDownLatch(1);
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
response.setContentLength(2);
// Send only part of the content.
OutputStream output = response.getOutputStream();
output.write('x');
output.flush();
serviceLatch.countDown();
}
});
final CountDownLatch destroyLatch = new CountDownLatch(1);
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new AfterContentTransformer() {
{
setOverflowDirectory(targetTestsDir);
setMaxInputBufferSize(0);
setMaxOutputBufferSize(0);
}
@Override
public boolean transform(Source source, Sink sink) throws IOException {
IO.copy(source.getInputStream(), sink.getOutputStream());
return true;
}
@Override
public void destroy() {
super.destroy();
destroyLatch.countDown();
}
};
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertTrue(serviceLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
Assert.assertEquals(HttpStatus.BAD_GATEWAY_502, response.getStatus());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class WebSocketUpgradeRequest method onComplete.
@Override
public void onComplete(Result result) {
if (LOG.isDebugEnabled()) {
LOG.debug("onComplete() - {}", result);
}
URI requestURI = result.getRequest().getURI();
Response response = result.getResponse();
int responseStatusCode = response.getStatus();
String responseLine = responseStatusCode + " " + response.getReason();
if (result.isFailed()) {
if (LOG.isDebugEnabled()) {
if (result.getFailure() != null)
LOG.debug("General Failure", result.getFailure());
if (result.getRequestFailure() != null)
LOG.debug("Request Failure", result.getRequestFailure());
if (result.getResponseFailure() != null)
LOG.debug("Response Failure", result.getResponseFailure());
}
Throwable failure = result.getFailure();
if ((failure instanceof java.net.ConnectException) || (failure instanceof UpgradeException)) {
// handle as-is
handleException(failure);
} else {
// wrap in UpgradeException
handleException(new UpgradeException(requestURI, responseStatusCode, responseLine, failure));
}
}
if (responseStatusCode != HttpStatus.SWITCHING_PROTOCOLS_101) {
// Failed to upgrade (other reason)
handleException(new UpgradeException(requestURI, responseStatusCode, responseLine));
}
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpReceiverOverHTTPTest method test_Receive_NoResponseContent.
@Test
public void test_Receive_NoResponseContent() throws Exception {
endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: 0\r\n" + "\r\n");
HttpExchange exchange = newExchange();
FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
connection.getHttpChannel().receive();
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("OK", response.getReason());
Assert.assertSame(HttpVersion.HTTP_1_1, response.getVersion());
HttpFields headers = response.getHeaders();
Assert.assertNotNull(headers);
Assert.assertEquals(1, headers.size());
Assert.assertEquals("0", headers.get(HttpHeader.CONTENT_LENGTH));
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpReceiverOverHTTPTest method test_FillInterested_RacingWith_BufferRelease.
@Test
public void test_FillInterested_RacingWith_BufferRelease() throws Exception {
connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<>()) {
@Override
protected HttpChannelOverHTTP newHttpChannel() {
return new HttpChannelOverHTTP(this) {
@Override
protected HttpReceiverOverHTTP newHttpReceiver() {
return new HttpReceiverOverHTTP(this) {
@Override
protected void fillInterested() {
// Verify that the buffer has been released
// before fillInterested() is called.
Assert.assertNull(getResponseBuffer());
// Fill the endpoint so receive is called again.
endPoint.addInput("X");
super.fillInterested();
}
};
}
};
}
};
endPoint.setConnection(connection);
// Partial response to trigger the call to fillInterested().
endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 1\r\n" + "\r\n");
HttpExchange exchange = newExchange();
FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
connection.getHttpChannel().receive();
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
}
Aggregations