Search in sources :

Example 6 with HttpClientWithContext

use of org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext in project incubator-servicecomb-java-chassis by apache.

the class TestClientPoolManager method findByContext_normalThread.

@Test
public void findByContext_normalThread() {
    HttpClientWithContext pool = new HttpClientWithContext(null, null);
    pools.add(pool);
    new Expectations(VertxImpl.class) {

        {
            VertxImpl.context();
            result = null;
        }
    };
    Assert.assertSame(pool, poolMgr.findByContext());
}
Also used : Expectations(mockit.Expectations) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) Test(org.junit.Test)

Example 7 with HttpClientWithContext

use of org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext in project incubator-servicecomb-java-chassis by apache.

the class TestClientPoolManager method findByContext_wrongContext_reverse.

@Test
public void findByContext_wrongContext_reverse() {
    HttpClientWithContext pool1 = new HttpClientWithContext(null, null);
    HttpClientWithContext pool2 = new HttpClientWithContext(null, null);
    pools.add(pool1);
    pools.add(pool2);
    new Expectations(VertxImpl.class) {

        {
            VertxImpl.context();
            result = null;
        }
    };
    AtomicInteger reactiveNextIndex = Deencapsulation.getField(poolMgr, "reactiveNextIndex");
    reactiveNextIndex.set(Integer.MAX_VALUE);
    // each time invoke find, reactiveNextIndex will inc 1
    Assert.assertSame(pool2, poolMgr.findByContext());
    Assert.assertSame(pool1, poolMgr.findByContext());
    Assert.assertSame(pool2, poolMgr.findByContext());
    Assert.assertSame(pool1, poolMgr.findByContext());
}
Also used : Expectations(mockit.Expectations) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) Test(org.junit.Test)

Example 8 with HttpClientWithContext

use of org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext in project incubator-servicecomb-java-chassis by apache.

the class TestClientPoolManager method findByContext_woker.

@Test
public void findByContext_woker(@Mocked Context workerContext) {
    HttpClientWithContext pool = new HttpClientWithContext(null, null);
    pools.add(pool);
    new Expectations(VertxImpl.class) {

        {
            VertxImpl.context();
            result = workerContext;
            workerContext.owner();
            result = vertx;
            workerContext.isEventLoopContext();
            result = false;
        }
    };
    Assert.assertSame(pool, poolMgr.findByContext());
}
Also used : Expectations(mockit.Expectations) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) Test(org.junit.Test)

Example 9 with HttpClientWithContext

use of org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext in project incubator-servicecomb-java-chassis by apache.

the class RestUtils method httpDo.

public static void httpDo(long timeout, RequestContext requestContext, Handler<RestResponse> responseHandler) {
    HttpClientWithContext vertxHttpClient = HttpClientPool.INSTANCE.getClient();
    vertxHttpClient.runOnContext(httpClient -> {
        IpPort ipPort = requestContext.getIpPort();
        HttpMethod httpMethod = requestContext.getMethod();
        RequestParam requestParam = requestContext.getParams();
        if (ipPort == null) {
            LOGGER.error("request address is null");
            responseHandler.handle(new RestResponse(requestContext, null));
            return;
        }
        // query params
        StringBuilder url = new StringBuilder(requestContext.getUri());
        String queryParams = requestParam.getQueryParams();
        if (!queryParams.isEmpty()) {
            url.append(url.lastIndexOf("?") > 0 ? "&" : "?").append(queryParams);
        }
        HttpClientRequest httpClientRequest = httpClient.request(httpMethod, ipPort.getPort(), ipPort.getHostOrIp(), url.toString(), response -> {
            responseHandler.handle(new RestResponse(requestContext, response));
        });
        httpClientRequest.setTimeout(timeout).exceptionHandler(e -> {
            LOGGER.error("{} {} fail, endpoint is {}:{}, message: {}", httpMethod, url.toString(), ipPort.getHostOrIp(), ipPort.getPort(), e.getMessage());
            responseHandler.handle(new RestResponse(requestContext, null));
        });
        // headers
        Map<String, String> headers = defaultHeaders();
        httpClientRequest.headers().addAll(headers);
        if (requestParam.getHeaders() != null && requestParam.getHeaders().size() > 0) {
            headers.putAll(requestParam.getHeaders());
            for (Map.Entry<String, String> header : requestParam.getHeaders().entrySet()) {
                httpClientRequest.putHeader(header.getKey(), header.getValue());
            }
        }
        // cookies header
        if (requestParam.getCookies() != null && requestParam.getCookies().size() > 0) {
            StringBuilder stringBuilder = new StringBuilder();
            for (Map.Entry<String, String> cookie : requestParam.getCookies().entrySet()) {
                stringBuilder.append(cookie.getKey()).append("=").append(cookie.getValue()).append("; ");
            }
            httpClientRequest.putHeader("Cookie", stringBuilder.toString());
            headers.put("Cookie", stringBuilder.toString());
        }
        // SignAuth
        SignRequest signReq = createSignRequest(requestContext.getMethod().toString(), requestContext.getIpPort(), requestContext.getParams(), url.toString(), headers);
        httpClientRequest.headers().addAll(getSignAuthHeaders(signReq));
        // body
        if (httpMethod != HttpMethod.GET && requestParam.getBody() != null && requestParam.getBody().length > 0) {
            httpClientRequest.end(Buffer.buffer(requestParam.getBody()));
        } else {
            httpClientRequest.end();
        }
    });
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) SignRequest(org.apache.servicecomb.foundation.auth.SignRequest) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) MultiMap(io.vertx.core.MultiMap) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(io.vertx.core.http.HttpMethod)

