Search in sources :

Example 21 with Response

use of org.apache.apex.shaded.ning19.com.ning.http.client.Response in project riposte by Nike-Inc.

the class VerifyAsyncHttpClientHelperComponentTest method verify_basic_functionality.

@Test
public void verify_basic_functionality() throws Exception {
    // given
    RequestBuilderWrapper rbw = asyncClient.getRequestBuilder("http://localhost:" + serverPort + TestEndpoint.MATCHING_PATH, HttpMethod.GET);
    rbw.requestBuilder.setHeader(TestEndpoint.EXPECTED_HEADER_KEY, TestEndpoint.EXPECTED_HEADER_VAL);
    rbw.requestBuilder.setBody(TestEndpoint.EXPECTED_REQUEST_PAYLOAD);
    Span origSpan = Tracer.getInstance().startRequestWithRootSpan("overallReqSpan");
    Deque<Span> distributedTraceStackForCall = Tracer.getInstance().getCurrentSpanStackCopy();
    Map<String, String> mdcContextForCall = MDC.getCopyOfContextMap();
    resetTracingAndMdc();
    // when
    Response result = asyncClient.executeAsyncHttpRequest(rbw, response -> response, distributedTraceStackForCall, mdcContextForCall).join();
    // then
    assertThat(result.getStatusCode()).isEqualTo(200);
    assertThat(result.getResponseBody()).isEqualTo(TestEndpoint.RESPONSE_PAYLOAD);
    assertThat(result.getHeader(TraceHeaders.TRACE_ID)).isEqualTo(origSpan.getTraceId());
    // The async client should have surrounded the request in a subspan,
    //      so the parent ID sent to the downstream service should be the original span's span ID.
    assertThat(result.getHeader(TraceHeaders.PARENT_SPAN_ID)).isEqualTo(origSpan.getSpanId());
}
Also used : Response(com.ning.http.client.Response) Span(com.nike.wingtips.Span) RequestInfo(com.nike.riposte.server.http.RequestInfo) BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResponseInfo(com.nike.riposte.server.http.ResponseInfo) TraceHeaders(com.nike.wingtips.TraceHeaders) Tracer(com.nike.wingtips.Tracer) CompletableFuture(java.util.concurrent.CompletableFuture) ServerConfig(com.nike.riposte.server.config.ServerConfig) Deque(java.util.Deque) ServerSocket(java.net.ServerSocket) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Collections.singleton(java.util.Collections.singleton) ApiError(com.nike.backstopper.apierror.ApiError) After(org.junit.After) Map(java.util.Map) Server(com.nike.riposte.server.Server) AsyncHttpClientHelper(com.nike.riposte.client.asynchttp.ning.AsyncHttpClientHelper) Response(com.ning.http.client.Response) Before(org.junit.Before) AfterClass(org.junit.AfterClass) ApiException(com.nike.backstopper.exception.ApiException) Executor(java.util.concurrent.Executor) Collection(java.util.Collection) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test) IOException(java.io.IOException) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) UUID(java.util.UUID) ApiErrorBase(com.nike.backstopper.apierror.ApiErrorBase) Endpoint(com.nike.riposte.server.http.Endpoint) Matcher(com.nike.riposte.util.Matcher) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) MDC(org.slf4j.MDC) RequestBuilderWrapper(com.nike.riposte.client.asynchttp.ning.RequestBuilderWrapper) RequestBuilderWrapper(com.nike.riposte.client.asynchttp.ning.RequestBuilderWrapper) Span(com.nike.wingtips.Span) Test(org.junit.Test)

Example 22 with Response

use of org.apache.apex.shaded.ning19.com.ning.http.client.Response in project riposte by Nike-Inc.

the class AwsUtil method getAwsRegion.

/**
     * @param asyncHttpClientHelper The async HTTP client you want this method to use to make the AWS metadata call.
     *
     * @return A {@link CompletableFuture} that will contain the AWS region this app is running in (assuming it
     * completes successfully). If an error occurs retrieving the region from AWS then the error will be logged and
     * {@link AppInfo#UNKNOWN_VALUE} returned as the value.
     */
public static CompletableFuture<String> getAwsRegion(AsyncHttpClientHelper asyncHttpClientHelper) {
    return asyncHttpClientHelper.executeAsyncHttpRequest(asyncHttpClientHelper.getRequestBuilder(AMAZON_METADATA_DOCUMENT_URL, HttpMethod.GET), response -> {
        String region = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, String> resultMap = objectMapper.readValue(response.getResponseBody(), new TypeReference<Map<String, String>>() {
            });
            region = resultMap.get("region");
        } catch (Throwable t) {
            logger.error("Error retrieving region from AWS", t);
        }
        if (region == null) {
            logger.error("AWS metadata service returned null for region. Using 'unknown' as fallback.");
            region = AppInfo.UNKNOWN_VALUE;
        }
        return region;
    }).handle((region, error) -> {
        if (error != null) {
            logger.error("Unable to get region info from AWS metadata service.", error);
            return AppInfo.UNKNOWN_VALUE;
        }
        if (region == null) {
            logger.error("AWS metadata service returned null for region. Using 'unknown' as fallback.");
            region = AppInfo.UNKNOWN_VALUE;
        }
        return region;
    });
}
Also used : AppInfoImpl(com.nike.riposte.server.config.impl.AppInfoImpl) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) HttpMethod(io.netty.handler.codec.http.HttpMethod) CompletableFuture(java.util.concurrent.CompletableFuture) UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress) Map(java.util.Map) AppInfo(com.nike.riposte.server.config.AppInfo) TypeReference(com.fasterxml.jackson.core.type.TypeReference) AsyncHttpClientHelper(com.nike.riposte.client.asynchttp.ning.AsyncHttpClientHelper) Response(com.ning.http.client.Response) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 23 with Response

