Search in sources :

Example 86 with TestHttpClient

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

the class SimpleBlockingServerTestCase method testHeadRequests.

@Test
public void testHeadRequests() throws IOException {
    message = "My HTTP Request!";
    TestHttpClient client = new TestHttpClient();
    HttpHead head = new HttpHead(DefaultServer.getDefaultServerURL() + "/path");
    try {
        for (int i = 0; i < 3; ++i) {
            //WFLY-1540 run a few requests to make sure persistent re
            HttpResponse result = client.execute(head);
            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
            Assert.assertEquals("", HttpClientUtils.readResponse(result));
            Assert.assertEquals(message.length() + "", result.getFirstHeader(Headers.CONTENT_LENGTH_STRING).getValue());
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) HttpHead(org.apache.http.client.methods.HttpHead) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 87 with TestHttpClient

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

the class SimpleBlockingServerTestCase method testDeleteRequests.

@Test
public void testDeleteRequests() throws IOException {
    message = "My HTTP Request!";
    TestHttpClient client = new TestHttpClient();
    HttpDelete delete = new HttpDelete(DefaultServer.getDefaultServerURL() + "/path");
    try {
        for (int i = 0; i < 3; ++i) {
            //WFLY-1540 run a few requests to make sure persistent re
            HttpResponse result = client.execute(delete);
            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
            Assert.assertEquals(message, HttpClientUtils.readResponse(result));
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 88 with TestHttpClient

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

the class SimpleBlockingServerTestCase method testLargeResponse.

@Test
public void testLargeResponse() throws IOException {
    final StringBuilder messageBuilder = new StringBuilder(6919638);
    for (int i = 0; i < 6919638; ++i) {
        messageBuilder.append("*");
    }
    message = messageBuilder.toString();
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String resultString = HttpClientUtils.readResponse(result);
        Assert.assertEquals(message.length(), resultString.length());
        Assert.assertTrue(message.equals(resultString));
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path?useSender");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String resultBody = HttpClientUtils.readResponse(result);
        Assert.assertTrue(message.equals(resultBody));
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path?useFragmentedSender");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        resultBody = HttpClientUtils.readResponse(result);
        Assert.assertTrue(message.equals(resultBody));
    } 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 89 with TestHttpClient

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

the class InMemorySessionTestCase method inMemorySessionTest.

@Test
public void inMemorySessionTest() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setCookieStore(new BasicCookieStore());
    try {
        final SessionCookieConfig sessionConfig = new SessionCookieConfig();
        final SessionAttachmentHandler handler = new SessionAttachmentHandler(new InMemorySessionManager(""), sessionConfig);
        handler.setNext(new HttpHandler() {

            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                final SessionManager manager = exchange.getAttachment(SessionManager.ATTACHMENT_KEY);
                Session session = manager.getSession(exchange, sessionConfig);
                if (session == null) {
                    session = manager.createSession(exchange, sessionConfig);
                    session.setAttribute(COUNT, 0);
                }
                Integer count = (Integer) session.getAttribute(COUNT);
                exchange.getResponseHeaders().add(new HttpString(COUNT), count.toString());
                session.setAttribute(COUNT, ++count);
            }
        });
        DefaultServer.setRootHandler(handler);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        Header[] header = result.getHeaders(COUNT);
        Assert.assertEquals("0", header[0].getValue());
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("1", header[0].getValue());
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("2", header[0].getValue());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) SessionManager(io.undertow.server.session.SessionManager) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) HttpServerExchange(io.undertow.server.HttpServerExchange) SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Header(org.apache.http.Header) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) Session(io.undertow.server.session.Session) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Example 90 with TestHttpClient

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

the class SenderTestCase method testSyncTransfer.

@Test
public void testSyncTransfer() throws Exception {
    StringBuilder sb = new StringBuilder(TXS);
    for (int i = 0; i < TXS; ++i) {
        sb.append("a");
    }
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/transfer?blocking=true");
    TestHttpClient client = new TestHttpClient();
    try {
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Path file = Paths.get(SenderTestCase.class.getResource(SenderTestCase.class.getSimpleName() + ".class").toURI());
        long length = Files.size(file);
        byte[] data = new byte[(int) length * TXS];
        for (int i = 0; i < TXS; i++) {
            try (DataInputStream is = new DataInputStream(Files.newInputStream(file))) {
                is.readFully(data, (int) (i * length), (int) length);
            }
        }
        Assert.assertArrayEquals(data, HttpClientUtils.readRawResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Path(java.nio.file.Path) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) DataInputStream(java.io.DataInputStream) 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