Search in sources :

Example 21 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient 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 22 with CloseableHttpClient

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

the class TestNanolets method doOtherMethod.

@Test
public void doOtherMethod() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpTrace httphead = new HttpTrace("http://localhost:9090/index.html");
    CloseableHttpResponse response = httpclient.execute(httphead);
    HttpEntity entity = response.getEntity();
    String string = new String(readContents(entity), "UTF-8");
    Assert.assertEquals("<html><body><h2>Hello world!</h3></body></html>", string);
    response.close();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpTrace(org.apache.http.client.methods.HttpTrace) HttpEntity(org.apache.http.HttpEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Example 23 with CloseableHttpClient

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

the class TestCorsHttpServer method testAccessControlAllowHeaderUsesDefaultsWithoutSystemProperty.

@Test
public void testAccessControlAllowHeaderUsesDefaultsWithoutSystemProperty() throws Exception {
    Assert.assertNull("no System " + SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME + " shoudl be set", System.getProperty(SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME));
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpOptions httpOption = new HttpOptions("http://localhost:9090/xxx/yyy.html");
    CloseableHttpResponse response = httpclient.execute(httpOption);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    Assert.assertEquals("Cors should have added a header: Access-Control-Allow-Headers: " + SimpleWebServer.DEFAULT_ALLOWED_HEADERS, SimpleWebServer.DEFAULT_ALLOWED_HEADERS, response.getLastHeader("Access-Control-Allow-Headers").getValue());
    response.close();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpOptions(org.apache.http.client.methods.HttpOptions) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Example 24 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient 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 25 with CloseableHttpClient

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

the class TestCorsHttpServerWithSingleOrigin method doTestOption.

@Test
public void doTestOption() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpOptions httpOption = new HttpOptions("http://localhost:9090/xxx/yyy.html");
    CloseableHttpResponse response = httpclient.execute(httpOption);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    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());
    response.close();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpOptions(org.apache.http.client.methods.HttpOptions) 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