Search in sources :

Example 56 with HttpUriRequest

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."));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 57 with HttpUriRequest

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."));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 58 with HttpUriRequest

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);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) URI(java.net.URI)

Example 59 with HttpUriRequest

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");
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader) URI(java.net.URI) PrintWriter(java.io.PrintWriter)

Example 60 with HttpUriRequest

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");
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) JSONArray(org.json.JSONArray) URI(java.net.URI) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader) PrintWriter(java.io.PrintWriter)

Aggregations

HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)185 Test (org.junit.Test)62 TestRequest (com.android.volley.mock.TestRequest)52 HttpGet (org.apache.http.client.methods.HttpGet)45 URI (java.net.URI)43 HttpResponse (org.apache.http.HttpResponse)41 HttpEntity (org.apache.http.HttpEntity)38 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)36 HttpPost (org.apache.http.client.methods.HttpPost)22 IOException (java.io.IOException)19 Header (org.apache.http.Header)18 JSONObject (org.json.JSONObject)18 BufferedReader (java.io.BufferedReader)17 InputStreamReader (java.io.InputStreamReader)17 PrintWriter (java.io.PrintWriter)17 StringWriter (java.io.StringWriter)17 HttpHost (org.apache.http.HttpHost)13 HttpPut (org.apache.http.client.methods.HttpPut)12 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)11 StringEntity (org.apache.http.entity.StringEntity)10