Search in sources :

Example 61 with TestHttpClient

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

the class DefaultServletCachingTestCase method testRangeRequest.

@Test
public void testRangeRequest() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        String fileName = "range.html";
        Path f = tmpDir.resolve(fileName);
        Files.write(f, "hello".getBytes());
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/range.html");
        get.addHeader("range", "bytes=2-3");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("ll", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : Path(java.nio.file.Path) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 62 with TestHttpClient

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

the class DefaultServletTestCase method testResourceWithFilter.

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

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

the class DefaultServletTestCase method testIfMoodifiedSince.

@Test
public void testIfMoodifiedSince() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/index.html");
        //UNDERTOW-458
        get.addHeader("date-header", "Fri, 10 Oct 2014 21:35:55 CEST");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertTrue(response.contains("Redirected home page"));
        String lm = result.getHeaders("Last-Modified")[0].getValue();
        System.out.println(lm);
        Assert.assertTrue(lm.endsWith("GMT"));
        get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/index.html");
        get.addHeader("IF-Modified-Since", lm);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.NOT_MODIFIED, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertFalse(response.contains("Redirected home page"));
    } 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 64 with TestHttpClient

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

the class DefaultServletTestCase method testRangeRequest.

@Test
public void testRangeRequest() throws IOException, InterruptedException {
    String uri = DefaultServer.getDefaultServerURL() + "/servletContext/range.txt";
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/index.html");
        get.addHeader(Headers.RANGE_STRING, "bytes=2-3");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("--", response);
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=3-100");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("3456789", response);
        Assert.assertEquals("bytes 3-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=3-9");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("3456789", response);
        Assert.assertEquals("bytes 3-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=2-3");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("23", response);
        Assert.assertEquals("bytes 2-3/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=0-0");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("0", response);
        Assert.assertEquals("bytes 0-0/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=1-");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("123456789", response);
        Assert.assertEquals("bytes 1-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=0-");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("0123456789", response);
        Assert.assertEquals("bytes 0-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=9-");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("9", response);
        Assert.assertEquals("bytes 9-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=-1");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("9", response);
        Assert.assertEquals("bytes 9-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=-100");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("0123456789", response);
        Assert.assertEquals("bytes 0-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=99-100");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.REQUEST_RANGE_NOT_SATISFIABLE, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("", response);
        Assert.assertEquals("bytes */10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=2-1");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.REQUEST_RANGE_NOT_SATISFIABLE, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("", response);
        Assert.assertEquals("bytes */10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
        //test if-range
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=2-3");
        get.addHeader(Headers.IF_RANGE_STRING, DateUtils.toDateString(new Date(System.currentTimeMillis() + 1000)));
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("23", response);
        Assert.assertEquals("bytes 2-3/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
        get = new HttpGet(uri);
        get.addHeader(Headers.RANGE_STRING, "bytes=2-3");
        get.addHeader(Headers.IF_RANGE_STRING, DateUtils.toDateString(new Date(0)));
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = EntityUtils.toString(result.getEntity());
        Assert.assertEquals("0123456789", response);
        Assert.assertNull(result.getFirstHeader(Headers.CONTENT_RANGE_STRING));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Date(java.util.Date) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 65 with TestHttpClient

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

the class DefaultServletTestCase method testDirectoryListing.

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