Search in sources :

Example 21 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange 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));
}
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 22 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange 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());
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpExchange(org.eclipse.jetty.client.HttpExchange) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 23 with HttpExchange

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

the class HttpReceiverOverHTTPTest method test_Receive_BadResponse.

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

Example 24 with HttpExchange

use of org.eclipse.jetty.client.HttpExchange in project openhab1-addons by openhab.

the class FritzahaWebInterface method asyncGet.

/**
     * Sends an HTTP GET request using the asynchronous client
     * 
     * @param Path
     *            Path of the requested resource
     * @param Args
     *            Arguments for the request
     * @param Callback
     *            Callback to handle the response with
     */
public HttpExchange asyncGet(String path, String args, FritzahaCallback callback) {
    if (!isAuthenticated()) {
        authenticate();
    }
    HttpExchange getExchange = new FritzahaContentExchange(callback);
    getExchange.setMethod("GET");
    getExchange.setURL(getURL(path, addSID(args)));
    try {
        asyncclient.send(getExchange);
    } catch (IOException e) {
        logger.error("An I/O error occurred while sending the GET request " + getURL(path, addSID(args)));
        return null;
    }
    logger.debug("GETting URL " + getURL(path, addSID(args)));
    return getExchange;
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange) IOException(java.io.IOException)

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