Search in sources :

Example 16 with HttpClient

use of io.vertx.core.http.HttpClient in project incubator-servicecomb-java-chassis by apache.

the class TestConfigCenterClient method testConfigRefresh.

@SuppressWarnings("unchecked")
@Test
public void testConfigRefresh() {
    ConfigCenterConfigurationSourceImpl impl = new ConfigCenterConfigurationSourceImpl();
    impl.init(ConfigUtil.createLocalConfig());
    UpdateHandler updateHandler = impl.new UpdateHandler();
    HttpClientRequest request = Mockito.mock(HttpClientRequest.class);
    Mockito.when(request.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
    Buffer rsp = Mockito.mock(Buffer.class);
    Mockito.when(rsp.toString()).thenReturn("{\"application\":{\"2\":\"2\",\"aa\":\"1\"},\"vmalledge\":{\"aa\":\"3\"}}");
    HttpClientResponse event = Mockito.mock(HttpClientResponse.class);
    Mockito.when(event.bodyHandler(Mockito.any(Handler.class))).then(invocation -> {
        Handler<Buffer> handler = invocation.getArgumentAt(0, Handler.class);
        handler.handle(rsp);
        return null;
    });
    Mockito.when(event.statusCode()).thenReturn(200);
    Buffer buf = Mockito.mock(Buffer.class);
    Mockito.when(buf.toJsonObject()).thenReturn(new JsonObject("{\"action\":\"UPDATE\",\"key\":\"vmalledge\",\"value\":\"{\\\"aa\\\":\\\"3\\\"}\"}"));
    WebSocket websocket = Mockito.mock(WebSocket.class);
    Mockito.when(websocket.handler(Mockito.any(Handler.class))).then(invocation -> {
        Handler<Buffer> handler = invocation.getArgumentAt(0, Handler.class);
        handler.handle(buf);
        return websocket;
    });
    HttpClient httpClient = Mockito.mock(HttpClient.class);
    Mockito.when(httpClient.get(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.any(Handler.class))).then(invocation -> {
        Handler<HttpClientResponse> handler = invocation.getArgumentAt(3, Handler.class);
        handler.handle(event);
        return request;
    });
    Mockito.when(httpClient.websocket(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.any(MultiMap.class), Mockito.any(Handler.class), Mockito.any(Handler.class))).then(invocation -> {
        Handler<WebSocket> handler = invocation.getArgumentAt(4, Handler.class);
        handler.handle(websocket);
        return null;
    });
    new MockUp<HttpClientWithContext>() {

        @Mock
        public void runOnContext(RunHandler handler) {
            handler.run(httpClient);
        }
    };
    ConfigCenterClient cc = new ConfigCenterClient(updateHandler);
    ParseConfigUtils parseConfigUtils = new ParseConfigUtils(updateHandler);
    MemberDiscovery memberdis = new MemberDiscovery(Arrays.asList("http://configcentertest:30103"));
    ConfigRefresh refresh = cc.new ConfigRefresh(parseConfigUtils, memberdis);
    refresh.run();
    Map<String, Object> flatItems = Deencapsulation.getField(parseConfigUtils, "flatItems");
    Assert.assertEquals(2, flatItems.size());
    Deencapsulation.setField(cc, "refreshMode", 0);
    refresh.run();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) UpdateHandler(org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler) ConfigCenterConfigurationSourceImpl(org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl) RunHandler(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext.RunHandler) UpdateHandler(org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) MockUp(mockit.MockUp) RunHandler(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext.RunHandler) WebSocket(io.vertx.core.http.WebSocket) MultiMap(io.vertx.core.MultiMap) HttpClientRequest(io.vertx.core.http.HttpClientRequest) ConfigRefresh(org.apache.servicecomb.config.client.ConfigCenterClient.ConfigRefresh) HttpClientResponse(io.vertx.core.http.HttpClientResponse) HttpClient(io.vertx.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 17 with HttpClient

use of io.vertx.core.http.HttpClient in project vertx-micrometer-metrics by vert-x3.

the class VertxHttpClientServerMetricsTest method httpRequest.

private void httpRequest(HttpClient httpClient, TestContext ctx) {
    Async async = ctx.async(HTTP_SENT_COUNT);
    for (int i = 0; i < HTTP_SENT_COUNT; i++) {
        httpClient.post(9195, "127.0.0.1", "/resource", response -> {
            async.countDown();
            if (response.statusCode() != 200) {
                ctx.fail(response.statusMessage());
            }
        }).exceptionHandler(t -> {
            async.countDown();
            ctx.fail(t);
        }).putHeader("Content-Length", String.valueOf(CLIENT_REQUEST.getBytes().length)).write(CLIENT_REQUEST).end();
    }
    async.await();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) HttpServer(io.vertx.core.http.HttpServer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) UUID(java.util.UUID) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) RegistryInspector.dp(io.vertx.micrometer.RegistryInspector.dp) List(java.util.List) ForkJoinPool(java.util.concurrent.ForkJoinPool) AbstractVerticle(io.vertx.core.AbstractVerticle) After(org.junit.After) HttpClient(io.vertx.core.http.HttpClient) Before(org.junit.Before) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Async(io.vertx.ext.unit.Async)

Example 18 with HttpClient

use of io.vertx.core.http.HttpClient in project vertx-micrometer-metrics by vert-x3.

the class MetricsServiceImplTest method shouldGetHttpServerSnapshot.

@Test
public void shouldGetHttpServerSnapshot(TestContext ctx) throws InterruptedException {
    HttpClient httpClient = vertx.createHttpClient();
    runClientRequests(ctx, httpClient, 5, "/r2");
    httpClient.close();
    JsonObject snapshot = MetricsService.create(httpServer).getMetricsSnapshot();
    assertThat(snapshot).extracting(Map.Entry::getKey).containsExactly("vertx.http.server.bytesReceived", "vertx.http.server.bytesSent", "vertx.http.server.connections", "vertx.http.server.requestCount", "vertx.http.server.requests", "vertx.http.server.responseTime");
}
Also used : HttpClient(io.vertx.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject) Map(java.util.Map) Test(org.junit.Test)

Example 19 with HttpClient

use of io.vertx.core.http.HttpClient in project sonar-java by SonarSource.

the class A method test_saveUser.

@Test
public void test_saveUser() {
    // Compliant even if this test may not be run correctly - assertion is present
    TestSuite suite = TestSuite.create("suite_user_save");
    HttpClient client = vertx.createHttpClient();
    suite.test("user_save", context -> {
        client.getNow(port, host, requestURI, resp -> {
            resp.bodyHandler(body -> {
                // assertion
                context.assertNotEquals("created", body.toString());
                client.close();
            });
        });
    });
}
Also used : TestSuite(io.vertx.ext.unit.TestSuite) HttpClient(io.vertx.core.http.HttpClient) Test(org.junit.Test)

Example 20 with HttpClient

use of io.vertx.core.http.HttpClient in project sonar-java by SonarSource.

the class A method test_getAuthURL.

@Test
public void test_getAuthURL(TestContext contextVertx) {
    // Compliant
    HttpClient client = vertx.createHttpClient();
    Async async = contextVertx.async();
    client.getNow(port, "localhost", requestURI, resp -> {
        resp.bodyHandler(body -> {
            // assertion
            contextVertx.assertEquals(urlString, body.toString());
            client.close();
            async.complete();
        });
    });
}
Also used : Async(io.vertx.ext.unit.Async) HttpClient(io.vertx.core.http.HttpClient) Test(org.junit.Test)

Aggregations

HttpClient (io.vertx.core.http.HttpClient)77 Test (org.junit.Test)47 HttpClientRequest (io.vertx.core.http.HttpClientRequest)36 HttpClientOptions (io.vertx.core.http.HttpClientOptions)25 Vertx (io.vertx.core.Vertx)22 HttpMethod (io.vertx.core.http.HttpMethod)22 JsonObject (io.vertx.core.json.JsonObject)22 Handler (io.vertx.core.Handler)18 Buffer (io.vertx.core.buffer.Buffer)18 HttpClientResponse (io.vertx.core.http.HttpClientResponse)16 TimeUnit (java.util.concurrent.TimeUnit)16 HttpServer (io.vertx.core.http.HttpServer)15 Async (io.vertx.ext.unit.Async)15 Before (org.junit.Before)15 File (java.io.File)14 CountDownLatch (java.util.concurrent.CountDownLatch)14 URL (java.net.URL)12 HttpServerOptions (io.vertx.core.http.HttpServerOptions)11 IOException (java.io.IOException)10 List (java.util.List)10