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());
}
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());
}
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());
}
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();
}
});
}
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);
}
Aggregations