Search in sources :

Example 6 with HttpServerOptions

use of io.vertx.core.http.HttpServerOptions 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)

Example 7 with HttpServerOptions

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

the class HttpRequestStreamTest method testCloseServerAsynchronously.

@Test
public void testCloseServerAsynchronously() {
    this.server = vertx.createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT));
    AtomicInteger done = new AtomicInteger();
    ReadStream<HttpServerRequest> stream = server.requestStream();
    stream.handler(req -> {
    });
    ThreadLocal<Object> stack = new ThreadLocal<>();
    stack.set(true);
    stream.endHandler(v -> {
        assertTrue(Vertx.currentContext().isEventLoopContext());
        assertNull(stack.get());
        if (done.incrementAndGet() == 2) {
            testComplete();
        }
    });
    server.listen(ar -> {
        assertTrue(Vertx.currentContext().isEventLoopContext());
        assertNull(stack.get());
        ThreadLocal<Object> stack2 = new ThreadLocal<>();
        stack2.set(true);
        server.close(v -> {
            assertTrue(Vertx.currentContext().isEventLoopContext());
            assertNull(stack2.get());
            if (done.incrementAndGet() == 2) {
                testComplete();
            }
        });
        stack2.set(null);
    });
    await();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Test(org.junit.Test)

Example 8 with HttpServerOptions

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

the class HttpTest method testInVerticle.

private void testInVerticle(boolean worker) throws Exception {
    client.close();
    server.close();
    class MyVerticle extends AbstractVerticle {

        Context ctx;

        @Override
        public void start() {
            ctx = Vertx.currentContext();
            if (worker) {
                assertTrue(ctx instanceof WorkerContext);
            } else {
                assertTrue(ctx instanceof EventLoopContext);
            }
            Thread thr = Thread.currentThread();
            server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT));
            server.requestHandler(req -> {
                req.response().end();
                assertSame(ctx, Vertx.currentContext());
                if (!worker) {
                    assertSame(thr, Thread.currentThread());
                }
            });
            server.listen(ar -> {
                assertTrue(ar.succeeded());
                assertSame(ctx, Vertx.currentContext());
                if (!worker) {
                    assertSame(thr, Thread.currentThread());
                }
                client = vertx.createHttpClient(new HttpClientOptions());
                client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", res -> {
                    assertSame(ctx, Vertx.currentContext());
                    if (!worker) {
                        assertSame(thr, Thread.currentThread());
                    }
                    assertEquals(200, res.statusCode());
                    testComplete();
                }).end();
            });
        }
    }
    MyVerticle verticle = new MyVerticle();
    vertx.deployVerticle(verticle, new DeploymentOptions().setWorker(worker));
    await();
}
Also used : Context(io.vertx.core.Context) WorkerContext(io.vertx.core.impl.WorkerContext) EventLoopContext(io.vertx.core.impl.EventLoopContext) 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) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpServerOptions(io.vertx.core.http.HttpServerOptions) WorkerContext(io.vertx.core.impl.WorkerContext) EventLoopContext(io.vertx.core.impl.EventLoopContext) AbstractVerticle(io.vertx.core.AbstractVerticle) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 9 with HttpServerOptions

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

the class HttpTest method testServerActualPortWhenZeroPassedInListen.

@Test
public void testServerActualPortWhenZeroPassedInListen() {
    server = vertx.createHttpServer(new HttpServerOptions(createBaseServerOptions()).setHost(DEFAULT_HTTP_HOST));
    server.requestHandler(request -> {
        request.response().end("hello");
    }).listen(0, ar -> {
        assertTrue(ar.result().actualPort() != 0);
        vertx.createHttpClient(createBaseClientOptions()).getNow(ar.result().actualPort(), DEFAULT_HTTP_HOST, "/", response -> {
            assertEquals(response.statusCode(), 200);
            response.bodyHandler(body -> {
                assertEquals(body.toString("UTF-8"), "hello");
                testComplete();
            });
        });
    });
    await();
}
Also used : 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) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Test(org.junit.Test)

Example 10 with HttpServerOptions

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

the class SimpleServer method start.

@Override
public void start(Future<Void> startFuture) throws Exception {
    HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
    server.requestHandler(req -> req.response().end());
    server.listen(res -> {
        if (res.succeeded()) {
            startFuture.complete();
        } else {
            startFuture.fail(res.cause());
        }
    });
}
Also used : HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions)

Aggregations

HttpServerOptions (io.vertx.core.http.HttpServerOptions)27 Test (org.junit.Test)20 HttpClientOptions (io.vertx.core.http.HttpClientOptions)11 HttpServer (io.vertx.core.http.HttpServer)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 HttpClient (io.vertx.core.http.HttpClient)8 Buffer (io.vertx.core.buffer.Buffer)7 HttpClientRequest (io.vertx.core.http.HttpClientRequest)7 HttpMethod (io.vertx.core.http.HttpMethod)7 HttpServerResponse (io.vertx.core.http.HttpServerResponse)7 Context (io.vertx.core.Context)6 Vertx (io.vertx.core.Vertx)6 HttpVersion (io.vertx.core.http.HttpVersion)6 NetSocket (io.vertx.core.net.NetSocket)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Handler (io.vertx.core.Handler)5 MultiMap (io.vertx.core.MultiMap)5 SSLHelper (io.vertx.core.net.impl.SSLHelper)5 AbstractVerticle (io.vertx.core.AbstractVerticle)4