Search in sources :

Example 6 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange in project jetty.project by eclipse.

the class HttpReceiverOverHTTPTest method newExchange.

protected HttpExchange newExchange() {
    HttpRequest request = (HttpRequest) client.newRequest("http://localhost");
    FutureResponseListener listener = new FutureResponseListener(request);
    HttpExchange exchange = new HttpExchange(destination, request, Collections.<Response.ResponseListener>singletonList(listener));
    boolean associated = connection.getHttpChannel().associate(exchange);
    Assert.assertTrue(associated);
    exchange.requestComplete(null);
    exchange.terminateRequest();
    return exchange;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) Response(org.eclipse.jetty.client.api.Response) HttpExchange(org.eclipse.jetty.client.HttpExchange) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Example 7 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange in project jetty.project by eclipse.

the class HttpReceiverOverHTTPTest method test_Receive_ResponseContent_EarlyEOF.

@Test
public void test_Receive_ResponseContent_EarlyEOF() throws Exception {
    String content1 = "0123456789";
    String content2 = "ABCDEF";
    endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: " + (content1.length() + content2.length()) + "\r\n" + "\r\n" + content1);
    HttpExchange exchange = newExchange();
    FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
    connection.getHttpChannel().receive();
    endPoint.addInputEOF();
    connection.getHttpChannel().receive();
    try {
        listener.get(5, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof EOFException);
    }
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange) EOFException(java.io.EOFException) ExecutionException(java.util.concurrent.ExecutionException) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 8 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange in project jetty.project by eclipse.

the class HttpReceiverOverHTTPTest method test_Receive_ResponseContent.

@Test
public void test_Receive_ResponseContent() throws Exception {
    String content = "0123456789ABCDEF";
    endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: " + content.length() + "\r\n" + "\r\n" + content);
    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(String.valueOf(content.length()), headers.get(HttpHeader.CONTENT_LENGTH));
    String received = listener.getContentAsString(StandardCharsets.UTF_8);
    Assert.assertEquals(content, received);
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpFields(org.eclipse.jetty.http.HttpFields) HttpExchange(org.eclipse.jetty.client.HttpExchange) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 9 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange in project jetty.project by eclipse.

the class HttpReceiverOverHTTPTest method test_Receive_ResponseContent_IdleTimeout.

@Test
public void test_Receive_ResponseContent_IdleTimeout() throws Exception {
    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();
    // ByteArrayEndPoint has an idle timeout of 0 by default,
    // so to simulate an idle timeout is enough to wait a bit.
    Thread.sleep(100);
    connection.onIdleExpired();
    try {
        listener.get(5, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof TimeoutException);
    }
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange) ExecutionException(java.util.concurrent.ExecutionException) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 10 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange in project jetty.project by eclipse.

the class HttpChannelOverFCGI method send.

@Override
public void send() {
    HttpExchange exchange = getHttpExchange();
    if (exchange != null) {
        version = exchange.getRequest().getVersion();
        idle.onOpen();
        sender.send(exchange);
    }
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange)

Aggregations

HttpExchange (org.eclipse.jetty.client.HttpExchange)24 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)7 Response (org.eclipse.jetty.client.api.Response)6 Test (org.junit.Test)6 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)3 HttpResponse (org.eclipse.jetty.client.HttpResponse)3 HttpFields (org.eclipse.jetty.http.HttpFields)3 EOFException (java.io.EOFException)2 HttpRequest (org.eclipse.jetty.client.HttpRequest)2 HttpResponseException (org.eclipse.jetty.client.HttpResponseException)2 MetaData (org.eclipse.jetty.http.MetaData)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ByteBuffer (java.nio.ByteBuffer)1 TimeoutException (java.util.concurrent.TimeoutException)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1