Search in sources :

Example 31 with HttpServerExchange

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

the class SameSiteCookieHandlerTestCase method testNoneUACheckerEnabledAlthoughUAHeaderNotSet.

@Test
public void testNoneUACheckerEnabledAlthoughUAHeaderNotSet() throws IOException {
    DefaultServer.setRootHandler(new SameSiteCookieHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            exchange.setResponseCookie(new CookieImpl("foo", "bar"));
        }
    }, "None", "foo"));
    DefaultServer.startSSLServer();
    TestHttpClient client = new TestHttpClient() {

        // Here we need to get client instance that does not set ANY User-Agent header by default.
        @Override
        protected HttpParams createHttpParams() {
            HttpParams params = super.createHttpParams();
            params.removeParameter(CoreProtocolPNames.USER_AGENT);
            HttpConnectionParams.setSoTimeout(params, 30000);
            return params;
        }
    };
    client.setSSLContext(DefaultServer.getClientSSLContext());
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
        // Don't set any User-Agent header
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Header header = result.getFirstHeader("set-cookie");
        Assert.assertEquals("foo=bar; secure; SameSite=None", header.getValue());
        FileUtils.readFile(result.getEntity().getContent());
    } finally {
        client.getConnectionManager().shutdown();
        DefaultServer.stopSSLServer();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpParams(org.apache.http.params.HttpParams) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 32 with HttpServerExchange

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

the class SameSiteCookieHandlerTestCase method testAllCookies.

@Test
public void testAllCookies() throws IOException {
    DefaultServer.setRootHandler(new SameSiteCookieHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.setResponseCookie(new CookieImpl("foo", "bar"));
            exchange.setResponseCookie(new CookieImpl("baz", "qux"));
            exchange.setResponseCookie(new CookieImpl("test", "test"));
        }
    }, "Strict"));
    DefaultServer.startSSLServer();
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(DefaultServer.getClientSSLContext());
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Header[] headerArray = result.getHeaders("set-cookie");
        for (Header h : headerArray) {
            if (h.getValue().contains("foo")) {
                Assert.assertEquals("foo=bar; SameSite=Strict", h.getValue());
            }
            if (h.getValue().contains("baz")) {
                Assert.assertEquals("baz=qux; SameSite=Strict", h.getValue());
            }
            if (h.getValue().contains("test")) {
                Assert.assertEquals("test=test; SameSite=Strict", h.getValue());
            }
        }
        FileUtils.readFile(result.getEntity().getContent());
    } finally {
        client.getConnectionManager().shutdown();
        DefaultServer.stopSSLServer();
    }
}
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) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 33 with HttpServerExchange

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

the class SameSiteCookieHandlerTestCase method testNoneIncompatibleUA.

@Test
public void testNoneIncompatibleUA() throws IOException {
    DefaultServer.setRootHandler(new SameSiteCookieHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.setResponseCookie(new CookieImpl("foo", "bar"));
        }
    }, "None", "foo"));
    DefaultServer.startSSLServer();
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(DefaultServer.getClientSSLContext());
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
        // Chrome version whic is known to be incompatible with the `SameSite=None` attribute
        get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Header header = result.getFirstHeader("set-cookie");
        Assert.assertEquals("foo=bar", header.getValue());
        FileUtils.readFile(result.getEntity().getContent());
    } finally {
        client.getConnectionManager().shutdown();
        DefaultServer.stopSSLServer();
    }
}
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) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 34 with HttpServerExchange

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

the class BlockingWriteTimeoutHandlerTestCase method testWriteTimeout.

@Test
public void testWriteTimeout() throws InterruptedException {
    DefaultServer.setRootHandler(BlockingWriteTimeoutHandler.builder().nextHandler(new BlockingHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            // 1mb
            final int capacity = 1 * 1024 * 1024;
            final byte[] data = new byte[capacity];
            for (int i = 0; i < capacity; ++i) {
                data[i] = (byte) '*';
            }
            try {
                // Must write enough data that it's not buffered
                for (int i = 0; i < 20; i++) {
                    exchange.getOutputStream().write(data);
                }
            } catch (IOException e) {
                exception = e;
                errorLatch.countDown();
            }
        }
    })).writeTimeout(Duration.ofMillis(1)).build());
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL());
        try {
            HttpResponse result = client.execute(get);
            Assert.assertFalse("The result entity is buffered", result.getEntity().isRepeatable());
            InputStream content = result.getEntity().getContent();
            byte[] buffer = new byte[512];
            int r = 0;
            while ((r = content.read(buffer)) > 0) {
                Thread.sleep(200);
                if (exception != null) {
                    Assert.assertEquals(WriteTimeoutException.class, exception.getClass());
                    return;
                }
            }
            Assert.fail("Write did not time out");
        } catch (IOException e) {
            if (errorLatch.await(5, TimeUnit.SECONDS)) {
                Assert.assertEquals(WriteTimeoutException.class, exception.getClass());
            } else {
                Assert.fail("Write did not time out");
            }
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) WriteTimeoutException(org.xnio.channels.WriteTimeoutException) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) HttpServerExchange(io.undertow.server.HttpServerExchange) WriteTimeoutException(org.xnio.channels.WriteTimeoutException) Test(org.junit.Test)

Example 35 with HttpServerExchange

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

the class ExceptionHandlerTestCase method testExceptionMappers.

@Test
public void testExceptionMappers() throws IOException {
    HttpHandler pathHandler = Handlers.path().addExactPath("/", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("expected");
        }
    }).addExactPath("/exceptionParent", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            throw new ParentException();
        }
    }).addExactPath("/exceptionChild", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            throw new ChildException();
        }
    }).addExactPath("/exceptionAnotherChild", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            throw new AnotherChildException();
        }
    }).addExactPath("/illegalArgumentException", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            throw new IllegalArgumentException();
        }
    });
    HttpHandler exceptionHandler = Handlers.exceptionHandler(pathHandler).addExceptionHandler(ChildException.class, new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("child exception handled");
        }
    }).addExceptionHandler(ParentException.class, new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("parent exception handled");
        }
    }).addExceptionHandler(Throwable.class, new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("catch all throwables");
        }
    });
    DefaultServer.setRootHandler(exceptionHandler);
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("expected", HttpClientUtils.readResponse(result));
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/exceptionParent");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("parent exception handled", HttpClientUtils.readResponse(result));
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/exceptionChild");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("child exception handled", HttpClientUtils.readResponse(result));
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/exceptionAnotherChild");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("parent exception handled", HttpClientUtils.readResponse(result));
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/illegalArgumentException");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("catch all throwables", HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) 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)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)270 HttpHandler (io.undertow.server.HttpHandler)126 Test (org.junit.Test)109 IOException (java.io.IOException)87 UnitTest (io.undertow.testutils.category.UnitTest)45 BeforeClass (org.junit.BeforeClass)43 TestHttpClient (io.undertow.testutils.TestHttpClient)42 HttpGet (org.apache.http.client.methods.HttpGet)40 HttpResponse (org.apache.http.HttpResponse)37 HttpString (io.undertow.util.HttpString)36 Header (org.apache.http.Header)24 Undertow (io.undertow.Undertow)19 ByteBuffer (java.nio.ByteBuffer)19 Map (java.util.Map)16 SessionConfig (io.undertow.server.session.SessionConfig)15 Sender (io.undertow.io.Sender)14 ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)14 HeaderMap (io.undertow.util.HeaderMap)13 HeaderValues (io.undertow.util.HeaderValues)12 URI (java.net.URI)12