Search in sources :

Example 6 with HttpServerResponse

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

the class Http2ClientTest method testResetActivePushPromise.

@Test
public void testResetActivePushPromise() throws Exception {
    server.requestHandler(req -> {
        req.response().push(HttpMethod.GET, "/wibble", ar -> {
            assertTrue(ar.succeeded());
            HttpServerResponse response = ar.result();
            response.exceptionHandler(err -> {
                if (err instanceof StreamResetException) {
                    assertEquals(Http2Error.CANCEL.code(), ((StreamResetException) err).getCode());
                    testComplete();
                }
            });
            response.setChunked(true).write("some_content");
        });
    });
    startServer();
    HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
        fail();
    });
    req.pushHandler(pushedReq -> {
        pushedReq.handler(pushedResp -> {
            pushedResp.handler(buff -> {
                pushedReq.reset(Http2Error.CANCEL.code());
            });
        });
    });
    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 7 with HttpServerResponse

use of io.vertx.core.http.HttpServerResponse 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 8 with HttpServerResponse

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

the class Http2ClientTest method testTrailers.

@Test
public void testTrailers() throws Exception {
    server.requestHandler(req -> {
        HttpServerResponse resp = req.response();
        resp.setChunked(true);
        resp.write("some-content");
        resp.putTrailer("Foo", "foo_value");
        resp.putTrailer("bar", "bar_value");
        resp.putTrailer("juu", (List<String>) Arrays.asList("juu_value_1", "juu_value_2"));
        resp.end();
    });
    startServer();
    client.getNow(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepeth", resp -> {
        assertEquals(null, resp.getTrailer("foo"));
        resp.exceptionHandler(this::fail);
        resp.endHandler(v -> {
            assertEquals("foo_value", resp.getTrailer("foo"));
            assertEquals("foo_value", resp.getTrailer("Foo"));
            assertEquals("bar_value", resp.getTrailer("bar"));
            assertEquals(2, resp.trailers().getAll("juu").size());
            assertEquals("juu_value_1", resp.trailers().getAll("juu").get(0));
            assertEquals("juu_value_2", resp.trailers().getAll("juu").get(1));
            testComplete();
        });
    });
    await();
}
Also used : HttpServerResponse(io.vertx.core.http.HttpServerResponse) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 9 with HttpServerResponse

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

the class Http2ClientTest method testBodyEndHandler.

@Test
public void testBodyEndHandler() throws Exception {
    // Large body so it will be fragmented in several HTTP2 data frames
    Buffer expected = Buffer.buffer(TestUtils.randomAlphaString(128 * 1024));
    server.requestHandler(req -> {
        HttpServerResponse resp = req.response();
        resp.end(expected);
    });
    startServer();
    client.getNow(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
        Context ctx = vertx.getOrCreateContext();
        resp.exceptionHandler(this::fail);
        resp.bodyHandler(body -> {
            assertOnIOContext(ctx);
            assertEquals(expected, body);
            testComplete();
        });
    });
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Test(org.junit.Test)

Example 10 with HttpServerResponse

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

the class Http2Test method testServerStreamPausedWhenConnectionIsPaused.

@Test
public void testServerStreamPausedWhenConnectionIsPaused() throws Exception {
    CountDownLatch fullLatch = new CountDownLatch(1);
    CompletableFuture<Void> resumeLatch = new CompletableFuture<>();
    server.requestHandler(req -> {
        HttpServerResponse resp = req.response();
        switch(req.path()) {
            case "/0":
                {
                    vertx.setPeriodic(1, timerID -> {
                        if (resp.writeQueueFull()) {
                            vertx.cancelTimer(timerID);
                            fullLatch.countDown();
                        } else {
                            resp.write(Buffer.buffer(TestUtils.randomAlphaString(512)));
                        }
                    });
                    break;
                }
            case "/1":
                {
                    assertTrue(resp.writeQueueFull());
                    resp.drainHandler(v -> {
                        resp.end();
                    });
                    resumeLatch.complete(null);
                    break;
                }
        }
    });
    startServer();
    client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/0", resp -> {
        resp.pause();
        Context ctx = vertx.getOrCreateContext();
        resumeLatch.thenAccept(v1 -> {
            ctx.runOnContext(v2 -> {
                resp.endHandler(v -> {
                    testComplete();
                });
                resp.resume();
            });
        });
    });
    awaitLatch(fullLatch);
    client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/1", resp -> {
        resp.endHandler(v -> {
            complete();
        });
    });
    // Make sure it completes
    resumeLatch.get(20, TimeUnit.SECONDS);
    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) Context(io.vertx.core.Context) CompletableFuture(java.util.concurrent.CompletableFuture) HttpServerResponse(io.vertx.core.http.HttpServerResponse) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

HttpServerResponse (io.vertx.core.http.HttpServerResponse)37 Test (org.junit.Test)29 Buffer (io.vertx.core.buffer.Buffer)20 HttpClientRequest (io.vertx.core.http.HttpClientRequest)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 HttpClientOptions (io.vertx.core.http.HttpClientOptions)17 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 HttpMethod (io.vertx.core.http.HttpMethod)15 HttpServerOptions (io.vertx.core.http.HttpServerOptions)15 NetSocket (io.vertx.core.net.NetSocket)14 TimeUnit (java.util.concurrent.TimeUnit)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 ChannelFuture (io.netty.channel.ChannelFuture)13 HttpConnection (io.vertx.core.http.HttpConnection)13 HttpVersion (io.vertx.core.http.HttpVersion)13 TestUtils.assertIllegalStateException (io.vertx.test.core.TestUtils.assertIllegalStateException)13 ArrayList (java.util.ArrayList)13 Collections (java.util.Collections)13 List (java.util.List)13