use of io.undertow.client.ClientResponse in project light-4j by networknt.
the class CorsHttpHandlerTest method testOptionsWrongOrigin.
@Test
public void testOptionsWrongOrigin() throws Exception {
String url = "http://localhost:8080";
Http2Client client = Http2Client.getInstance();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
} catch (Exception e) {
throw new ClientException(e);
}
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
try {
ClientRequest request = new ClientRequest().setPath("/").setMethod(Methods.OPTIONS);
request.getRequestHeaders().put(new HttpString("Origin"), "http://example.com");
request.getRequestHeaders().put(new HttpString("Access-Control-Request-Method"), "POST");
request.getRequestHeaders().put(new HttpString("Access-Control-Request-Headers"), "X-Requested-With");
connection.sendRequest(request, client.createClientCallback(reference, latch));
latch.await();
} catch (Exception e) {
logger.error("Exception: ", e);
throw new ClientException(e);
} finally {
IoUtils.safeClose(connection);
}
int statusCode = reference.get().getResponseCode();
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
HeaderMap headerMap = reference.get().getResponseHeaders();
String header = headerMap.getFirst("Access-Control-Allow-Origin");
Assert.assertEquals(200, statusCode);
if (statusCode == 200) {
Assert.assertNull(header);
}
}
use of io.undertow.client.ClientResponse in project light-4j by networknt.
the class HealthGetHandlerTest method testHealth.
@Test
public void testHealth() throws Exception {
final Http2Client client = Http2Client.getInstance();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
try {
ClientRequest request = new ClientRequest().setPath("/server/health").setMethod(Methods.GET);
connection.sendRequest(request, client.createClientCallback(reference, latch));
latch.await();
} catch (Exception e) {
logger.error("Exception: ", e);
throw new ClientException(e);
} finally {
IoUtils.safeClose(connection);
}
int statusCode = reference.get().getResponseCode();
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
Assert.assertEquals(200, statusCode);
Assert.assertEquals("OK", body);
}
use of io.undertow.client.ClientResponse in project light-4j by networknt.
the class ServerInfoDisabledTest method testServerInfo.
@Test
public void testServerInfo() throws Exception {
final Http2Client client = Http2Client.getInstance();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
try {
ClientRequest request = new ClientRequest().setPath("/v1/server/info").setMethod(Methods.GET);
connection.sendRequest(request, client.createClientCallback(reference, latch));
latch.await();
} catch (Exception e) {
logger.error("Exception: ", e);
throw new ClientException(e);
} finally {
IoUtils.safeClose(connection);
}
int statusCode = reference.get().getResponseCode();
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
Assert.assertEquals(404, statusCode);
if (statusCode == 404) {
Status status = Config.getInstance().getMapper().readValue(body, Status.class);
Assert.assertNotNull(status);
Assert.assertEquals("ERR10013", status.getCode());
}
}
use of io.undertow.client.ClientResponse in project light-4j by networknt.
the class LimitHandlerTest method testOneRequest.
@Test
public void testOneRequest() throws Exception {
final Http2Client client = Http2Client.getInstance();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
} catch (Exception e) {
throw new ClientException(e);
}
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
try {
ClientRequest request = new ClientRequest().setPath("/").setMethod(Methods.GET);
connection.sendRequest(request, client.createClientCallback(reference, latch));
latch.await();
} catch (Exception e) {
logger.error("Exception: ", e);
throw new ClientException(e);
} finally {
IoUtils.safeClose(connection);
}
int statusCode = reference.get().getResponseCode();
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
Assert.assertEquals(200, statusCode);
if (statusCode == 200) {
Assert.assertEquals("OK", body);
}
}
use of io.undertow.client.ClientResponse in project light-4j by networknt.
the class ConsulClientImpl method registerService.
@Override
public void registerService(ConsulService service, String token) {
String json = service.toString();
String path = "/v1/agent/service/register";
ClientConnection connection = null;
try {
connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
logger.error("Exeption:", e);
}
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
try {
ClientRequest request = new ClientRequest().setMethod(Methods.PUT).setPath(path);
if (token != null)
request.getRequestHeaders().put(Constants.CONSUL_TOKEN, token);
request.getRequestHeaders().put(Headers.HOST, "localhost");
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
connection.sendRequest(request, client.createClientCallback(reference, latch, json));
latch.await();
int statusCode = reference.get().getResponseCode();
if (statusCode >= 300) {
throw new Exception("Failed to register on Consul: " + statusCode);
}
} catch (Exception e) {
logger.error("Exception:", e);
} finally {
IoUtils.safeClose(connection);
}
}
Aggregations