Search in sources :

Example 51 with TestHttpClient

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

the class ChangeSessionIdTestCase method testChangeSessionId.

@Test
public void testChangeSessionId() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/aa");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        String oldId = testResponse(response, null);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        oldId = testResponse(response, oldId);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        oldId = testResponse(response, oldId);
    } 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 52 with TestHttpClient

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

the class CrossContextServletSessionTestCase method testCrossContextSessionIncludeInvocation.

@Test
public void testCrossContextSessionIncludeInvocation() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet direct1 = new HttpGet(DefaultServer.getDefaultServerURL() + "/1/servlet");
        HttpGet include1 = new HttpGet(DefaultServer.getDefaultServerURL() + "/1/include?context=/2&path=/servlet");
        HttpGet direct2 = new HttpGet(DefaultServer.getDefaultServerURL() + "/2/servlet");
        HttpGet include2 = new HttpGet(DefaultServer.getDefaultServerURL() + "/2/include?context=/1&path=/servlet");
        HttpResponse result = client.execute(include2);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("1", response);
        result = client.execute(direct1);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("2", response);
        result = client.execute(include2);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("3", response);
        result = client.execute(include2);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("4", response);
        result = client.execute(include1);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("1", response);
        result = client.execute(include1);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("2", response);
        result = client.execute(direct2);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("3", response);
        result = client.execute(direct2);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("4", 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 53 with TestHttpClient

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

the class ServletSessionCrawlerTestCase method testCrawlerSessionUsage.

@Test
public void testCrawlerSessionUsage() throws IOException, InterruptedException {
    final PathHandler pathHandler = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo().setCrawlerSessionManagerConfig(new CrawlerSessionManagerConfig()).setClassLoader(SimpleServletTestCase.class.getClassLoader()).setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addListener(new ListenerInfo(SessionCookieConfigListener.class)).addServlets(new ServletInfo("servlet", SessionServlet.class).addMapping("/aa/b"));
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    try {
        pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
    } catch (ServletException e) {
        throw new RuntimeException(e);
    }
    DefaultServer.setRootHandler(pathHandler);
    TestHttpClient client = new TestHttpClient();
    client.setCookieStore(new CookieStore() {

        @Override
        public void addCookie(Cookie cookie) {
        }

        @Override
        public List<Cookie> getCookies() {
            return Collections.EMPTY_LIST;
        }

        @Override
        public boolean clearExpired(Date date) {
            return false;
        }

        @Override
        public void clear() {
        }
    });
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/aa/b");
        get.addHeader(Headers.USER_AGENT_STRING, "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("1", response);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("2", response);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("3", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Cookie(org.apache.http.cookie.Cookie) DeploymentManager(io.undertow.servlet.api.DeploymentManager) HttpGet(org.apache.http.client.methods.HttpGet) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) Date(java.util.Date) TestHttpClient(io.undertow.testutils.TestHttpClient) ServletInfo(io.undertow.servlet.api.ServletInfo) ServletException(javax.servlet.ServletException) CookieStore(org.apache.http.client.CookieStore) ListenerInfo(io.undertow.servlet.api.ListenerInfo) CrawlerSessionManagerConfig(io.undertow.servlet.api.CrawlerSessionManagerConfig) ServletContainer(io.undertow.servlet.api.ServletContainer) List(java.util.List) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) Test(org.junit.Test)

Example 54 with TestHttpClient

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

the class ServletSessionTestCase method testSimpleSessionUsage.

@Test
public void testSimpleSessionUsage() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/aa/b");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("1", response);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("2", response);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("3", 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 55 with TestHttpClient

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

the class ServletInputStreamConnectionTerminationTestCase method testConnectionTermination.

@Test
public void testConnectionTermination() throws IOException {
    StringBuilder builder = new StringBuilder(1000 * HELLO_WORLD.length());
    for (int j = 0; j < 1000; ++j) {
        builder.append(HELLO_WORLD);
    }
    String message = builder.toString();
    TestHttpClient client = new TestHttpClient();
    try {
        String uri = DefaultServer.getDefaultServerURL() + "/servletContext/term";
        HttpPost post = new HttpPost(uri);
        post.setEntity(new StringEntity(message));
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.fail();
    } catch (IOException expected) {
    //expected
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) 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