use of io.vertx.core.http.HttpMethod in project vert.x by eclipse.
the class HttpTest method testFollowRedirect.
private void testFollowRedirect(HttpMethod method, HttpMethod expectedMethod, int statusCode, int expectedStatus, int expectedRequests, String location, String expectedURI) throws Exception {
String s;
if (createBaseServerOptions().isSsl() && location.startsWith("http://")) {
s = "https://" + location.substring("http://".length());
} else {
s = location;
}
String t;
if (createBaseServerOptions().isSsl() && expectedURI.startsWith("http://")) {
t = "https://" + expectedURI.substring("http://".length());
} else {
t = expectedURI;
}
AtomicInteger numRequests = new AtomicInteger();
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
if (numRequests.getAndIncrement() == 0) {
resp.setStatusCode(statusCode);
if (s != null) {
resp.putHeader(HttpHeaders.LOCATION, s);
}
resp.end();
} else {
assertEquals(t, req.absoluteURI());
assertEquals("foo_value", req.getHeader("foo"));
assertEquals(expectedMethod, req.method());
resp.end();
}
});
startServer();
client.request(method, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
assertEquals(resp.request().absoluteURI(), t);
assertEquals(expectedRequests, numRequests.get());
assertEquals(expectedStatus, resp.statusCode());
testComplete();
}).putHeader("foo", "foo_value").setFollowRedirects(true).end();
await();
}
use of io.vertx.core.http.HttpMethod in project java-chassis by ServiceComb.
the class RestUtils method httpDo.
public static void httpDo(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;
}
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(ServiceRegistryConfig.INSTANCE.getRequestTimeout()).exceptionHandler(e -> {
LOGGER.error("{} {} fail, endpoint is {}:{}, message: {}", httpMethod, url.toString(), ipPort.getHostOrIp(), ipPort.getPort(), e.getMessage());
responseHandler.handle(new RestResponse(requestContext, null));
});
addDefaultHeaders(httpClientRequest);
if (requestParam.getHeaders() != null && requestParam.getHeaders().size() > 0) {
for (Map.Entry<String, String> header : requestParam.getHeaders().entrySet()) {
httpClientRequest.putHeader(header.getKey(), header.getValue());
}
}
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());
}
if (httpMethod != HttpMethod.GET && requestParam.getBody() != null && requestParam.getBody().length > 0) {
httpClientRequest.end(Buffer.buffer(requestParam.getBody()));
} else {
httpClientRequest.end();
}
});
}
Aggregations