Search in sources :

Example 11 with Response

use of com.ning.http.client.Response in project riposte by Nike-Inc.

the class AsyncHttpClientHelperTest method getRequestBuilder_with_circuit_breaker_args_sets_values_as_expected.

@DataProvider(value = { "CONNECT", "DELETE", "GET", "HEAD", "POST", "OPTIONS", "PUT", "PATCH", "TRACE", "FOO_METHOD_DOES_NOT_EXIST" }, splitBy = "\\|")
@Test
public void getRequestBuilder_with_circuit_breaker_args_sets_values_as_expected(String methodName) {
    CircuitBreaker<Response> cbMock = mock(CircuitBreaker.class);
    List<Pair<Optional<CircuitBreaker<Response>>, Boolean>> variations = Arrays.asList(Pair.of(Optional.empty(), true), Pair.of(Optional.empty(), false), Pair.of(Optional.of(cbMock), true), Pair.of(Optional.of(cbMock), false));
    variations.forEach(variation -> {
        String url = "http://localhost/some/path";
        HttpMethod method = HttpMethod.valueOf(methodName);
        Optional<CircuitBreaker<Response>> cbOpt = variation.getLeft();
        boolean disableCb = variation.getRight();
        RequestBuilderWrapper rbw = helperSpy.getRequestBuilder(url, method, cbOpt, disableCb);
        verifyRequestBuilderWrapperGeneratedAsExpected(rbw, url, methodName, cbOpt, disableCb);
    });
}
Also used : Response(com.ning.http.client.Response) CircuitBreaker(com.nike.fastbreak.CircuitBreaker) Matchers.anyString(org.mockito.Matchers.anyString) HttpMethod(io.netty.handler.codec.http.HttpMethod) Pair(com.nike.internal.util.Pair) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 12 with Response

use of com.ning.http.client.Response in project riposte by Nike-Inc.

the class AsyncCompletionHandlerWithTracingAndMdcSupportTest method onCompleted_handles_circuit_breaker_but_does_nothing_else_if_completableFutureResponse_is_already_completed.

@Test
public void onCompleted_handles_circuit_breaker_but_does_nothing_else_if_completableFutureResponse_is_already_completed() throws Throwable {
    // given
    CompletableFuture<String> cfMock = mock(CompletableFuture.class);
    Whitebox.setInternalState(handlerSpy, "completableFutureResponse", cfMock);
    doReturn(true).when(cfMock).isDone();
    // when
    Response ignoredResult = handlerSpy.onCompleted(responseMock);
    // then
    verify(circuitBreakerManualTaskMock).handleEvent(responseMock);
    verify(cfMock).isDone();
    verifyNoMoreInteractions(cfMock);
    assertThat(ignoredResult).isEqualTo(responseMock);
    verifyZeroInteractions(responseMock);
}
Also used : Response(com.ning.http.client.Response) Test(org.junit.Test)

Example 13 with Response

use of com.ning.http.client.Response in project riposte by Nike-Inc.

the class AsyncCompletionHandlerWithTracingAndMdcSupportTest method onCompleted_completes_completableFutureResponse_with_result_of_responseHandlerFunction.

@DataProvider(value = { "true", "false" })
@Test
public void onCompleted_completes_completableFutureResponse_with_result_of_responseHandlerFunction(boolean throwException) throws Throwable {
    // given
    Exception ex = new Exception("kaboom");
    if (throwException)
        doThrow(ex).when(responseHandlerFunctionMock).handleResponse(responseMock);
    // when
    Response ignoredResult = handlerSpy.onCompleted(responseMock);
    // then
    verify(responseHandlerFunctionMock).handleResponse(responseMock);
    if (throwException) {
        assertThat(completableFutureResponse).isCompletedExceptionally();
        assertThat(completableFutureResponse).hasFailedWithThrowableThat().isEqualTo(ex);
    } else {
        assertThat(completableFutureResponse).isCompleted();
        assertThat(completableFutureResponse.get()).isEqualTo(responseHandlerResult);
    }
    assertThat(ignoredResult).isEqualTo(responseMock);
    verifyZeroInteractions(responseMock);
}
Also used : Response(com.ning.http.client.Response) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 14 with Response

