Search in sources :

Example 71 with HttpServerExchange

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

the class SimpleSSLTestCase method testNonPersistentConnections.

@Test
public void testNonPersistentConnections() throws IOException, GeneralSecurityException {
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(HttpString.tryFromString("scheme"), exchange.getRequestScheme());
            exchange.getResponseHeaders().put(Headers.CONNECTION, "close");
            exchange.endExchange();
        }
    });
    DefaultServer.startSSLServer();
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(DefaultServer.getClientSSLContext());
    try {
        for (int i = 0; i < 5; ++i) {
            HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
            HttpResponse result = client.execute(get);
            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
            Header[] header = result.getHeaders("scheme");
            Assert.assertEquals("https", header[0].getValue());
            HttpClientUtils.readResponse(result);
        }
    } 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) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 72 with HttpServerExchange

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

the class SimpleSSLTestCase method simpleSSLTestCase.

@Test
public void simpleSSLTestCase() throws IOException, GeneralSecurityException {
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(HttpString.tryFromString("scheme"), exchange.getRequestScheme());
            exchange.endExchange();
        }
    });
    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[] header = result.getHeaders("scheme");
        Assert.assertEquals("https", header[0].getValue());
    } 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) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 73 with HttpServerExchange

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

the class FormAuthTestCase method setRootHandler.

@Override
protected void setRootHandler(HttpHandler current) {
    final PredicateHandler handler = new PredicateHandler(Predicates.path("/login"), new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("Login Page");
        }
    }, current);
    super.setRootHandler(new SessionAttachmentHandler(handler, new InMemorySessionManager("test"), new SessionCookieConfig()));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) PredicateHandler(io.undertow.server.handlers.PredicateHandler) ProtocolException(org.apache.http.ProtocolException) IOException(java.io.IOException) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager)

Example 74 with HttpServerExchange

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

the class SimpleParserTestCase method testServletPathWithPathParams.

@Test
public void testServletPathWithPathParams() throws BadRequestException {
    byte[] in = "GET /servletContext/aa/b;foo=bar;mysessioncookie=mSwrYUX8_e3ukAylNMkg3oMRglB4-YjxqeWqXQsI HTTP/1.1\r\n\r\n".getBytes();
    ParseState context = new ParseState(10);
    HttpServerExchange result = new HttpServerExchange(null);
    HttpRequestParser.instance(OptionMap.create(UndertowOptions.ALLOW_ENCODED_SLASH, true)).handle(ByteBuffer.wrap(in), context, result);
    Assert.assertSame(Methods.GET, result.getRequestMethod());
    Assert.assertEquals("/servletContext/aa/b;foo=bar;mysessioncookie=mSwrYUX8_e3ukAylNMkg3oMRglB4-YjxqeWqXQsI", result.getRequestURI());
    Assert.assertEquals("/servletContext/aa/b", result.getRequestPath());
    Assert.assertEquals(2, result.getPathParameters().size());
    Assert.assertEquals("bar", result.getPathParameters().get("foo").getFirst());
    Assert.assertEquals("mSwrYUX8_e3ukAylNMkg3oMRglB4-YjxqeWqXQsI", result.getPathParameters().get("mysessioncookie").getFirst());
    Assert.assertSame(Protocols.HTTP_1_1, result.getProtocol());
    Assert.assertFalse(result.isHostIncludedInRequestURI());
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 75 with HttpServerExchange

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

the class SimpleParserTestCase method testCanonicalPath.

@Test
public void testCanonicalPath() throws BadRequestException {
    byte[] in = "GET http://www.somehost.net/somepath HTTP/1.1\nHost: \t www.somehost.net\nOtherHeader:\tsome\n \t  value\n\r\n".getBytes();
    final ParseState context = new ParseState(5);
    HttpServerExchange result = new HttpServerExchange(null);
    HttpRequestParser.instance(OptionMap.EMPTY).handle(ByteBuffer.wrap(in), context, result);
    Assert.assertEquals("/somepath", result.getRelativePath());
    Assert.assertEquals("http://www.somehost.net/somepath", result.getRequestURI());
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)277 HttpHandler (io.undertow.server.HttpHandler)127 Test (org.junit.Test)109 IOException (java.io.IOException)90 UnitTest (io.undertow.testutils.category.UnitTest)45 BeforeClass (org.junit.BeforeClass)44 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 SessionConfig (io.undertow.server.session.SessionConfig)16 Map (java.util.Map)16 Sender (io.undertow.io.Sender)15 ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)14 HeaderMap (io.undertow.util.HeaderMap)13 URI (java.net.URI)13 HeaderValues (io.undertow.util.HeaderValues)12