use of io.undertow.util.HttpString in project light-rest-4j by networknt.
the class ValidatorHandlerTest method testResponseHeaderValidationWithError.
@Test
public void testResponseHeaderValidationWithError() throws ClientException, URISyntaxException, ExecutionException, InterruptedException {
ClientRequest clientRequest = new ClientRequest();
clientRequest.getRequestHeaders().put(new HttpString("todo_Header1"), "header_1");
clientRequest.getRequestHeaders().put(new HttpString("todo_Header2"), "header_2");
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 testResponseContentValidationWithError.
@Test
public void testResponseContentValidationWithError() throws ClientException, URISyntaxException, ExecutionException, InterruptedException, TimeoutException {
ClientRequest clientRequest = new ClientRequest();
clientRequest.getRequestHeaders().put(new HttpString("todo_Header1"), "header_1");
CompletableFuture<ClientResponse> future = sendResponse(clientRequest, "response2");
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 testInvalidMaximumHeaders.
@Test
public void testInvalidMaximumHeaders() 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 {
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("/v1/pets");
request.getRequestHeaders().put(Headers.HOST, "localhost");
request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
request.getRequestHeaders().put(new HttpString("accessId"), "001");
// the maximum for the request is 64 in the spec.
request.getRequestHeaders().put(new HttpString("requestId"), "65");
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(400, statusCode);
if (statusCode == 400) {
Status status = Config.getInstance().getMapper().readValue(body, Status.class);
Assert.assertNotNull(status);
Assert.assertEquals("ERR11004", status.getCode());
}
}
use of io.undertow.util.HttpString in project light-rest-4j by networknt.
the class IntegrationTest 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);
}
int statusCode = reference.get().getResponseCode();
Assert.assertEquals(200, statusCode);
if (statusCode == 200) {
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
Assert.assertNotNull(body);
Assert.assertEquals(expectedValue, body);
}
}
use of io.undertow.util.HttpString in project light-4j by networknt.
the class JaegerHandler method handleRequest.
/**
* Extract the context, start and stop the span here.
*
* @param exchange HttpServerExchange
* @throws Exception Exception
*/
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
// get the path and method to construct the endpoint for the operation of tracing.
Map<String, Object> auditInfo = exchange.getAttachment(AttachmentConstants.AUDIT_INFO);
String endpoint = null;
if (auditInfo != null) {
endpoint = (String) auditInfo.get(Constants.ENDPOINT_STRING);
} else {
endpoint = exchange.getRequestPath() + "@" + exchange.getRequestMethod();
}
HeaderMap headerMap = exchange.getRequestHeaders();
final HashMap<String, String> headers = new HashMap<>();
for (HttpString key : headerMap.getHeaderNames()) {
headers.put(key.toString(), headerMap.getFirst(key));
}
TextMap carrier = new TextMapAdapter(headers);
// start the server span.
Tracer.SpanBuilder spanBuilder;
try {
SpanContext parentSpanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, carrier);
if (parentSpanCtx == null) {
spanBuilder = tracer.buildSpan(endpoint);
} else {
spanBuilder = tracer.buildSpan(endpoint).asChildOf(parentSpanCtx);
}
} catch (IllegalArgumentException e) {
spanBuilder = tracer.buildSpan(endpoint);
}
Span rootSpan = spanBuilder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER).withTag(Tags.PEER_HOSTNAME.getKey(), NetUtils.getLocalAddressByDatagram()).withTag(Tags.PEER_PORT.getKey(), Server.getServerConfig().getHttpsPort()).start();
tracer.activateSpan(rootSpan);
// This can be retrieved in the business handler to add tags and logs for tracing.
exchange.putAttachment(ROOT_SPAN, rootSpan);
// The client module can use this to inject tracer.
exchange.putAttachment(EXCHANGE_TRACER, tracer);
// add an exchange complete listener to close the Root Span for the request.
exchange.addExchangeCompleteListener((exchange1, nextListener) -> {
Span span = exchange1.getAttachment(ROOT_SPAN);
if (span != null) {
span.finish();
}
nextListener.proceed();
});
Handler.next(exchange, next);
}
Aggregations