Search in sources :

Example 86 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class EncodingSelectionTestCase method testEncodingSelectionWithQValueAndPredicate.

@Test
public void testEncodingSelectionWithQValueAndPredicate() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        final EncodingHandler handler = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("compress", ContentEncodingProvider.IDENTITY, 100, Predicates.falsePredicate()).addEncodingHandler("bzip", ContentEncodingProvider.IDENTITY, 50)).setNext(new HttpHandler() {

            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                //we need some content to encode
                exchange.getResponseSender().send("hi");
            }
        });
        DefaultServer.setRootHandler(handler);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "bzip, compress;q=0.6");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Header[] header = result.getHeaders(HEADER);
        Assert.assertEquals("bzip", header[0].getValue());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "*;q=0.00");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.NOT_ACCEPTABLE, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "compress");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals(0, header.length);
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "*;q=0.00 bzip;q=0.3");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals("bzip", header[0].getValue());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "compress;q=0.1 bzip;q=0.05");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals("bzip", header[0].getValue());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "compress;q=0.1, bzip;q=1.000");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals("bzip", header[0].getValue());
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 87 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class EncodingSelectionTestCase method testEncodingSelectWithQValue.

/**
     * Tests encoding selection with a qvalue
     *
     * @throws IOException
     */
@Test
public void testEncodingSelectWithQValue() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        final EncodingHandler handler = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("compress", ContentEncodingProvider.IDENTITY, 100).addEncodingHandler("bzip", ContentEncodingProvider.IDENTITY, 50)).setNext(new HttpHandler() {

            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                //we need some content to encode
                exchange.getResponseSender().send("hi");
            }
        });
        DefaultServer.setRootHandler(handler);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "bzip, compress;q=0.6");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Header[] header = result.getHeaders(HEADER);
        Assert.assertEquals("bzip", header[0].getValue());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "*;q=0.00");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.NOT_ACCEPTABLE, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "*;q=0.00 bzip");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals("bzip", header[0].getValue());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "*;q=0.00 bzip;q=0.3");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals("bzip", header[0].getValue());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "compress;q=0.1 bzip;q=0.05");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals("compress", header[0].getValue());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "compress;q=0.1, bzip;q=1.000");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals("bzip", header[0].getValue());
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 88 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class EncodingSelectionTestCase method testBasicEncodingSelect.

/**
     * Tests encoding selection with no qvalue
     * <p/>
     * Also tests a lot of non standard formats for Accept-Encoding to make sure that
     * we are liberal in what we accept
     *
     * @throws IOException
     */
@Test
public void testBasicEncodingSelect() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        final EncodingHandler handler = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("compress", ContentEncodingProvider.IDENTITY, 50).addEncodingHandler("bzip", ContentEncodingProvider.IDENTITY, 100)).setNext(new HttpHandler() {

            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                //we need some content to encode
                exchange.getResponseSender().send("hi");
            }
        });
        DefaultServer.setRootHandler(handler);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Header[] header = result.getHeaders(HEADER);
        Assert.assertEquals(0, header.length);
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "bzip");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals("bzip", header[0].getValue());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "bzip compress identity someOtherEncoding");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals("bzip", header[0].getValue());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, " compress, identity, someOtherEncoding,  bzip  , ");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals("bzip", header[0].getValue());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "boo; compress, identity; someOtherEncoding,   , ");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals("compress", header[0].getValue());
        HttpClientUtils.readResponse(result);
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.setHeader(Headers.ACCEPT_ENCODING_STRING, "boo; compress; identity; someOtherEncoding,   , ");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        header = result.getHeaders(HEADER);
        Assert.assertEquals("compress", header[0].getValue());
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 89 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class GzipContentEncodingTestCase method setup.

@BeforeClass
public static void setup() {
    final EncodingHandler handler = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("gzip", new GzipEncodingProvider(), 50, Predicates.parse("max-content-size[5]"))).setNext(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, message.length() + "");
            exchange.getResponseSender().send(message, IoCallback.END_EXCHANGE);
        }
    });
    DefaultServer.setRootHandler(handler);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 90 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class RequestContentEncodingTestCase method setup.

@BeforeClass
public static void setup() {
    final ContentEncodingRepository contentEncodingRepository = new ContentEncodingRepository().addEncodingHandler("deflate", new DeflateEncodingProvider(), 50).addEncodingHandler("gzip", new GzipEncodingProvider(), 60);
    final EncodingHandler encode = new EncodingHandler(contentEncodingRepository).setNext(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, message.length() + "");
            exchange.getResponseSender().send(message, IoCallback.END_EXCHANGE);
        }
    });
    final EncodingHandler wrappedEncode = new EncodingHandler(contentEncodingRepository).setNext(encode);
    final HttpHandler decode = new RequestEncodingHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getRequestReceiver().receiveFullBytes(new Receiver.FullBytesCallback() {

                @Override
                public void handle(HttpServerExchange exchange, byte[] message) {
                    exchange.getResponseSender().send(ByteBuffer.wrap(message));
                }
            });
        }
    }).addEncoding("deflate", InflatingStreamSourceConduit.WRAPPER).addEncoding("gzip", GzipStreamSourceConduit.WRAPPER);
    final HttpHandler wrappedDecode = new RequestEncodingHandler(decode).addEncoding("deflate", InflatingStreamSourceConduit.WRAPPER).addEncoding("gzip", GzipStreamSourceConduit.WRAPPER);
    PathHandler pathHandler = new PathHandler();
    pathHandler.addPrefixPath("/encode", wrappedEncode);
    pathHandler.addPrefixPath("/decode", wrappedDecode);
    DefaultServer.setRootHandler(pathHandler);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) PathHandler(io.undertow.server.handlers.PathHandler) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Aggregations

HttpHandler (io.undertow.server.HttpHandler)126 HttpServerExchange (io.undertow.server.HttpServerExchange)72 IOException (java.io.IOException)54 BeforeClass (org.junit.BeforeClass)35 Test (org.junit.Test)25 TestHttpClient (io.undertow.testutils.TestHttpClient)20 HttpResponse (org.apache.http.HttpResponse)20 HttpGet (org.apache.http.client.methods.HttpGet)19 PathHandler (io.undertow.server.handlers.PathHandler)17 HttpString (io.undertow.util.HttpString)15 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)13 Undertow (io.undertow.Undertow)12 ArrayList (java.util.ArrayList)11 HandlerWrapper (io.undertow.server.HandlerWrapper)9 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)9 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)9 Header (org.apache.http.Header)9 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)8 AuthenticationMechanism (io.undertow.security.api.AuthenticationMechanism)7 AuthenticationCallHandler (io.undertow.security.handlers.AuthenticationCallHandler)7