Search in sources :

Example 16 with Buffer

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

the class Http1xTest method testConnectionCloseHttp_1_1_NoClose.

@Test
public void testConnectionCloseHttp_1_1_NoClose() throws Exception {
    testConnectionClose(HttpClientRequest::end, socket -> {
        AtomicBoolean firstRequest = new AtomicBoolean(true);
        socket.handler(RecordParser.newDelimited("\r\n\r\n", buffer -> {
            if (firstRequest.getAndSet(false)) {
                socket.write("HTTP/1.1 200 OK\n" + "Content-Type: text/plain\n" + "Content-Length: 4\n" + "\n" + "xxx\n");
            } else {
                socket.write("HTTP/1.1 200 OK\n" + "Content-Type: text/plain\n" + "Content-Length: 1\n" + "Connection: close\n" + "\n" + "\n");
            }
        }));
    });
}
Also used : 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) Test(org.junit.Test)

Example 17 with Buffer

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

the class Http1xTest method testInvalidHttpResponse.

@Test
public void testInvalidHttpResponse() {
    waitFor(2);
    AtomicInteger count = new AtomicInteger(0);
    CompletableFuture<Void> sendResp = new CompletableFuture<>();
    NetServer server = vertx.createNetServer();
    String match = "GET /somepath HTTP/1.1\r\nHost: localhost:8080\r\n\r\n";
    server.connectHandler(so -> {
        StringBuilder content = new StringBuilder();
        so.handler(buff -> {
            content.append(buff);
            while (content.toString().startsWith(match)) {
                content.delete(0, match.length());
                switch(count.getAndIncrement()) {
                    case 0:
                        sendResp.thenAccept(v -> {
                        });
                        break;
                    case 1:
                        Buffer resp1 = Buffer.buffer(TestUtils.randomAlphaString(40) + "\r\n");
                        Buffer resp2 = Buffer.buffer("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
                        so.write(Buffer.buffer().appendBuffer(resp1).appendBuffer(resp2));
                        break;
                    default:
                        fail();
                        break;
                }
            }
        });
    }).listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(s -> {
        client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setPipelining(true).setMaxPoolSize(1));
        AtomicBoolean fail1 = new AtomicBoolean();
        HttpClientRequest req1 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
            fail();
        }).exceptionHandler(err -> {
            if (fail1.compareAndSet(false, true)) {
                assertEquals(IllegalArgumentException.class, err.getClass());
                complete();
            }
        });
        AtomicBoolean fail2 = new AtomicBoolean();
        HttpClientRequest req2 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
            resp.bodyHandler(buff -> {
                assertEquals("okusa", buff.toString());
                testComplete();
            });
        }).exceptionHandler(err -> {
            if (fail2.compareAndSet(false, true)) {
                assertEquals(VertxException.class, err.getClass());
                complete();
            }
        });
        req1.end();
        req2.end();
    }));
    await();
}
Also used : 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) Buffer(io.vertx.core.buffer.Buffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 18 with Buffer

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

the class HostnameResolutionTest method testHttp.

@Test
public void testHttp() throws Exception {
    HttpClient client = vertx.createHttpClient();
    HttpServer server = vertx.createHttpServer().requestHandler(req -> {
        req.response().end("foo");
    });
    try {
        CountDownLatch listenLatch = new CountDownLatch(1);
        server.listen(8080, "vertx.io", onSuccess(s -> {
            listenLatch.countDown();
        }));
        awaitLatch(listenLatch);
        client.getNow(8080, "vertx.io", "/somepath", resp -> {
            Buffer buffer = Buffer.buffer();
            resp.handler(buffer::appendBuffer);
            resp.endHandler(v -> {
                assertEquals(Buffer.buffer("foo"), buffer);
                testComplete();
            });
        });
        await();
    } finally {
        client.close();
        server.close();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) VertxException(io.vertx.core.VertxException) Arrays(java.util.Arrays) HttpServer(io.vertx.core.http.HttpServer) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Locale(java.util.Locale) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) FakeDNSServer(io.vertx.test.fakedns.FakeDNSServer) NetClient(io.vertx.core.net.NetClient) VertxImpl(io.vertx.core.impl.VertxImpl) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) AddressResolver(io.vertx.core.impl.AddressResolver) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) NetServerOptions(io.vertx.core.net.NetServerOptions) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) Buffer(io.vertx.core.buffer.Buffer) HttpClient(io.vertx.core.http.HttpClient) HttpServer(io.vertx.core.http.HttpServer) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 19 with Buffer

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

the class Http2ServerTest method testReceivePing.

@Test
public void testReceivePing() throws Exception {
    Buffer expected = TestUtils.randomBuffer(8);
    Context ctx = vertx.getOrCreateContext();
    server.close();
    server.connectionHandler(conn -> {
        conn.pingHandler(buff -> {
            assertOnIOContext(ctx);
            assertEquals(expected, buff);
            testComplete();
        });
    });
    server.requestHandler(req -> fail());
    startServer(ctx);
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        request.encoder.writePing(request.context, false, expected.getByteBuf(), request.context.newPromise());
    });
    fut.sync();
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Context(io.vertx.core.Context) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelFuture(io.netty.channel.ChannelFuture) Test(org.junit.Test)

Example 20 with Buffer

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

the class HttpRequestStreamTest method testReadStreamPauseResume.

@Test
public void testReadStreamPauseResume() {
    String path = "/some/path";
    this.server = vertx.createHttpServer(new HttpServerOptions().setAcceptBacklog(10).setPort(HttpTestBase.DEFAULT_HTTP_PORT));
    ReadStream<HttpServerRequest> httpStream = server.requestStream();
    AtomicBoolean paused = new AtomicBoolean();
    httpStream.handler(req -> {
        assertFalse(paused.get());
        HttpServerResponse response = req.response();
        response.setStatusCode(200).end();
        response.close();
    });
    server.listen(listenAR -> {
        assertTrue(listenAR.succeeded());
        paused.set(true);
        httpStream.pause();
        netClient = vertx.createNetClient(new NetClientOptions().setConnectTimeout(1000));
        netClient.connect(HttpTestBase.DEFAULT_HTTP_PORT, "localhost", socketAR -> {
            assertTrue(socketAR.succeeded());
            NetSocket socket = socketAR.result();
            Buffer buffer = Buffer.buffer();
            socket.handler(buffer::appendBuffer);
            socket.closeHandler(v -> {
                assertEquals(0, buffer.length());
                paused.set(false);
                httpStream.resume();
                client = vertx.createHttpClient(new HttpClientOptions());
                client.request(HttpMethod.GET, HttpTestBase.DEFAULT_HTTP_PORT, "localhost", path, resp -> {
                    assertEquals(200, resp.statusCode());
                    testComplete();
                }).end();
            });
        });
    });
    await();
}
Also used : NetSocket(io.vertx.core.net.NetSocket) Buffer(io.vertx.core.buffer.Buffer) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServer(io.vertx.core.http.HttpServer) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) NetClientOptions(io.vertx.core.net.NetClientOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerResponse(io.vertx.core.http.HttpServerResponse) ReadStream(io.vertx.core.streams.ReadStream) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) NetClient(io.vertx.core.net.NetClient) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NetClientOptions(io.vertx.core.net.NetClientOptions) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) 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