use of io.undertow.client.ClientResponse in project light-rest-4j by networknt.
the class ValidatorHandlerTest method testInvalidPost.
@Test
public void testInvalidPost() throws Exception {
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
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);
}
try {
String post = "{\"name\":\"Pinky\", \"photoUrl\": \"http://www.photo.com/1.jpg\"}";
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath("/post");
request.getRequestHeaders().put(Headers.HOST, "localhost");
request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
connection.sendRequest(request, client.createClientCallback(reference, latch, post));
}
});
latch.await(10, TimeUnit.SECONDS);
} catch (Exception e) {
logger.error("IOException: ", 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("ERR10007", status.getCode());
}
}
use of io.undertow.client.ClientResponse in project light-rest-4j by networknt.
the class ValidatorHandlerTest method testDeleteWithHeader.
@Test
public void testDeleteWithHeader() 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("/v2/pet/111").setMethod(Methods.DELETE);
request.getRequestHeaders().put(new HttpString("api_key"), "key");
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.assertNotNull(body);
Assert.assertEquals("deletePet", body);
}
}
use of io.undertow.client.ClientResponse in project light-rest-4j by networknt.
the class ValidatorHandlerTest method testValidPost.
@Test
public void testValidPost() throws Exception {
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
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);
}
try {
String post = "{\"id\":0,\"category\":{\"id\":0,\"name\":\"string\"},\"name\":\"doggie\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":0,\"name\":\"string\"}],\"status\":\"available\"}";
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath("/v2/pet");
request.getRequestHeaders().put(Headers.HOST, "localhost");
request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
connection.sendRequest(request, client.createClientCallback(reference, latch, post));
}
});
latch.await(10, TimeUnit.SECONDS);
} catch (Exception e) {
logger.error("IOException: ", 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.assertNotNull(body);
Assert.assertEquals("addPet", body);
}
}
use of io.undertow.client.ClientResponse in project light-4j by networknt.
the class ConsulClientImpl method unregisterService.
@Override
public void unregisterService(String serviceId, String token) {
String path = "/v1/agent/service/deregister/" + serviceId;
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);
request.getRequestHeaders().put(Headers.HOST, "localhost");
if (token != null)
request.getRequestHeaders().put(Constants.CONSUL_TOKEN, token);
connection.sendRequest(request, client.createClientCallback(reference, latch));
latch.await();
int statusCode = reference.get().getResponseCode();
if (statusCode >= 300) {
System.out.println("body = " + reference.get().getAttachment(Http2Client.RESPONSE_BODY));
throw new Exception("Failed to unregister on Consul: " + statusCode);
}
} catch (Exception e) {
logger.error("Exception:", e);
} finally {
IoUtils.safeClose(connection);
}
}
use of io.undertow.client.ClientResponse in project light-4j by networknt.
the class ConsulClientImpl method checkPass.
@Override
public void checkPass(String serviceId, String token) {
if (logger.isDebugEnabled())
logger.debug("checkPass serviceId = " + serviceId);
String path = "/v1/agent/check/pass/" + "service:" + serviceId;
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);
request.getRequestHeaders().put(Headers.HOST, "localhost");
if (token != null)
request.getRequestHeaders().put(Constants.CONSUL_TOKEN, token);
connection.sendRequest(request, client.createClientCallback(reference, latch));
latch.await();
int statusCode = reference.get().getResponseCode();
if (statusCode >= 300) {
logger.error("Failed to checkPass on Consul: " + statusCode + ":" + reference.get().getAttachment(Http2Client.RESPONSE_BODY));
throw new Exception("Failed to checkPass on Consul: " + statusCode + ":" + reference.get().getAttachment(Http2Client.RESPONSE_BODY));
}
} catch (Exception e) {
logger.error("CheckPass request exception", e);
} finally {
IoUtils.safeClose(connection);
}
}
Aggregations