Search in sources :

Example 81 with TestHttpClient

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

the class LoadBalancingProxyHTTP2TestCase method testHeadersAreLowercase.

@Test
public void testHeadersAreLowercase() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/name");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        Header header = result.getFirstHeader("x-custom-header");
        Assert.assertEquals("x-custom-header", header.getName());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : 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 82 with TestHttpClient

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

the class LoadBalancingProxyWithCustomHostSelectorTestCase method testDistributeLoadToGivenHost.

// https://issues.jboss.org/browse/UNDERTOW-289
@Test
public void testDistributeLoadToGivenHost() throws Throwable {
    final StringBuilder resultString = new StringBuilder();
    for (int i = 0; i < 6; ++i) {
        TestHttpClient client = new TestHttpClient();
        try {
            HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/name");
            HttpResponse result = client.execute(get);
            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
            resultString.append(HttpClientUtils.readResponse(result));
            resultString.append(' ');
        } finally {
            client.getConnectionManager().shutdown();
        }
    }
    Assert.assertTrue(resultString.toString().contains("server1"));
    Assert.assertFalse(resultString.toString().contains("server2"));
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 83 with TestHttpClient

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

the class ProxyHandlerXForwardedForTestCase method testXForwarded.

@Test
public void testXForwarded() throws Exception {
    setProxyHandler(false, false);
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/x-forwarded");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(port, Integer.parseInt(result.getFirstHeader(Headers.X_FORWARDED_PORT.toString()).getValue()));
        Assert.assertEquals("http", result.getFirstHeader(Headers.X_FORWARDED_PROTO.toString()).getValue());
        Assert.assertEquals("localhost", result.getFirstHeader(Headers.X_FORWARDED_HOST.toString()).getValue());
        Assert.assertEquals(DefaultServer.getDefaultServerAddress().getAddress().getHostAddress(), result.getFirstHeader(Headers.X_FORWARDED_FOR.toString()).getValue());
    } 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 84 with TestHttpClient

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

the class ProxyHandlerXForwardedForTestCase method testReuseXForwarded.

@Test
public void testReuseXForwarded() throws Exception {
    setProxyHandler(false, true);
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/x-forwarded");
        get.addHeader(Headers.X_FORWARDED_FOR.toString(), "50.168.245.32");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(port, Integer.parseInt(result.getFirstHeader(Headers.X_FORWARDED_PORT.toString()).getValue()));
        Assert.assertEquals("http", result.getFirstHeader(Headers.X_FORWARDED_PROTO.toString()).getValue());
        Assert.assertEquals("localhost", result.getFirstHeader(Headers.X_FORWARDED_HOST.toString()).getValue());
        Assert.assertEquals("50.168.245.32," + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress(), result.getFirstHeader(Headers.X_FORWARDED_FOR.toString()).getValue());
    } 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 85 with TestHttpClient

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

the class AccessLogTestCase method testRemoteAddress.

@Test
public void testRemoteAddress() throws IOException, InterruptedException {
    latch = new CountDownLatch(1);
    DefaultServer.setRootHandler(new StoredResponseHandler(new AccessLogHandler(HELLO_HANDLER, RECEIVER, "Remote address %a Code %s test-header %{i,test-header} %{STORED_RESPONSE}", AccessLogFileTestCase.class.getClassLoader())));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.addHeader("test-header", "test-value");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("HelloResponse", HttpClientUtils.readResponse(result));
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header test-value HelloResponse", message);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : StoredResponseHandler(io.undertow.server.handlers.StoredResponseHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) 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