use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class HttpTest method testFollowRedirectWithChunkedBody.
@Test
public void testFollowRedirectWithChunkedBody() throws Exception {
Buffer buff1 = Buffer.buffer(TestUtils.randomAlphaString(2048));
Buffer buff2 = Buffer.buffer(TestUtils.randomAlphaString(2048));
Buffer expected = Buffer.buffer().appendBuffer(buff1).appendBuffer(buff2);
AtomicBoolean redirected = new AtomicBoolean();
CountDownLatch latch = new CountDownLatch(1);
server.requestHandler(req -> {
boolean redirect = redirected.compareAndSet(false, true);
if (redirect) {
latch.countDown();
}
req.bodyHandler(body -> {
assertEquals(HttpMethod.PUT, req.method());
assertEquals(body, expected);
if (redirect) {
req.response().setStatusCode(307).putHeader(HttpHeaders.LOCATION, "http://localhost:8080/whatever").end();
} else {
req.response().end();
}
});
});
startServer();
HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
assertEquals(200, resp.statusCode());
testComplete();
}).setFollowRedirects(true).setChunked(true).write(buff1);
awaitLatch(latch);
req.end(buff2);
await();
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class HttpTest method test100ContinueHandledManually.
@Test
public void test100ContinueHandledManually() throws Exception {
server.close();
server = vertx.createHttpServer(createBaseServerOptions());
Buffer toSend = TestUtils.randomBuffer(1000);
server.requestHandler(req -> {
assertEquals("100-continue", req.getHeader("expect"));
req.response().writeContinue();
req.bodyHandler(data -> {
assertEquals(toSend, data);
req.response().end();
});
});
server.listen(onSuccess(s -> {
HttpClientRequest req = client.request(HttpMethod.PUT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
resp.endHandler(v -> testComplete());
});
req.headers().set("Expect", "100-continue");
req.setChunked(true);
req.continueHandler(v -> {
req.write(toSend);
req.end();
});
req.sendHead();
}));
await();
}
use of io.vertx.core.http.HttpClientRequest in project java-chassis by ServiceComb.
the class GrpcTransport method send.
@Override
public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
HttpClientWithContext httpClientWithContext = clientMgr.findThreadBindClientPool();
OperationMeta operationMeta = invocation.getOperationMeta();
OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(operationMeta);
String cseContext = JsonUtils.writeValueAsString(invocation.getContext());
// 在verticle之外的线程调用
IpPort ipPort = (IpPort) invocation.getEndpoint().getAddress();
Buffer requestBuf = GrpcCodec.encodeRequest(invocation, operationProtobuf);
String url = "/" + invocation.getSchemaId() + "/" + operationMeta.getOperationId();
Handler<HttpClientResponse> responseHandler = httpResponse -> {
httpResponse.bodyHandler(responseBuf -> {
invocation.getResponseExecutor().execute(() -> {
try {
Response response = GrpcCodec.decodeResponse(invocation, operationProtobuf, httpResponse, responseBuf);
ResponseMeta responseMeta = operationMeta.findResponseMeta(response.getStatusCode());
for (String headerName : responseMeta.getHeaders().keySet()) {
for (String value : httpResponse.headers().getAll(headerName)) {
response.getHeaders().addHeader(headerName, value);
}
}
asyncResp.complete(response);
} catch (Exception e) {
asyncResp.fail(invocation.getInvocationType(), e);
}
});
});
};
// 从业务线程转移到网络线程中去发送
httpClientWithContext.runOnContext(httpClient -> {
HttpClientRequest httpClientRequest = httpClient.post(ipPort.getPort(), ipPort.getHostOrIp(), url, responseHandler);
httpClientRequest.exceptionHandler(e -> {
asyncResp.fail(invocation.getInvocationType(), e);
});
httpClientRequest.setTimeout(AbstractTransport.getRequestTimeout());
httpClientRequest.putHeader(HEADER_CONTENT_TYPE, "application/grpc").putHeader(HEADER_TE, "trailers").putHeader(HEADER_USER_AGENT, "cse-client/1.0.0").putHeader(Const.CSE_CONTEXT, cseContext).putHeader(Const.DEST_MICROSERVICE, invocation.getMicroserviceName()).end(requestBuf);
});
}
use of io.vertx.core.http.HttpClientRequest in project java-chassis by ServiceComb.
the class TestVertxPutMethod method testVertxPutMethod.
@Test
public void testVertxPutMethod() {
HttpClient client = Mockito.mock(HttpClient.class);
Invocation invocation = Mockito.mock(Invocation.class);
IpPort ipPort = Mockito.mock(IpPort.class);
RestOperationMeta operation = Mockito.mock(RestOperationMeta.class);
AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
Mockito.when(ipPort.getHostOrIp()).thenReturn("test");
assertNotNull("test", ipPort.getHostOrIp());
Mockito.when(ipPort.getPort()).thenReturn(13);
assertEquals(13, ipPort.getPort());
HttpClientRequest obj = VertxPutMethod.INSTANCE.createRequest(client, invocation, ipPort, "testCall", operation, asyncResp);
Assert.assertNull(obj);
}
use of io.vertx.core.http.HttpClientRequest in project java-chassis by ServiceComb.
the class VertxHttpMethod method doMethod.
public void doMethod(HttpClientWithContext httpClientWithContext, Invocation invocation, AsyncResponse asyncResp) throws Exception {
OperationMeta operationMeta = invocation.getOperationMeta();
RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
String path = this.createRequestPath(invocation, swaggerRestOperation);
IpPort ipPort = (IpPort) invocation.getEndpoint().getAddress();
HttpClientRequest clientRequest = this.createRequest(httpClientWithContext.getHttpClient(), invocation, ipPort, path, swaggerRestOperation, asyncResp);
RestClientRequestImpl restClientRequest = new RestClientRequestImpl(clientRequest);
RestCodec.argsToRest(invocation.getArgs(), swaggerRestOperation, restClientRequest);
clientRequest.exceptionHandler(e -> {
LOGGER.error(e.toString());
asyncResp.fail(invocation.getInvocationType(), e);
});
// 从业务线程转移到网络线程中去发送
httpClientWithContext.runOnContext(httpClient -> {
this.setCseContext(invocation, clientRequest);
clientRequest.setTimeout(AbstractTransport.getRequestTimeout());
try {
restClientRequest.end();
} catch (Exception e) {
LOGGER.error("send http reqeust failed,", e);
asyncResp.fail(invocation.getInvocationType(), e);
}
});
}
Aggregations