use of org.apache.http.impl.client.CloseableHttpClient in project nanohttpd by NanoHttpd.
the class TestHttpServer method testRangeHeaderWithStartPositionOnly.
@Test
public void testRangeHeaderWithStartPositionOnly() throws ClientProtocolException, IOException {
CloseableHttpResponse response = null;
try {
HttpGet httpGet = new HttpGet("http://localhost:9090/testdir/test.html");
httpGet.addHeader("range", "bytes=10-");
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 response should contain the data from the end of the file since end position was not given in the 'range' header", responseString, containsString("</head>"));
Assert.assertEquals("The content length should be the length starting from the requested byte", "74", response.getHeaders("Content-Length")[0].getValue());
Assert.assertEquals("The 'Content-Range' header should contain the correct lengths and offsets based on the range served", "bytes 10-83/84", response.getHeaders("Content-Range")[0].getValue());
Assert.assertEquals("Response status for a successful range request should be PARTIAL_CONTENT(206)", 206, response.getStatusLine().getStatusCode());
} finally {
if (response != null) {
response.close();
}
}
}
use of org.apache.http.impl.client.CloseableHttpClient in project nanohttpd by NanoHttpd.
the class TestHttpServer method testIfNoneMatchHeader.
@Test
public void testIfNoneMatchHeader() throws ClientProtocolException, IOException {
CloseableHttpResponse response = null;
try {
HttpGet httpGet = new HttpGet("http://localhost:9090/testdir/test.html");
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 should be NOT_MODIFIED(304), if the file exists", 304, response.getStatusLine().getStatusCode());
} finally {
if (response != null) {
response.close();
}
}
}
use of org.apache.http.impl.client.CloseableHttpClient in project nanohttpd by NanoHttpd.
the class TestNanolets method doGeneralParams.
@Test
public void doGeneralParams() throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost:9090/general/value1/value2?param3=value3¶m4=value4");
CloseableHttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
String string = new String(readContents(entity), "UTF-8");
Assert.assertEquals("<html><body><h1>Url: /general/value1/value2</h1><br><p>Param 'param3' = value3</p><p>Param 'param4' = value4</p>", string);
response.close();
}
use of org.apache.http.impl.client.CloseableHttpClient in project nanohttpd by NanoHttpd.
the class TestNanolets method doSomeBasicMethodTest.
@Test
public void doSomeBasicMethodTest() throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost:9090/user/blabla");
CloseableHttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
String string = new String(readContents(entity), "UTF-8");
Assert.assertEquals("<html><body>User handler. Method: GET<br><h1>Uri parameters:</h1><div> Param: id Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
response.close();
HttpPost httppost = new HttpPost("http://localhost:9090/user/blabla");
response = httpclient.execute(httppost);
entity = response.getEntity();
string = new String(readContents(entity), "UTF-8");
Assert.assertEquals("<html><body>User handler. Method: POST<br><h1>Uri parameters:</h1><div> Param: id Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
response.close();
HttpPut httpgput = new HttpPut("http://localhost:9090/user/blabla");
response = httpclient.execute(httpgput);
entity = response.getEntity();
string = new String(readContents(entity), "UTF-8");
Assert.assertEquals("<html><body>User handler. Method: PUT<br><h1>Uri parameters:</h1><div> Param: id Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
response.close();
HttpDelete httpdelete = new HttpDelete("http://localhost:9090/user/blabla");
response = httpclient.execute(httpdelete);
entity = response.getEntity();
string = new String(readContents(entity), "UTF-8");
Assert.assertEquals("<html><body>User handler. Method: DELETE<br><h1>Uri parameters:</h1><div> Param: id Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
response.close();
}
use of org.apache.http.impl.client.CloseableHttpClient in project disconf by knightliao.
the class HttpClientUtil method buildHttpClient.
/**
* 初始化httpclient对象
*/
private static void buildHttpClient() {
RequestConfig globalConfig = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();
CloseableHttpClient httpclient = HttpClients.custom().setKeepAliveStrategy(new HttpClientKeepAliveStrategy()).setDefaultRequestConfig(globalConfig).build();
HttpClientUtil.httpclient = httpclient;
}
Aggregations