use of org.apache.apex.shaded.ning19.com.ning.http.client.Response in project killbill by killbill.

the class TestPlugin method testPassRequestsToKnownPluginButWrongPath.

@Test(groups = "slow")
public void testPassRequestsToKnownPluginButWrongPath() throws Exception {
    final String uri = TEST_PLUGIN_NAME + "/somethingSomething";
    Response response;
    response = killBillClient.pluginGET(uri);
    testAndResetAllMarkers(response, 200, new byte[] {}, false, false, false, false, false, false);
    response = killBillClient.pluginHEAD(uri);
    testAndResetAllMarkers(response, 204, new byte[] {}, false, false, false, false, false, false);
    response = killBillClient.pluginPOST(uri, null);
    testAndResetAllMarkers(response, 200, new byte[] {}, false, false, false, false, false, false);
    response = killBillClient.pluginPUT(uri, null);
    testAndResetAllMarkers(response, 200, new byte[] {}, false, false, false, false, false, false);
    response = killBillClient.pluginDELETE(uri);
    testAndResetAllMarkers(response, 200, new byte[] {}, false, false, false, false, false, false);
    response = killBillClient.pluginOPTIONS(uri);
    testAndResetAllMarkers(response, 200, new byte[] {}, false, false, false, false, false, false);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(com.ning.http.client.Response) Test(org.testng.annotations.Test)

Example 24 with Response

use of org.apache.apex.shaded.ning19.com.ning.http.client.Response in project killbill by killbill.

the class TestPlugin method testPassRequestsToUnknownPlugin.

@Test(groups = "slow")
public void testPassRequestsToUnknownPlugin() throws Exception {
    final String uri = "pluginDoesNotExist/something";
    Response response;
    // We don't test the output here as it is some Jetty specific HTML blurb
    response = killBillClient.pluginGET(uri);
    testAndResetAllMarkers(response, 404, null, false, false, false, false, false, false);
    response = killBillClient.pluginHEAD(uri);
    testAndResetAllMarkers(response, 404, null, false, false, false, false, false, false);
    response = killBillClient.pluginPOST(uri, null);
    testAndResetAllMarkers(response, 404, null, false, false, false, false, false, false);
    response = killBillClient.pluginPUT(uri, null);
    testAndResetAllMarkers(response, 404, null, false, false, false, false, false, false);
    response = killBillClient.pluginDELETE(uri);
    testAndResetAllMarkers(response, 404, null, false, false, false, false, false, false);
    response = killBillClient.pluginOPTIONS(uri);
    testAndResetAllMarkers(response, 404, null, false, false, false, false, false, false);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(com.ning.http.client.Response) Test(org.testng.annotations.Test)

Example 25 with Response

use of org.apache.apex.shaded.ning19.com.ning.http.client.Response in project pinot by linkedin.

the class JsonAsyncHttpPinotClientTransport method executeQueryAsync.

@Override
public Future<BrokerResponse> executeQueryAsync(String brokerAddress, final String query) {
    try {
        final JSONObject json = new JSONObject();
        json.put("pql", query);
        final String url = "http://" + brokerAddress + "/query";
        final Future<Response> response = _httpClient.preparePost(url).setBody(json.toString()).execute();
        return new BrokerResponseFuture(response, query, url);
    } catch (Exception e) {
        throw new PinotClientException(e);
    }
}
Also used : Response(com.ning.http.client.Response) JSONObject(org.json.JSONObject) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Response (com.ning.http.client.Response)33 Test (org.junit.Test)10 IOException (java.io.IOException)9 AsyncHttpClient (com.ning.http.client.AsyncHttpClient)7 Test (org.testng.annotations.Test)7 AsyncHttpClientConfig (com.ning.http.client.AsyncHttpClientConfig)4 Request (com.ning.http.client.Request)4 RequestBuilder (com.ning.http.client.RequestBuilder)4 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)4 HttpMethod (io.netty.handler.codec.http.HttpMethod)4 Map (java.util.Map)4 NodesInfoResponse (org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 CircuitBreaker (com.nike.fastbreak.CircuitBreaker)3 Span (com.nike.wingtips.Span)3 AsyncCompletionHandler (com.ning.http.client.AsyncCompletionHandler)3 NettyAsyncHttpProvider (com.ning.http.client.providers.netty.NettyAsyncHttpProvider)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 JsonObject (javax.json.JsonObject)3 JsonReader (javax.json.JsonReader)3