Search in sources :

Example 26 with ClientRequest

use of io.undertow.client.ClientRequest in project light-portal by networknt.

the class UserTokenIdGetHandlerTest method testUserTokenIdGetHandlerTest.

@Test
public void testUserTokenIdGetHandlerTest() throws ClientException, ApiException {
    final 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, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/v1/user/token/3e0-456-33eo67").setMethod(Methods.GET);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        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);
    System.out.println("response:" + body);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) ApiException(com.networknt.exception.ApiException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 27 with ClientRequest

use of io.undertow.client.ClientRequest in project light-portal by networknt.

the class UserIdGetHandlerTest method testUserIdGetHandlerTest.

@Test
public void testUserIdGetHandlerTest() throws ClientException, ApiException {
    final 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, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/v1/user/12222").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);
    System.out.println("response:" + body);
    Assert.assertNotNull(body);
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) IOException(java.io.IOException) ApiException(com.networknt.exception.ApiException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 28 with ClientRequest

use of io.undertow.client.ClientRequest in project light-portal by networknt.

the class UserNameGetHandlerTest method testUserNameGetHandlerTest.

@Test
public void testUserNameGetHandlerTest() throws ClientException, ApiException {
    final 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, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/v1/user/name?name=testname").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);
    System.out.println("response:" + body);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) IOException(java.io.IOException) ApiException(com.networknt.exception.ApiException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 29 with ClientRequest

use of io.undertow.client.ClientRequest in project light-codegen by networknt.

the class FrameworkListHandlerTest method testGenerator.

@Test
public void testGenerator() throws ClientException, ApiException, UnsupportedEncodingException {
    String s = "{\"host\":\"lightapi.net\",\"service\":\"codegen\",\"action\":\"listFramework\",\"version\":\"0.0.1\"}";
    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(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.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.POST).setPath("/api/json");
                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, s));
            }
        });
        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.assertTrue(body.contains("swagger"));
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ApiException(com.networknt.exception.ApiException) ClientConnection(io.undertow.client.ClientConnection) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 30 with ClientRequest

use of io.undertow.client.ClientRequest in project light-codegen by networknt.

the class GeneratorServiceHandlerTest method testInvalidFramework.

@Test
public void testInvalidFramework() throws ClientException, IOException {
    Map<String, Object> params = new HashMap<>();
    params.put("host", "lightapi.net");
    params.put("service", "codegen");
    params.put("action", "generate");
    params.put("version", "0.0.1");
    params.put("data", new ObjectMapper().readValue("[{\"framework\": \"invalid\", \"model\": {\"key\":\"value\"}, \"config\": {\"key\":\"value\"}}]", List.class));
    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(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    try {
        connection.getIoThread().execute(() -> {
            final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath("/api/json");
            request.getRequestHeaders().put(Headers.HOST, "localhost");
            request.getRequestHeaders().put(Headers.AUTHORIZATION, auth);
            request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
            request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
            try {
                connection.sendRequest(request, client.createClientCallback(reference, latch, new ObjectMapper().writeValueAsString(params)));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        });
        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.assertTrue(body.contains("ERR11100"));
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ApiException(com.networknt.exception.ApiException) List(java.util.List) ClientConnection(io.undertow.client.ClientConnection) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Aggregations

ClientRequest (io.undertow.client.ClientRequest)187 ClientResponse (io.undertow.client.ClientResponse)175 ClientConnection (io.undertow.client.ClientConnection)169 CountDownLatch (java.util.concurrent.CountDownLatch)159 URI (java.net.URI)156 AtomicReference (java.util.concurrent.atomic.AtomicReference)155 Test (org.junit.Test)148 Http2Client (com.networknt.client.Http2Client)146 ClientException (com.networknt.exception.ClientException)145 ApiException (com.networknt.exception.ApiException)48 HttpString (io.undertow.util.HttpString)35 IOException (java.io.IOException)35 Status (com.networknt.status.Status)30 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)17 UndertowClient (io.undertow.client.UndertowClient)16 URISyntaxException (java.net.URISyntaxException)14 SQLException (java.sql.SQLException)12 HeaderMap (io.undertow.util.HeaderMap)8 HashMap (java.util.HashMap)8 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)7