Search in sources :

Example 6 with TestHttpClient

use of io.undertow.testutils.TestHttpClient 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)

Example 7 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class LotsOfHeadersRequestTestCase method testLotsOfHeadersInRequest_Default_BadRequest.

@Test
public void testLotsOfHeadersInRequest_Default_BadRequest() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        // add request headers more than MAX_HEADERS
        for (int i = 0; i < (getDefaultMaxHeaders() + 1); ++i) {
            get.addHeader(HEADER + i, MESSAGE + i);
        }
        HttpResponse result = client.execute(get);
        //this is not great, but the HTTP/2 impl sends a stream error which is translated to a 503. Should not be a big deal in practice
        Assert.assertEquals(DefaultServer.isH2() ? StatusCodes.SERVICE_UNAVAILABLE : StatusCodes.BAD_REQUEST, result.getStatusLine().getStatusCode());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 8 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class LotsOfQueryParametersTestCase method testLotsOfQueryParameters_MaxParameters_BadRequest.

@Test
@AjpIgnore
public void testLotsOfQueryParameters_MaxParameters_BadRequest() throws IOException {
    OptionMap existing = DefaultServer.getUndertowOptions();
    TestHttpClient client = new TestHttpClient();
    try {
        StringBuilder qs = new StringBuilder();
        // add query parameters more than specified MAX_PARAMETERS
        for (int i = 0; i < (TEST_MAX_PARAMETERS + 1); ++i) {
            qs.append(QUERY + i);
            qs.append("=");
            qs.append(URLEncoder.encode(MESSAGE + i, "UTF-8"));
            qs.append("&");
        }
        // delete last useless '&'
        qs.deleteCharAt(qs.length() - 1);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path?" + qs.toString());
        DefaultServer.setUndertowOptions(OptionMap.create(UndertowOptions.MAX_PARAMETERS, TEST_MAX_PARAMETERS));
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.BAD_REQUEST, result.getStatusLine().getStatusCode());
    } finally {
        DefaultServer.setUndertowOptions(existing);
        client.getConnectionManager().shutdown();
    }
}
Also used : OptionMap(org.xnio.OptionMap) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test) AjpIgnore(io.undertow.testutils.AjpIgnore)

Example 9 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class LotsOfQueryParametersTestCase method testLotsOfQueryParameters_MaxParameters_Ok.

@Test
@AjpIgnore
public void testLotsOfQueryParameters_MaxParameters_Ok() throws IOException {
    OptionMap existing = DefaultServer.getUndertowOptions();
    TestHttpClient client = new TestHttpClient();
    try {
        StringBuilder qs = new StringBuilder();
        for (int i = 0; i < TEST_MAX_PARAMETERS; ++i) {
            qs.append(QUERY + i);
            qs.append("=");
            qs.append(URLEncoder.encode(MESSAGE + i, "UTF-8"));
            qs.append("&");
        }
        // delete last useless '&'
        qs.deleteCharAt(qs.length() - 1);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path?" + qs.toString());
        DefaultServer.setUndertowOptions(OptionMap.create(UndertowOptions.MAX_PARAMETERS, TEST_MAX_PARAMETERS));
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        for (int i = 0; i < TEST_MAX_PARAMETERS; ++i) {
            Header[] header = result.getHeaders(QUERY + i);
            Assert.assertEquals(MESSAGE + i, header[0].getValue());
        }
    } finally {
        DefaultServer.setUndertowOptions(existing);
        client.getConnectionManager().shutdown();
    }
}
Also used : Header(org.apache.http.Header) OptionMap(org.xnio.OptionMap) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test) AjpIgnore(io.undertow.testutils.AjpIgnore)

Example 10 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class MetricsHandlerTestCase method testMetrics.

@Test
public void testMetrics() throws IOException, InterruptedException {
    MetricsHandler metricsHandler;
    CompletionLatchHandler latchHandler;
    DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(metricsHandler = new MetricsHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            Thread.sleep(100);
            exchange.getResponseSender().send("Hello");
        }
    })));
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
    TestHttpClient client = new TestHttpClient();
    try {
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
        latchHandler.await();
        latchHandler.reset();
        MetricsHandler.MetricResult metrics = metricsHandler.getMetrics();
        Assert.assertEquals(1, metrics.getTotalRequests());
        Assert.assertTrue(metrics.getMaxRequestTime() > 0);
        Assert.assertEquals(metrics.getMinRequestTime(), metrics.getMaxRequestTime());
        Assert.assertEquals(metrics.getMaxRequestTime(), metrics.getTotalRequestTime());
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
        latchHandler.await();
        latchHandler.reset();
        metrics = metricsHandler.getMetrics();
        Assert.assertEquals(2, metrics.getTotalRequests());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) CompletionLatchHandler(io.undertow.util.CompletionLatchHandler) 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

TestHttpClient (io.undertow.testutils.TestHttpClient)302 HttpResponse (org.apache.http.HttpResponse)290 Test (org.junit.Test)269 HttpGet (org.apache.http.client.methods.HttpGet)239 Header (org.apache.http.Header)66 HttpPost (org.apache.http.client.methods.HttpPost)54 IOException (java.io.IOException)38 StringEntity (org.apache.http.entity.StringEntity)30 Path (java.nio.file.Path)29 PathHandler (io.undertow.server.handlers.PathHandler)28 HttpHandler (io.undertow.server.HttpHandler)20 CanonicalPathHandler (io.undertow.server.handlers.CanonicalPathHandler)20 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)20 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)20 ArrayList (java.util.ArrayList)20 HttpServerExchange (io.undertow.server.HttpServerExchange)19 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)16 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)14 NameValuePair (org.apache.http.NameValuePair)13 DigestWWWAuthenticateToken (io.undertow.security.impl.DigestWWWAuthenticateToken)11