Search in sources :

Example 31 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class HttpTest method testFollowRedirectWithChunkedBody.

@Test
public void testFollowRedirectWithChunkedBody() throws Exception {
    Buffer buff1 = Buffer.buffer(TestUtils.randomAlphaString(2048));
    Buffer buff2 = Buffer.buffer(TestUtils.randomAlphaString(2048));
    Buffer expected = Buffer.buffer().appendBuffer(buff1).appendBuffer(buff2);
    AtomicBoolean redirected = new AtomicBoolean();
    CountDownLatch latch = new CountDownLatch(1);
    server.requestHandler(req -> {
        boolean redirect = redirected.compareAndSet(false, true);
        if (redirect) {
            latch.countDown();
        }
        req.bodyHandler(body -> {
            assertEquals(HttpMethod.PUT, req.method());
            assertEquals(body, expected);
            if (redirect) {
                req.response().setStatusCode(307).putHeader(HttpHeaders.LOCATION, "http://localhost:8080/whatever").end();
            } else {
                req.response().end();
            }
        });
    });
    startServer();
    HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
        assertEquals(200, resp.statusCode());
        testComplete();
    }).setFollowRedirects(true).setChunked(true).write(buff1);
    awaitLatch(latch);
    req.end(buff2);
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) TimeoutException(java.util.concurrent.TimeoutException) Context(io.vertx.core.Context) InetAddress(java.net.InetAddress) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Nullable(io.vertx.codegen.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) WorkerContext(io.vertx.core.impl.WorkerContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) IntStream(java.util.stream.IntStream) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) BufferedWriter(java.io.BufferedWriter) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TemporaryFolder(org.junit.rules.TemporaryFolder) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClientRequest(io.vertx.core.http.HttpClientRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 32 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class HttpTest method drainingServer.

private void drainingServer(Consumer<Future<Void>> consumer) {
    Future<Void> resumeFuture = Future.future();
    server.requestHandler(req -> {
        req.response().setChunked(true);
        assertFalse(req.response().writeQueueFull());
        req.response().setWriteQueueMaxSize(1000);
        Buffer buff = TestUtils.randomBuffer(10000);
        vertx.setPeriodic(1, id -> {
            req.response().write(buff);
            if (req.response().writeQueueFull()) {
                vertx.cancelTimer(id);
                req.response().drainHandler(v -> {
                    assertFalse(req.response().writeQueueFull());
                    testComplete();
                });
                resumeFuture.complete();
            }
        });
    });
    server.listen(onSuccess(s -> consumer.accept(resumeFuture)));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) TimeoutException(java.util.concurrent.TimeoutException) Context(io.vertx.core.Context) InetAddress(java.net.InetAddress) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Nullable(io.vertx.codegen.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) WorkerContext(io.vertx.core.impl.WorkerContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) IntStream(java.util.stream.IntStream) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) BufferedWriter(java.io.BufferedWriter) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TemporaryFolder(org.junit.rules.TemporaryFolder) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException)

Example 33 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class HttpTest method test100ContinueHandledManually.

@Test
public void test100ContinueHandledManually() throws Exception {
    server.close();
    server = vertx.createHttpServer(createBaseServerOptions());
    Buffer toSend = TestUtils.randomBuffer(1000);
    server.requestHandler(req -> {
        assertEquals("100-continue", req.getHeader("expect"));
        req.response().writeContinue();
        req.bodyHandler(data -> {
            assertEquals(toSend, data);
            req.response().end();
        });
    });
    server.listen(onSuccess(s -> {
        HttpClientRequest req = client.request(HttpMethod.PUT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
            resp.endHandler(v -> testComplete());
        });
        req.headers().set("Expect", "100-continue");
        req.setChunked(true);
        req.continueHandler(v -> {
            req.write(toSend);
            req.end();
        });
        req.sendHead();
    }));
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) TimeoutException(java.util.concurrent.TimeoutException) Context(io.vertx.core.Context) InetAddress(java.net.InetAddress) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Nullable(io.vertx.codegen.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) WorkerContext(io.vertx.core.impl.WorkerContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) IntStream(java.util.stream.IntStream) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) BufferedWriter(java.io.BufferedWriter) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TemporaryFolder(org.junit.rules.TemporaryFolder) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Test(org.junit.Test)

Example 34 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class Http1xTest method testCloseTheConnectionAfterResetPersistentClientResponse.

