Java Code Examples for org.apache.http.client.fluent.Request#execute()

The following examples show how to use org.apache.http.client.fluent.Request#execute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InvalidHttpMethodGatewayTest.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRespondWithNotImplemented() throws Exception {
    wireMockRule.stubFor(any(urlEqualTo("/team/my_team"))
            .willReturn(aResponse().withStatus(HttpStatus.SC_NOT_IMPLEMENTED)));


    Request request = Request.Get("http://localhost:8082/test/my_team");

    // A little bit of reflection to set an unknown HTTP method since the fluent API does not allow it.
    Field requestField = request.getClass().getDeclaredField("request");
    requestField.setAccessible(true);
    Field methodField = requestField.get(request).getClass().getDeclaredField("method");
    methodField.setAccessible(true);
    methodField.set(requestField.get(request), "unkown-method");

    Response response = request.execute();
    HttpResponse returnResponse = response.returnResponse();

    assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, returnResponse.getStatusLine().getStatusCode());

    wireMockRule.verify(anyRequestedFor(urlPathEqualTo("/team/my_team")));
}
 
Example 2
Source File: WeightedRoundRobinLoadBalancingTest.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void call_weighted_lb_multiple_endpoints() throws Exception {
    wireMockRule.stubFor(get(urlEqualTo("/api1")).willReturn(ok()));
    wireMockRule.stubFor(get(urlEqualTo("/api2")).willReturn(ok()));

    Request request = Request.Get("http://localhost:8082/api");

    int calls = 10;

    for(int i = 0 ; i < calls ; i++) {
        Response response = request.execute();
        HttpResponse returnResponse = response.returnResponse();

        assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());
    }

    wireMockRule.verify(3, getRequestedFor(urlPathEqualTo("/api1")));
    wireMockRule.verify(7, getRequestedFor(urlPathEqualTo("/api2")));
}
 
Example 3
Source File: PostContentGatewayTest.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void no_content_without_chunked_encoding_transfer() throws Exception {
    stubFor(post(urlEqualTo("/team/my_team")).willReturn(ok()));

    Request request = Request.Post("http://localhost:8082/test/my_team")
            .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
            .removeHeaders(HttpHeaders.TRANSFER_ENCODING);

    Response response = request.execute();

    HttpResponse returnResponse = response.returnResponse();
    assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());

    // Set chunk mode in request but returns raw because of the size of the content
    assertEquals(null, returnResponse.getFirstHeader("X-Forwarded-Transfer-Encoding"));

    String responseContent = StringUtils.copy(returnResponse.getEntity().getContent());
    assertEquals(0, responseContent.length());

    verify(postRequestedFor(urlEqualTo("/team/my_team"))
            .withoutHeader(HttpHeaders.TRANSFER_ENCODING)
            .withHeader(io.gravitee.common.http.HttpHeaders.CONTENT_TYPE, new EqualToPattern(MediaType.APPLICATION_JSON)));
}
 
Example 4
Source File: PostContentGatewayTest.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void get_no_content_with_chunked_encoding_transfer() throws Exception {
    stubFor(get(urlEqualTo("/team/my_team")).willReturn(ok()));

    Request request = Request.Get("http://localhost:8082/test/my_team")
            .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
            .removeHeaders(HttpHeaders.TRANSFER_ENCODING);

    Response response = request.execute();

    HttpResponse returnResponse = response.returnResponse();
    assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());

    // Set chunk mode in request but returns raw because of the size of the content
    assertEquals(null, returnResponse.getFirstHeader("X-Forwarded-Transfer-Encoding"));

    String responseContent = StringUtils.copy(returnResponse.getEntity().getContent());
    assertEquals(0, responseContent.length());

    verify(getRequestedFor(urlEqualTo("/team/my_team"))
            .withoutHeader(HttpHeaders.TRANSFER_ENCODING)
            .withHeader(io.gravitee.common.http.HttpHeaders.CONTENT_TYPE, new EqualToPattern(MediaType.APPLICATION_JSON)));
}
 
Example 5
Source File: PostContentGatewayTest.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void get_no_content_with_chunked_encoding_transfer_and_content_type() throws Exception {
    stubFor(get(urlEqualTo("/team/my_team")).willReturn(ok()));

    Request request = Request.Get("http://localhost:8082/test/my_team")
            .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);

    Response response = request.execute();

    HttpResponse returnResponse = response.returnResponse();
    assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());

    // Set chunk mode in request but returns raw because of the size of the content
    assertEquals(null, returnResponse.getFirstHeader("X-Forwarded-Transfer-Encoding"));

    String responseContent = StringUtils.copy(returnResponse.getEntity().getContent());
    assertEquals(0, responseContent.length());

    verify(getRequestedFor(urlEqualTo("/team/my_team"))
            .withHeader(io.gravitee.common.http.HttpHeaders.CONTENT_TYPE, new EqualToPattern(MediaType.APPLICATION_JSON)));
}
 
Example 6
Source File: OrionConnector.java    From kurento-tutorial-java with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a request to Orion
 *
 * @param ctxElement
 *          The context element
 * @param path
 *          the path from the context broker that determines which "operation"will be executed
 * @param responseClazz
 *          The class expected for the response
 * @return The object representing the JSON answer from Orion
 * @throws OrionConnectorException
 *           if a communication exception happens, either when contacting the context broker at
 *           the given address, or obtaining the answer from it.
 */
private <E, T> T sendRequestToOrion(E ctxElement, String path, Class<T> responseClazz) {
  String jsonEntity = gson.toJson(ctxElement);
  log.debug("Send request to Orion: {}", jsonEntity);

  Request req = Request.Post(this.orionAddr.toString() + path)
      .addHeader("Accept", APPLICATION_JSON.getMimeType())
      .bodyString(jsonEntity, APPLICATION_JSON).connectTimeout(5000).socketTimeout(5000);
  Response response;
  try {
    response = req.execute();
  } catch (IOException e) {
    throw new OrionConnectorException("Could not execute HTTP request", e);
  }

  HttpResponse httpResponse = checkResponse(response);

  T ctxResp = getOrionObjFromResponse(httpResponse, responseClazz);
  log.debug("Sent to Orion. Obtained response: {}", httpResponse);

  return ctxResp;
}
 
Example 7
Source File: RestMetricsDispatcher.java    From gradle-metrics-plugin with Apache License 2.0 5 votes vote down vote up
protected void postPayload(String payload) {
    checkNotNull(payload);

    try {
        Request postReq = Request.Post(extension.getRestUri());
        postReq.bodyString(payload , ContentType.APPLICATION_JSON);
        addHeaders(postReq);
        postReq.execute();
    } catch (IOException e) {
        throw new RuntimeException("Unable to POST to " + extension.getRestUri(), e);
    }
}