Example 10 with HttpClientWithContext

use of org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext in project incubator-servicecomb-java-chassis by apache.

the class TestVertxHttpMethod method testDoMethod.

@Test
public void testDoMethod(@Mocked HttpClient httpClient, @Injectable URIEndpointObject address) throws Exception {
    Context context = new MockUp<Context>() {

        @Mock
        public void runOnContext(Handler<Void> action) {
            action.handle(null);
        }
    }.getMockInstance();
    HttpClientWithContext httpClientWithContext = new HttpClientWithContext(httpClient, context);
    Invocation invocation = mock(Invocation.class);
    AsyncResponse asyncResp = mock(AsyncResponse.class);
    OperationMeta operationMeta = mock(OperationMeta.class);
    RestOperationMeta swaggerRestOperation = mock(RestOperationMeta.class);
    Endpoint endpoint = mock(Endpoint.class);
    when(invocation.getOperationMeta()).thenReturn(operationMeta);
    URLPathBuilder urlPathBuilder = mock(URLPathBuilder.class);
    when(swaggerRestOperation.getPathBuilder()).thenReturn(urlPathBuilder);
    operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
    when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerRestOperation);
    when(invocation.getEndpoint()).thenReturn(endpoint);
    when(endpoint.getAddress()).thenReturn(address);
    when(request.exceptionHandler(Mockito.any())).then(answer -> null);
    Map<String, Object> map = new HashMap<>();
    when(invocation.getHandlerContext()).then(answer -> map);
    ;
    this.doMethod(httpClientWithContext, invocation, asyncResp);
    Assert.assertTrue(true);
}
Also used : Context(io.vertx.core.Context) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) Invocation(org.apache.servicecomb.core.Invocation) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) HashMap(java.util.HashMap) Mock(mockit.Mock) Endpoint(org.apache.servicecomb.core.Endpoint) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) URLPathBuilder(org.apache.servicecomb.common.rest.definition.path.URLPathBuilder) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Test(org.junit.Test)

Aggregations

HttpClientWithContext (org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext)10 Test (org.junit.Test)8 Expectations (mockit.Expectations)6 HashMap (java.util.HashMap)3 Mock (mockit.Mock)3 Context (io.vertx.core.Context)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Invocation (org.apache.servicecomb.core.Invocation)2 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)2 MultiMap (io.vertx.core.MultiMap)1 HttpClientRequest (io.vertx.core.http.HttpClientRequest)1 HttpMethod (io.vertx.core.http.HttpMethod)1 JsonObject (io.vertx.core.json.JsonObject)1 Map (java.util.Map)1 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)1 URLPathBuilder (org.apache.servicecomb.common.rest.definition.path.URLPathBuilder)1 Endpoint (org.apache.servicecomb.core.Endpoint)1 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)1 SignRequest (org.apache.servicecomb.foundation.auth.SignRequest)1 IpPort (org.apache.servicecomb.foundation.common.net.IpPort)1