Search in sources :

Example 46 with TestHttpClient

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

the class GetCookiesTestCase method testGetCookiesWithOnlyValidCookie.

@Test
public void testGetCookiesWithOnlyValidCookie() throws Exception {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/aaa");
        get.setHeader(Headers.COOKIE_STRING, "testcookie=works");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("Only one valid cookie", "name='testcookie'value='works'", 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 47 with TestHttpClient

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

the class ParameterEchoTestCase method testPostBoth.

@Test
public void testPostBoth() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/aaa?param1=1&param2=2");
        final List<NameValuePair> values = new ArrayList<>();
        values.add(new BasicNameValuePair("param3", "3"));
        UrlEncodedFormEntity data = new UrlEncodedFormEntity(values, "UTF-8");
        post.setEntity(data);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals(RESPONSE, response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 48 with TestHttpClient

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

the class GetResourceTestCase method testGetResource.

@Test
public void testGetResource() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/file?file=/file.txt");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("File Contents", 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 49 with TestHttpClient

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

the class SslUpgradeTestCase method runTest.

public void runTest(final String url) throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        final Socket socket = DefaultServer.getClientSSLContext().getSocketFactory().createSocket(new Socket(DefaultServer.getHostAddress("default"), DefaultServer.getHostSSLPort("default")), DefaultServer.getHostAddress("default"), DefaultServer.getHostSSLPort("default"), true);
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        out.write(("GET " + url + " HTTP/1.1\r\nHost: default\r\nConnection: upgrade\r\nUpgrade:servlet\r\n\r\n").getBytes());
        out.flush();
        String bytes = readBytes(in);
        Assert.assertTrue(bytes, bytes.startsWith("HTTP/1.1 101 Switching Protocols\r\n"));
        out.write("Echo Messages\r\n\r\n".getBytes());
        out.flush();
        Assert.assertEquals("Echo Messages\r\n\r\n", readBytes(in));
        out.write("Echo Messages2\r\n\r\n".getBytes());
        out.flush();
        Assert.assertEquals("Echo Messages2\r\n\r\n", readBytes(in));
        out.write("exit\r\n\r\n".getBytes());
        out.flush();
        socket.close();
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket) TestHttpClient(io.undertow.testutils.TestHttpClient)

Example 50 with TestHttpClient

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

the class AbstractResponseWrapperTestCase method testStandardWrapper.

@Test
public void testStandardWrapper() throws IOException, ServletException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/standard");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals(StandardRequestWrapper.class.getName() + "\n" + StandardResponseWrapper.class.getName(), 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