Search in sources :

Example 66 with HttpString

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);
}
Also used : HttpString(io.undertow.util.HttpString) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HttpString(io.undertow.util.HttpString)

Example 67 with HttpString

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);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) ClientException(com.networknt.exception.ClientException) RoutingHandler(io.undertow.server.RoutingHandler) Handlers(io.undertow.Handlers) BeforeClass(org.junit.BeforeClass) LoggerFactory(org.slf4j.LoggerFactory) HttpServerExchange(io.undertow.server.HttpServerExchange) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) OptionMap(org.xnio.OptionMap) Undertow(io.undertow.Undertow) HttpString(io.undertow.util.HttpString) BodyHandler(com.networknt.body.BodyHandler) Map(java.util.Map) URI(java.net.URI) ClientResponse(io.undertow.client.ClientResponse) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) Collection(java.util.Collection) Test(org.junit.Test) HttpHandler(io.undertow.server.HttpHandler) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ClientConnection(io.undertow.client.ClientConnection) StringUtils(com.networknt.utility.StringUtils) ClientRequest(io.undertow.client.ClientRequest) Headers(io.undertow.util.Headers) Methods(io.undertow.util.Methods) Assert(org.junit.Assert) Http2Client(com.networknt.client.Http2Client) Collections(java.util.Collections) IoUtils(org.xnio.IoUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpString(io.undertow.util.HttpString) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) ClientConnection(io.undertow.client.ClientConnection) ArrayList(java.util.ArrayList) List(java.util.List) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest) HttpString(io.undertow.util.HttpString)

Example 68 with HttpString

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);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HttpString(io.undertow.util.HttpString) ClientRequest(io.undertow.client.ClientRequest) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Example 69 with HttpString

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);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HttpString(io.undertow.util.HttpString) ClientRequest(io.undertow.client.ClientRequest) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Example 70 with HttpString

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);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) ClientConnection(io.undertow.client.ClientConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) URISyntaxException(java.net.URISyntaxException) ClientRequest(io.undertow.client.ClientRequest) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Aggregations

HttpString (io.undertow.util.HttpString)147 HeaderMap (io.undertow.util.HeaderMap)31 HttpServerExchange (io.undertow.server.HttpServerExchange)27 Test (org.junit.Test)25 ClientRequest (io.undertow.client.ClientRequest)23 Map (java.util.Map)23 ClientResponse (io.undertow.client.ClientResponse)21 IOException (java.io.IOException)21 HashMap (java.util.HashMap)21 URI (java.net.URI)19 ClientConnection (io.undertow.client.ClientConnection)17 HttpHandler (io.undertow.server.HttpHandler)17 Http2Client (com.networknt.client.Http2Client)14 ClientException (com.networknt.exception.ClientException)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 User (com.networknt.portal.usermanagement.model.common.model.user.User)13 URISyntaxException (java.net.URISyntaxException)13 ArrayList (java.util.ArrayList)13 List (java.util.List)13 CountDownLatch (java.util.concurrent.CountDownLatch)11