Search in sources :

Example 26 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project nanohttpd by NanoHttpd.

the class TestHttpServer method testRangeHeaderWithStartAndEndPosition.

@Test
public void testRangeHeaderWithStartAndEndPosition() throws ClientProtocolException, IOException {
    CloseableHttpResponse response = null;
    try {
        HttpGet httpGet = new HttpGet("http://localhost:9090/testdir/test.html");
        httpGet.addHeader("range", "bytes=10-40");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String responseString = new String(readContents(entity), "UTF-8");
        Assert.assertThat("The data from the beginning of the file should have been skipped as specified in the 'range' header", responseString, not(containsString("<head>")));
        Assert.assertThat("The data from the end of the file should have been skipped as specified in the 'range' header", responseString, not(containsString("</head>")));
        Assert.assertEquals("The 'Content-Length' should be the length from the requested start position to end position", "31", response.getHeaders("Content-Length")[0].getValue());
        Assert.assertEquals("The 'Contnet-Range' header should contain the correct lengths and offsets based on the range served", "bytes 10-40/84", response.getHeaders("Content-Range")[0].getValue());
        Assert.assertEquals("Response status for a successful request with 'range' header should be PARTIAL_CONTENT(206)", 206, response.getStatusLine().getStatusCode());
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 27 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project nanohttpd by NanoHttpd.

the class TestHttpServer method testIndexFileIsShownWhenURLEndsWithDirectory.

@Test
public void testIndexFileIsShownWhenURLEndsWithDirectory() throws ClientProtocolException, IOException {
    CloseableHttpResponse response = null;
    try {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://localhost:9090/testdir/testdir");
        response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String responseString = new String(readContents(entity), "UTF-8");
        Assert.assertThat("When the URL ends with a directory, and if an index.html file is present in that directory," + " the server should respond with that file", responseString, containsString("Simple index file"));
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 28 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project nanohttpd by NanoHttpd.

the class TestHttpServer method doSomeBasicTest.

@Test
public void doSomeBasicTest() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://localhost:9090/testdir/test.html");
    CloseableHttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    String string = new String(readContents(entity), "UTF-8");
    Assert.assertEquals("<html>\n<head>\n<title>dummy</title>\n</head>\n<body>\n\t<h1>it works</h1>\n</body>\n</html>", string);
    response.close();
    httpget = new HttpGet("http://localhost:9090/");
    response = httpclient.execute(httpget);
    entity = response.getEntity();
    string = new String(readContents(entity), "UTF-8");
    Assert.assertTrue(string.indexOf("testdir") > 0);
    response.close();
    httpget = new HttpGet("http://localhost:9090/testdir");
    response = httpclient.execute(httpget);
    entity = response.getEntity();
    string = new String(readContents(entity), "UTF-8");
    Assert.assertTrue(string.indexOf("test.html") > 0);
    response.close();
    httpget = new HttpGet("http://localhost:9090/testdir/testpdf.pdf");
    response = httpclient.execute(httpget);
    entity = response.getEntity();
    byte[] actual = readContents(entity);
    byte[] expected = readContents(new FileInputStream("src/test/resources/testdir/testpdf.pdf"));
    Assert.assertArrayEquals(expected, actual);
    response.close();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 29 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project nanohttpd by NanoHttpd.

the class TestHttpServer method doArgumentTest.

@Test
public void doArgumentTest() throws InterruptedException, UnsupportedEncodingException, IOException {
    final String testPort = "9458";
    Thread testServer = new Thread(new Runnable() {

        @Override
        public void run() {
            String[] args = { "-h", "localhost", "-p", testPort, "-d", "src/test/resources" };
            SimpleWebServer.main(args);
        }
    });
    testServer.start();
    Thread.sleep(200);
    HttpGet httpget = new HttpGet("http://localhost:" + testPort + "/");
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String str = new String(readContents(entity), "UTF-8");
        Assert.assertTrue("The response entity didn't contain the string 'testdir'", str.indexOf("testdir") >= 0);
    } finally {
        if (response != null)
            response.close();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 30 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project nanohttpd by NanoHttpd.

the class TestHttpServer method testRangeHeaderAndIfNoneMatchHeader.

@Test
public void testRangeHeaderAndIfNoneMatchHeader() throws ClientProtocolException, IOException {
    CloseableHttpResponse response = null;
    try {
        HttpGet httpGet = new HttpGet("http://localhost:9090/testdir/test.html");
        httpGet.addHeader("range", "bytes=10-20");
        httpGet.addHeader("if-none-match", "*");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        response = httpClient.execute(httpGet);
        Assert.assertEquals("The response status to a reqeuest with 'if-non-match=*' header and 'range' header should be NOT_MODIFIED(304)," + " if the file exists, because 'if-non-match' header should be given priority", 304, response.getStatusLine().getStatusCode());
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Aggregations

CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)353 HttpGet (org.apache.http.client.methods.HttpGet)181 Test (org.junit.Test)178 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)172 HttpResponse (org.apache.http.HttpResponse)105 HttpEntity (org.apache.http.HttpEntity)82 IOException (java.io.IOException)75 HttpPost (org.apache.http.client.methods.HttpPost)62 StringEntity (org.apache.http.entity.StringEntity)59 InputStream (java.io.InputStream)40 StatusLine (org.apache.http.StatusLine)40 URI (java.net.URI)34 HttpHost (org.apache.http.HttpHost)29 RequestConfig (org.apache.http.client.config.RequestConfig)26 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)24 Header (org.apache.http.Header)20 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)19 AuthScope (org.apache.http.auth.AuthScope)18 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)18 CredentialsProvider (org.apache.http.client.CredentialsProvider)16