private void testCloseTheConnectionAfterResetPersistentClientResponse(boolean pipelined) throws Exception {
    waitFor(2);
    server.close();
    NetServer server = vertx.createNetServer();
    try {
        AtomicInteger count = new AtomicInteger();
        AtomicBoolean closed = new AtomicBoolean();
        server.connectHandler(so -> {
            Buffer total = Buffer.buffer();
            switch(count.getAndIncrement()) {
                case 0:
                    so.handler(buff -> {
                        total.appendBuffer(buff);
                        if (total.toString().equals("GET /somepath HTTP/1.1\r\n" + "Host: localhost:8080\r\n" + "\r\n")) {
                            so.write(Buffer.buffer("HTTP/1.1 200 OK\r\n" + "Content-Length: 200\r\n" + "\r\n" + "Some-Buffer"));
                            so.closeHandler(v -> {
                                closed.set(true);
                            });
                        }
                    });
                    break;
                case 1:
                    so.handler(buff -> {
                        total.appendBuffer(buff);
                        if (total.toString().equals("GET /somepath HTTP/1.1\r\n" + "Host: localhost:8080\r\n" + "\r\n")) {
                            so.write("HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: 11\r\n" + "\r\n" + "Hello world");
                            complete();
                        }
                    });
                    break;
                default:
                    fail("Invalid count");
                    break;
            }
        });
        CountDownLatch listenLatch = new CountDownLatch(1);
        server.listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v -> {
            listenLatch.countDown();
        }));
        awaitLatch(listenLatch);
        client.close();
        client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1).setPipelining(pipelined).setKeepAlive(true));
        HttpClientRequest req1 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
        if (pipelined) {
            req1.handler(resp1 -> {
                resp1.handler(buff -> {
                    req1.reset();
                    client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
                        assertEquals(200, resp.statusCode());
                        resp.bodyHandler(body -> {
                            assertEquals("Hello world", body.toString());
                            complete();
                        });
                    });
                });
            });
            req1.end();
        } else {
            req1.handler(resp -> {
                resp.handler(buff -> {
                    req1.reset();
                });
            });
            req1.end();
            client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
                assertEquals(200, resp.statusCode());
                resp.bodyHandler(body -> {
                    assertEquals("Hello world", body.toString());
                    complete();
                });
            });
        }
        await();
    } finally {
        server.close();
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) IntStream(java.util.stream.IntStream) java.util(java.util) io.vertx.core(io.vertx.core) io.vertx.core.impl(io.vertx.core.impl) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) io.vertx.core.http(io.vertx.core.http) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientRequestImpl(io.vertx.core.http.impl.HttpClientRequestImpl) Buffer(io.vertx.core.buffer.Buffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestUtils(io.vertx.test.core.TestUtils) RecordParser(io.vertx.core.parsetools.RecordParser) Pump(io.vertx.core.streams.Pump) JsonObject(io.vertx.core.json.JsonObject) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 35 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class Http1xTest method testHttpConnect.

@Test
public void testHttpConnect() {
    Buffer buffer = TestUtils.randomBuffer(128);
    Buffer received = Buffer.buffer();
    vertx.createNetServer(new NetServerOptions().setPort(1235)).connectHandler(socket -> {
        socket.handler(socket::write);
    }).listen(onSuccess(netServer -> {
        server.requestHandler(req -> {
            vertx.createNetClient(new NetClientOptions()).connect(netServer.actualPort(), "localhost", onSuccess(socket -> {
                req.response().setStatusCode(200);
                req.response().setStatusMessage("Connection established");
                req.response().end();
                Pump.pump(req.netSocket(), socket).start();
                Pump.pump(socket, req.netSocket()).start();
                req.netSocket().closeHandler(v -> socket.close());
            }));
        });
        server.listen(onSuccess(s -> {
            client.request(HttpMethod.CONNECT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
                assertEquals(200, resp.statusCode());
                NetSocket socket = resp.netSocket();
                socket.handler(buff -> {
                    received.appendBuffer(buff);
                    if (received.length() == buffer.length()) {
                        netServer.close();
                        assertEquals(buffer, received);
                        testComplete();
                    }
                });
                socket.write(buffer);
            }).end();
        }));
    }));
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) IntStream(java.util.stream.IntStream) java.util(java.util) io.vertx.core(io.vertx.core) io.vertx.core.impl(io.vertx.core.impl) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) io.vertx.core.http(io.vertx.core.http) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientRequestImpl(io.vertx.core.http.impl.HttpClientRequestImpl) Buffer(io.vertx.core.buffer.Buffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestUtils(io.vertx.test.core.TestUtils) RecordParser(io.vertx.core.parsetools.RecordParser) Pump(io.vertx.core.streams.Pump) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Aggregations

Buffer (io.vertx.core.buffer.Buffer)1053 Test (org.junit.Test)604 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)256 Future (io.vertx.core.Future)253 AtomicReference (java.util.concurrent.atomic.AtomicReference)237 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)235 Handler (io.vertx.core.Handler)230 Vertx (io.vertx.core.Vertx)185 TestUtils (io.vertx.test.core.TestUtils)177 JsonObject (io.vertx.core.json.JsonObject)172 TimeUnit (java.util.concurrent.TimeUnit)161 CountDownLatch (java.util.concurrent.CountDownLatch)153 List (java.util.List)152 Consumer (java.util.function.Consumer)149 Function (java.util.function.Function)142 ReadStream (io.vertx.core.streams.ReadStream)139 Context (io.vertx.core.Context)135 ArrayList (java.util.ArrayList)132 MultiMap (io.vertx.core.MultiMap)130 Assume (org.junit.Assume)130