use of io.undertow.util.HttpString in project light-codegen by networknt.
the class FrameworkListHandler method handle.
/**
* Returns a JSON list of all available frameworks defined in the service.yml of codegen-cli.
* If any issues occur with converting the result to JSON, an empty string is returned (not an empty json list).
*/
@Override
public ByteBuffer handle(HttpServerExchange exchange, Object input) {
String result = "";
exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json");
try {
result = Config.getInstance().getMapper().writeValueAsString(frameworks);
} catch (JsonProcessingException e) {
// return empty back in this case.
}
return NioUtils.toByteBuffer(result);
}
use of io.undertow.util.HttpString in project light-rest-4j by networknt.
the class RequestValidatorTest method runTest.
public void runTest(String requestPath, String expectedValue, Map<String, String> headers, Map<String, String> cookies) 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:7080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
try {
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(requestPath);
request.getRequestHeaders().put(Headers.HOST, "localhost");
if (!headers.isEmpty()) {
headers.entrySet().forEach(entry -> request.getRequestHeaders().put(new HttpString(entry.getKey()), entry.getValue()));
}
if (!cookies.isEmpty()) {
List<String> cookieItems = new ArrayList<>();
cookies.entrySet().forEach(entry -> cookieItems.add(String.format("%s=%s", entry.getKey(), entry.getValue())));
request.getRequestHeaders().put(Headers.COOKIE, String.join(";", cookieItems));
}
connection.sendRequest(request, client.createClientCallback(reference, latch, ""));
}
});
latch.await(60, TimeUnit.SECONDS);
} catch (Exception e) {
logger.error("IOException: ", e);
throw new ClientException(e);
} finally {
IoUtils.safeClose(connection);
}
ClientResponse resp = reference.get();
int statusCode = resp.getResponseCode();
Assert.assertEquals(200, statusCode);
if (statusCode == 200) {
String body = resp.getAttachment(Http2Client.RESPONSE_BODY);
Assert.assertNotNull(body);
Assert.assertEquals(expectedValue, body);
}
}
use of io.undertow.util.HttpString in project light-rest-4j by networknt.
the class ValidatorHandlerTest method testNoResponseContentValidation.
@Test
public void testNoResponseContentValidation() throws ClientException, URISyntaxException, ExecutionException, InterruptedException, TimeoutException {
ClientRequest clientRequest = new ClientRequest();
clientRequest.getRequestHeaders().put(new HttpString("todo_Header1"), "header_1");
CompletableFuture<ClientResponse> future = sendResponse(clientRequest, "");
String statusCode = future.get().getStatus();
Assert.assertNotEquals("OK", statusCode);
}
use of io.undertow.util.HttpString in project light-rest-4j by networknt.
the class ValidatorHandlerTest method testResponseContentValidationWithNoError.
@Test
public void testResponseContentValidationWithNoError() throws ClientException, URISyntaxException, ExecutionException, InterruptedException {
ClientRequest clientRequest = new ClientRequest();
clientRequest.getRequestHeaders().put(new HttpString("todo_Header1"), "header_1");
CompletableFuture<ClientResponse> future = sendResponse(clientRequest, "response1");
String statusCode = future.get().getStatus();
Assert.assertEquals("OK", statusCode);
List<String> errorLines = getErrorLinesFromLogFile();
Assert.assertTrue(errorLines.size() == 0);
}
use of io.undertow.util.HttpString in project light-rest-4j by networknt.
the class ValidatorHandlerTest method testIssue57.
/**
* Pass a query parameter all which is boolean type.
* @throws Exception
*/
@Test
public void testIssue57() throws Exception {
final Http2Client client = Http2Client.getInstance();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:7080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
try {
ClientRequest request = new ClientRequest().setPath("/v1/pets?all=false").setMethod(Methods.GET);
request.getRequestHeaders().put(Headers.HOST, "localhost");
request.getRequestHeaders().put(new HttpString("accessId"), "accessId");
request.getRequestHeaders().put(new HttpString("requestId"), "requestId");
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();
Assert.assertEquals(200, statusCode);
}
Aggregations