use of com.ning.http.client.Response in project apex-core by apache.

the class PubSubWebSocketClient method openConnectionAsync.

public void openConnectionAsync() throws IOException {
    throwable.set(null);
    if (loginUrl != null && userName != null && password != null) {
        // get the session key first before attempting web socket
        JSONObject json = new JSONObject();
        try {
            json.put("userName", userName);
            json.put("password", password);
        } catch (JSONException ex) {
            throw new RuntimeException(ex);
        }
        client.preparePost(loginUrl).setHeader("Content-Type", "application/json").setBody(json.toString()).execute(new AsyncCompletionHandler<Response>() {

            @Override
            public Response onCompleted(Response response) throws Exception {
                List<Cookie> cookies = response.getCookies();
                BoundRequestBuilder brb = client.prepareGet(uri.toString());
                if (cookies != null) {
                    for (Cookie cookie : cookies) {
                        brb.addCookie(cookie);
                    }
                }
                connection = brb.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new PubSubWebSocket()).build()).get();
                return response;
            }
        });
    } else {
        final PubSubWebSocket webSocket = new PubSubWebSocket() {

            @Override
            public void onOpen(WebSocket ws) {
                connection = ws;
                super.onOpen(ws);
            }
        };
        client.prepareGet(uri.toString()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(webSocket).build());
    }
}
Also used : Cookie(org.apache.apex.shaded.ning19.com.ning.http.client.cookie.Cookie) BoundRequestBuilder(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) JSONException(org.codehaus.jettison.json.JSONException) WebSocketUpgradeHandler(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketUpgradeHandler) TimeoutException(java.util.concurrent.TimeoutException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JsonParseException(org.codehaus.jackson.JsonParseException) JSONException(org.codehaus.jettison.json.JSONException) WebSocket(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocket) Response(org.apache.apex.shaded.ning19.com.ning.http.client.Response) BoundRequestBuilder(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) JSONObject(org.codehaus.jettison.json.JSONObject) List(java.util.List)

Example 15 with Response

use of com.ning.http.client.Response in project apex-core by apache.

the class PubSubWebSocketClient method openConnection.

/**
   * <p>openConnection.</p>
   *
   * @param timeoutMillis
   * @throws IOException
   * @throws ExecutionException
   * @throws InterruptedException
   * @throws TimeoutException
   */
public void openConnection(long timeoutMillis) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    throwable.set(null);
    List<Cookie> cookies = null;
    if (loginUrl != null && userName != null && password != null) {
        // get the session key first before attempting web socket
        JSONObject json = new JSONObject();
        try {
            json.put("userName", userName);
            json.put("password", password);
        } catch (JSONException ex) {
            throw new RuntimeException(ex);
        }
        Response response = client.preparePost(loginUrl).setHeader("Content-Type", "application/json").setBody(json.toString()).execute().get();
        cookies = response.getCookies();
    }
    BoundRequestBuilder brb = client.prepareGet(uri.toString());
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            brb.addCookie(cookie);
        }
    }
    connection = brb.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new PubSubWebSocket()).build()).get(timeoutMillis, TimeUnit.MILLISECONDS);
}
Also used : Cookie(org.apache.apex.shaded.ning19.com.ning.http.client.cookie.Cookie) Response(org.apache.apex.shaded.ning19.com.ning.http.client.Response) BoundRequestBuilder(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) JSONObject(org.codehaus.jettison.json.JSONObject) JSONException(org.codehaus.jettison.json.JSONException) WebSocketUpgradeHandler(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketUpgradeHandler)

Aggregations

Response (com.ning.http.client.Response)30 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 CircuitBreaker (com.nike.fastbreak.CircuitBreaker)3 Span (com.nike.wingtips.Span)3 NettyAsyncHttpProvider (com.ning.http.client.providers.netty.NettyAsyncHttpProvider)3 UnknownHostException (java.net.UnknownHostException)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 KillBillClientException (org.killbill.billing.client.KillBillClientException)3 RoleDefinition (org.killbill.billing.client.model.RoleDefinition)3 UserRoles (org.killbill.billing.client.model.UserRoles)3