use of org.apache.http.client.methods.HttpUriRequest in project opennms by OpenNMS.
the class JUnitHttpServerTest method testServer.
@Test
@JUnitHttpServer(port = 9162)
public void testServer() throws HttpException, IOException {
HttpUriRequest method = new HttpGet("http://localhost:9162/test.html");
final CloseableHttpResponse response = m_clientWrapper.execute(method);
String responseString = EntityUtils.toString(response.getEntity());
LOG.debug("got response:\n{}", responseString);
assertEquals(200, response.getStatusLine().getStatusCode());
assertTrue(responseString.contains("Purple monkey dishwasher."));
}
use of org.apache.http.client.methods.HttpUriRequest in project opennms by OpenNMS.
the class JUnitHttpServerTest method testWebapp.
@Test
@JUnitHttpServer(port = 9162, webapps = { @Webapp(context = "/testContext", path = "src/test/resources/test-webapp") })
public void testWebapp() throws Exception {
HttpUriRequest method = new HttpGet("http://localhost:9162/testContext/index.html");
final CloseableHttpResponse response = m_clientWrapper.execute(method);
String responseString = EntityUtils.toString(response.getEntity());
LOG.debug("got response:\n{}", responseString);
assertEquals(200, response.getStatusLine().getStatusCode());
assertTrue(responseString.contains("This is a webapp."));
}
use of org.apache.http.client.methods.HttpUriRequest in project platform_external_apache-http by android.
the class DefaultRequestDirector method determineRoute.
/**
* Determines the route for a request.
* Called by {@link #execute}
* to determine the route for either the original or a followup request.
*
* @param target the target host for the request.
* Implementations may accept <code>null</code>
* if they can still determine a route, for example
* to a default target or by inspecting the request.
* @param request the request to execute
* @param context the context to use for the execution,
* never <code>null</code>
*
* @return the route the request should take
*
* @throws HttpException in case of a problem
*/
protected HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
if (target == null) {
target = (HttpHost) request.getParams().getParameter(ClientPNames.DEFAULT_HOST);
}
if (target == null) {
// BEGIN android-changed
// If the URI was malformed, make it obvious where there's no host component
String scheme = null;
String host = null;
String path = null;
URI uri;
if (request instanceof HttpUriRequest && (uri = ((HttpUriRequest) request).getURI()) != null) {
scheme = uri.getScheme();
host = uri.getHost();
path = uri.getPath();
}
throw new IllegalStateException("Target host must not be null, or set in parameters." + " scheme=" + scheme + ", host=" + host + ", path=" + path);
// END android-changed
}
return this.routePlanner.determineRoute(target, request, context);
}
use of org.apache.http.client.methods.HttpUriRequest in project geode by apache.
the class ClusterSelectedRegionServiceTest method testResponseNotNull.
/**
* Tests that service returns json object
*
*/
@Test
public void testResponseNotNull() {
System.out.println("ClusterSelectedRegionServiceTest :: ------TESTCASE BEGIN : NULL RESPONSE CHECK FOR CLUSTER REGIONS------");
if (httpclient != null) {
try {
HttpUriRequest pulseupdate = RequestBuilder.post().setUri(new URI(PULSE_UPDATE_URL)).addParameter(PULSE_UPDATE_PARAM, PULSE_UPDATE_1_VALUE).build();
CloseableHttpResponse response = httpclient.execute(pulseupdate);
try {
HttpEntity entity = response.getEntity();
System.out.println("ClusterSelectedRegionServiceTest :: HTTP request status : " + response.getStatusLine());
BufferedReader respReader = new BufferedReader(new InputStreamReader(entity.getContent()));
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
String sz = null;
while ((sz = respReader.readLine()) != null) {
pw.print(sz);
}
String jsonResp = sw.getBuffer().toString();
System.out.println("ClusterSelectedRegionServiceTest :: JSON response returned : " + jsonResp);
EntityUtils.consume(entity);
JSONObject jsonObj = new JSONObject(jsonResp);
Assert.assertNotNull("ClusterSelectedRegionServiceTest :: Server returned null response for ClusterSelectedRegion", jsonObj.getJSONObject("ClusterSelectedRegion"));
} finally {
response.close();
}
} catch (Exception failed) {
logException(failed);
Assert.fail("Exception ! ");
}
} else {
Assert.fail("ClusterSelectedRegionServiceTest :: No Http connection was established.");
}
System.out.println("ClusterSelectedRegionServiceTest :: ------TESTCASE END : NULL RESPONSE CHECK FOR CLUSTER REGIONS------\n");
}
use of org.apache.http.client.methods.HttpUriRequest in project geode by apache.
the class ClusterSelectedRegionServiceTest method testResponseMemerberCount.
/**
*
* Tests that service returns json object
*
* Test method for
* {@link org.apache.geode.tools.pulse.internal.service.ClusterSelectedRegionService#execute(javax.servlet.http.HttpServletRequest)}.
*
*/
@Test
public void testResponseMemerberCount() {
System.out.println("ClusterSelectedRegionServiceTest :: ------TESTCASE BEGIN : MISMATCHED MEMBERCOUNT FOR REGION CHECK FOR CLUSTER REGIONS------");
if (httpclient != null) {
try {
HttpUriRequest pulseupdate = RequestBuilder.post().setUri(new URI(PULSE_UPDATE_URL)).addParameter(PULSE_UPDATE_PARAM, PULSE_UPDATE_1_VALUE).build();
CloseableHttpResponse response = httpclient.execute(pulseupdate);
try {
HttpEntity entity = response.getEntity();
System.out.println("ClusterSelectedRegionServiceTest :: HTTP request status : " + response.getStatusLine());
BufferedReader respReader = new BufferedReader(new InputStreamReader(entity.getContent()));
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
String sz = null;
while ((sz = respReader.readLine()) != null) {
pw.print(sz);
}
String jsonResp = sw.getBuffer().toString();
System.out.println("ClusterSelectedRegionServiceTest :: JSON response returned : " + jsonResp);
EntityUtils.consume(entity);
JSONObject jsonObj = new JSONObject(jsonResp);
JSONObject clusterSelectedRegionObj = jsonObj.getJSONObject("ClusterSelectedRegion");
Assert.assertNotNull("ClusterSelectedRegionServiceTest :: Server returned null ClusterSelectedRegion", clusterSelectedRegionObj);
JSONObject jsonObjRegion = clusterSelectedRegionObj.getJSONObject("selectedRegion");
Assert.assertNotNull("ClusterSelectedRegionServiceTest :: Server returned null for selectedRegion", jsonObjRegion);
Assert.assertTrue("ClusterSelectedRegionServiceTest :: Server did not return 'memberCount' of region", jsonObjRegion.has("memberCount"));
int memberCount = jsonObjRegion.getInt("memberCount");
Assert.assertTrue("ClusterSelectedRegionServiceTest :: Server did not return 'members' of region", jsonObjRegion.has("members"));
JSONArray arrMembers = jsonObjRegion.getJSONArray("members");
Assert.assertNotNull("ClusterSelectedRegionServiceTest :: Server returned null response in selectedRegion", arrMembers);
int members = arrMembers.length();
Assert.assertEquals("ClusterSelectedRegionServiceTest :: Server returned mismatched member count and region members", members, memberCount);
} finally {
response.close();
}
} catch (Exception failed) {
logException(failed);
Assert.fail("Exception ! ");
}
} else {
Assert.fail("ClusterSelectedRegionServiceTest :: No Http connection was established.");
}
System.out.println("ClusterSelectedRegionServiceTest :: ------TESTCASE END : MISMATCHED MEMBERCOUNT FOR REGION CHECK FOR CLUSTER REGIONS------\n");
}
Aggregations