Search in sources :

Example 11 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class Http2ClientTest method testResetPushPromiseNoHandler.

@Test
public void testResetPushPromiseNoHandler() throws Exception {
    server.requestHandler(req -> {
        req.response().push(HttpMethod.GET, "/wibble", ar -> {
            assertTrue(ar.succeeded());
            HttpServerResponse resp = ar.result();
            resp.setChunked(true).write("content");
            resp.exceptionHandler(err -> {
                assertTrue(err instanceof StreamResetException);
                assertEquals(Http2Error.CANCEL.code(), ((StreamResetException) err).getCode());
                testComplete();
            });
        });
    });
    startServer();
    HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
    });
    req.end();
    await();
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) StreamResetException(io.vertx.core.http.StreamResetException) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Test(org.junit.Test)

Example 12 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class Http2ClientTest method testSendPing.

@Test
public void testSendPing() throws Exception {
    waitFor(2);
    Buffer expected = TestUtils.randomBuffer(8);
    Context ctx = vertx.getOrCreateContext();
    server.close();
    server.connectionHandler(conn -> {
        conn.pingHandler(data -> {
            assertEquals(expected, data);
            complete();
        });
    });
    server.requestHandler(req -> {
    });
    startServer(ctx);
    HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
    });
    req.connectionHandler(conn -> {
        conn.ping(expected, ar -> {
            assertTrue(ar.succeeded());
            Buffer buff = ar.result();
            assertEquals(expected, buff);
            complete();
        });
    });
    req.end();
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Test(org.junit.Test)

Example 13 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class Http2Test method testClientStreamPausedWhenConnectionIsPaused.

@Test
public void testClientStreamPausedWhenConnectionIsPaused() throws Exception {
    waitFor(2);
    Buffer buffer = TestUtils.randomBuffer(512);
    CompletableFuture<Void> resumeLatch = new CompletableFuture<>();
    server.requestHandler(req -> {
        switch(req.path()) {
            case "/0":
                {
                    req.pause();
                    resumeLatch.thenAccept(v -> {
                        req.resume();
                    });
                    req.endHandler(v -> {
                        req.response().end();
                    });
                    break;
                }
            case "/1":
                {
                    req.bodyHandler(v -> {
                        assertEquals(v, buffer);
                        req.response().end();
                    });
                    break;
                }
        }
    });
    startServer();
    HttpClientRequest req1 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/0", resp -> {
        complete();
    }).setChunked(true);
    while (!req1.writeQueueFull()) {
        req1.write(Buffer.buffer(TestUtils.randomAlphaString(512)));
        Thread.sleep(1);
    }
    HttpClientRequest req2 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/1", resp -> {
        complete();
    }).setChunked(true);
    assertFalse(req2.writeQueueFull());
    req2.sendHead(v -> {
        assertTrue(req2.writeQueueFull());
        resumeLatch.complete(null);
    });
    resumeLatch.get(20, TimeUnit.SECONDS);
    waitUntil(() -> !req2.writeQueueFull());
    req1.end();
    req2.end(buffer);
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Http2Settings(io.vertx.core.http.Http2Settings) Cert(io.vertx.test.core.tls.Cert) Context(io.vertx.core.Context) TimeUnit(java.util.concurrent.TimeUnit) HttpClientRequest(io.vertx.core.http.HttpClientRequest) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerResponse(io.vertx.core.http.HttpServerResponse) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) StreamResetException(io.vertx.core.http.StreamResetException) CompletableFuture(java.util.concurrent.CompletableFuture) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Test(org.junit.Test)

Example 14 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class Http2Test method testClientRequestWriteFromOtherThread.

@Test
public void testClientRequestWriteFromOtherThread() throws Exception {
    CountDownLatch latch1 = new CountDownLatch(1);
    CountDownLatch latch2 = new CountDownLatch(1);
    server.requestHandler(req -> {
        latch2.countDown();
        req.endHandler(v -> {
            req.response().end();
        });
    }).listen(onSuccess(v -> {
        latch1.countDown();
    }));
    awaitLatch(latch1);
    HttpClientRequest req = client.get(8080, "localhost", "/somepath", resp -> {
        assertEquals(200, resp.statusCode());
        testComplete();
    }).setChunked(true).sendHead();
    // The next write won't be buffered
    awaitLatch(latch2);
    req.write("hello ").end("world");
    await();
}
Also used : Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Http2Settings(io.vertx.core.http.Http2Settings) Cert(io.vertx.test.core.tls.Cert) Context(io.vertx.core.Context) TimeUnit(java.util.concurrent.TimeUnit) HttpClientRequest(io.vertx.core.http.HttpClientRequest) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerResponse(io.vertx.core.http.HttpServerResponse) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) StreamResetException(io.vertx.core.http.StreamResetException) HttpClientRequest(io.vertx.core.http.HttpClientRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 15 with HttpClientRequest

use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.

the class HttpTest method testFollowRedirectWithCustomHandler.

@Test
public void testFollowRedirectWithCustomHandler() throws Exception {
    String scheme = createBaseClientOptions().isSsl() ? "https" : "http";
    waitFor(2);
    AtomicInteger redirections = new AtomicInteger();
    server.requestHandler(req -> {
        switch(redirections.getAndIncrement()) {
            case 0:
                req.response().setStatusCode(301).putHeader(HttpHeaders.LOCATION, "http://localhost:8080/whatever").end();
                break;
            case 1:
                assertEquals(scheme + "://another_host/custom", req.absoluteURI());
                req.response().end();
                complete();
                break;
        }
    });
    startServer();
    client.redirectHandler(resp -> {
        Future<HttpClientRequest> fut = Future.future();
        vertx.setTimer(25, id -> {
            HttpClientRequest req = client.getAbs(scheme + "://localhost:8080/custom");
            req.putHeader("foo", "foo_another");
            req.setHost("another_host");
            fut.complete(req);
        });
        return fut;
    });
    client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
        assertEquals(scheme + "://another_host/custom", resp.request().absoluteURI());
        complete();
    }).setFollowRedirects(true).putHeader("foo", "foo_value").setHost("the_host").end();
    await();
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

HttpClientRequest (io.vertx.core.http.HttpClientRequest)73 Test (org.junit.Test)59 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)42 Buffer (io.vertx.core.buffer.Buffer)39 HttpServerResponse (io.vertx.core.http.HttpServerResponse)36 HttpClientOptions (io.vertx.core.http.HttpClientOptions)34 HttpMethod (io.vertx.core.http.HttpMethod)32 CountDownLatch (java.util.concurrent.CountDownLatch)32 HttpServerOptions (io.vertx.core.http.HttpServerOptions)31 CompletableFuture (java.util.concurrent.CompletableFuture)31 AtomicReference (java.util.concurrent.atomic.AtomicReference)31 HttpVersion (io.vertx.core.http.HttpVersion)29 HttpConnection (io.vertx.core.http.HttpConnection)28 TestUtils.assertIllegalStateException (io.vertx.test.core.TestUtils.assertIllegalStateException)28 TimeUnit (java.util.concurrent.TimeUnit)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)28 HttpClient (io.vertx.core.http.HttpClient)27 NetSocket (io.vertx.core.net.NetSocket)27 ArrayList (java.util.ArrayList)26 Collections (java.util.Collections)26