Search in sources :

Example 46 with HttpGet

use of org.apache.http.client.methods.HttpGet in project nanohttpd by NanoHttpd.

the class TestNanolets method doMissingHandler.

@Test
public void doMissingHandler() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://localhost:9090/photos/abc/def");
    CloseableHttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    String string = new String(readContents(entity), "UTF-8");
    Assert.assertEquals("<html><body><h2>The uri is mapped in the router, but no handler is specified. <br> Status: Not implemented!</h3></body></html>", string);
    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) Test(org.junit.Test)

Example 47 with HttpGet

use of org.apache.http.client.methods.HttpGet in project nanohttpd by NanoHttpd.

the class CookieIntegrationTest method testMultipleCookieSentBackToClient.

@Test
public void testMultipleCookieSentBackToClient() throws Exception {
    this.testServer.cookiesToSend.add(new Cookie("name0", "value0", 30));
    this.testServer.cookiesToSend.add(new Cookie("name1", "value1", 30));
    this.testServer.cookiesToSend.add(new Cookie("name2", "value2", 30));
    this.testServer.cookiesToSend.add(new Cookie("name3", "value3", 30));
    HttpGet httpget = new HttpGet("http://localhost:8192/");
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    this.httpclient.execute(httpget, responseHandler);
    assertEquals(4, this.httpclient.getCookieStore().getCookies().size());
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Cookie(org.nanohttpd.protocols.http.content.Cookie) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) Test(org.junit.Test)

Example 48 with HttpGet

use of org.apache.http.client.methods.HttpGet in project nanohttpd by NanoHttpd.

the class TestCorsHttpServerWithSingleOrigin 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.assertNotNull("Cors should have added a header: Access-Control-Allow-Origin", response.getLastHeader("Access-Control-Allow-Origin"));
    Assert.assertEquals("Cors should have added a header: Access-Control-Allow-Origin: http://localhost:9090", "http://localhost:9090", response.getLastHeader("Access-Control-Allow-Origin").getValue());
    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();
}
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) Test(org.junit.Test)

Example 49 with HttpGet

use of org.apache.http.client.methods.HttpGet 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 50 with HttpGet

use of org.apache.http.client.methods.HttpGet 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)

Aggregations

HttpGet (org.apache.http.client.methods.HttpGet)1143 HttpResponse (org.apache.http.HttpResponse)717 Test (org.junit.Test)504 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)242 TestHttpClient (io.undertow.testutils.TestHttpClient)239 IOException (java.io.IOException)200 HttpClient (org.apache.http.client.HttpClient)185 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)179 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)176 HttpEntity (org.apache.http.HttpEntity)152 Header (org.apache.http.Header)133 InputStream (java.io.InputStream)103 URI (java.net.URI)83 JsonNode (com.fasterxml.jackson.databind.JsonNode)68 ArrayList (java.util.ArrayList)60 MockResponse (com.google.mockwebserver.MockResponse)54 HttpPost (org.apache.http.client.methods.HttpPost)54 Deployment (org.activiti.engine.test.Deployment)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)46 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)45