Search in sources :

Example 36 with TestHttpClient

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

the class URLRewritingSessionTestCase method testURLRewriting.

@Test
public void testURLRewriting() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setCookieStore(new BasicCookieStore());
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath;foo=bar");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String url = HttpClientUtils.readResponse(result);
        Header[] header = result.getHeaders(COUNT);
        Assert.assertEquals("0", header[0].getValue());
        get = new HttpGet(url);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        url = HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("1", header[0].getValue());
        get = new HttpGet(url);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        url = HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("2", header[0].getValue());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpString(io.undertow.util.HttpString) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 37 with TestHttpClient

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

the class ServerSentEventTestCase method testRetry.

@Test
public void testRetry() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        DefaultServer.setRootHandler(new ServerSentEventHandler(new ServerSentEventConnectionCallback() {

            @Override
            public void connected(ServerSentEventConnection connection, String lastEventId) {
                connection.sendRetry(1000, new ServerSentEventConnection.EventCallback() {

                    @Override
                    public void done(ServerSentEventConnection connection, String data, String event, String id) {
                        connection.shutdown();
                    }

                    @Override
                    public void failed(ServerSentEventConnection connection, String data, String event, String id, IOException e) {
                        e.printStackTrace();
                        IoUtils.safeClose(connection);
                    }
                });
            }
        }));
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("retry:1000\n\n", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : 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 38 with TestHttpClient

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

the class ServerSentEventTestCase method testLargeMessage.

@Test
public void testLargeMessage() throws IOException {
    TestHttpClient client = new TestHttpClient();
    final StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10000; ++i) {
        sb.append("hello world ");
    }
    try {
        DefaultServer.setRootHandler(new ServerSentEventHandler(new ServerSentEventConnectionCallback() {

            @Override
            public void connected(ServerSentEventConnection connection, String lastEventId) {
                connection.send(sb.toString(), new ServerSentEventConnection.EventCallback() {

                    @Override
                    public void done(ServerSentEventConnection connection, String data, String event, String id) {
                        connection.shutdown();
                    }

                    @Override
                    public void failed(ServerSentEventConnection connection, String data, String event, String id, IOException e) {
                        e.printStackTrace();
                        IoUtils.safeClose(connection);
                    }
                });
            }
        }));
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("data:" + sb.toString() + "\n\n", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : 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 39 with TestHttpClient

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

the class ContentOverrunTestCase method testContentPastContentLength.

@Test
public void testContentPastContentLength() throws Exception {
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/long");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("Overly lon", response);
    } 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 40 with TestHttpClient

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

the class ContentOverrunTestCase method testContentOn204.

@Test
public void testContentOn204() throws Exception {
    final TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/204");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.NO_CONTENT, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("", response);
    } 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)

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