Search in sources :

Example 1 with HttpServer

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

the class Http1xTLSTest method testRedirectFromSSL.

@Test
public void testRedirectFromSSL() throws Exception {
    HttpServer redirectServer = vertx.createHttpServer(new HttpServerOptions().setSsl(true).setKeyStoreOptions(Cert.SERVER_JKS.get()).setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
        req.response().setStatusCode(307).putHeader("location", "http://" + DEFAULT_HTTP_HOST + ":4043/" + DEFAULT_TEST_URI).end();
    });
    startServer(redirectServer);
    RequestOptions options = new RequestOptions().setHost(DEFAULT_HTTP_HOST).setURI(DEFAULT_TEST_URI).setPort(4043);
    testTLS(Cert.NONE, Trust.SERVER_JKS, Cert.NONE, Trust.NONE).clientSSL(true).serverSSL(false).requestOptions(options).followRedirects(true).pass();
}
Also used : RequestOptions(io.vertx.core.http.RequestOptions) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Test(org.junit.Test)

Example 2 with HttpServer

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

the class Http1xTLSTest method testRedirectToSSL.

// Redirect tests
@Test
public void testRedirectToSSL() throws Exception {
    HttpServer redirectServer = vertx.createHttpServer(new HttpServerOptions().setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
        req.response().setStatusCode(307).putHeader("location", "https://" + DEFAULT_HTTP_HOST + ":4043/" + DEFAULT_TEST_URI).end();
    });
    startServer(redirectServer);
    RequestOptions options = new RequestOptions().setHost(DEFAULT_HTTP_HOST).setURI(DEFAULT_TEST_URI).setPort(DEFAULT_HTTP_PORT);
    testTLS(Cert.NONE, Trust.SERVER_JKS, Cert.SERVER_JKS, Trust.NONE).clientSSL(false).serverSSL(true).requestOptions(options).followRedirects(true).pass();
}
Also used : RequestOptions(io.vertx.core.http.RequestOptions) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Test(org.junit.Test)

Example 3 with HttpServer

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

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

the class HttpMetricsTest method testHttpClientLifecycle.

private void testHttpClientLifecycle(HttpVersion protocol) throws Exception {
    HttpServer server = vertx.createHttpServer();
    CountDownLatch requestBeginLatch = new CountDownLatch(1);
    CountDownLatch requestBodyLatch = new CountDownLatch(1);
    CountDownLatch requestEndLatch = new CountDownLatch(1);
    CompletableFuture<Void> beginResponse = new CompletableFuture<>();
    CompletableFuture<Void> endResponse = new CompletableFuture<>();
    server.requestHandler(req -> {
        assertEquals(protocol, req.version());
        requestBeginLatch.countDown();
        req.handler(buff -> {
            requestBodyLatch.countDown();
        });
        req.endHandler(v -> {
            requestEndLatch.countDown();
        });
        Context ctx = vertx.getOrCreateContext();
        beginResponse.thenAccept(v1 -> {
            ctx.runOnContext(v2 -> {
                req.response().setChunked(true).write(TestUtils.randomAlphaString(1024));
            });
        });
        endResponse.thenAccept(v1 -> {
            ctx.runOnContext(v2 -> {
                req.response().end();
            });
        });
    });
    CountDownLatch listenLatch = new CountDownLatch(1);
    server.listen(8080, "localhost", onSuccess(s -> {
        listenLatch.countDown();
    }));
    awaitLatch(listenLatch);
    HttpClient client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(protocol));
    FakeHttpClientMetrics clientMetrics = FakeMetricsBase.getMetrics(client);
    CountDownLatch responseBeginLatch = new CountDownLatch(1);
    CountDownLatch responseEndLatch = new CountDownLatch(1);
    HttpClientRequest req = client.post(8080, "localhost", "/somepath", resp -> {
        responseBeginLatch.countDown();
        resp.endHandler(v -> {
            responseEndLatch.countDown();
        });
    }).setChunked(true);
    req.sendHead();
    awaitLatch(requestBeginLatch);
    HttpClientMetric reqMetric = clientMetrics.getMetric(req);
    assertEquals(0, reqMetric.requestEnded.get());
    assertEquals(0, reqMetric.responseBegin.get());
    req.write(TestUtils.randomAlphaString(1024));
    awaitLatch(requestBodyLatch);
    assertEquals(0, reqMetric.requestEnded.get());
    assertEquals(0, reqMetric.responseBegin.get());
    req.end();
    awaitLatch(requestEndLatch);
    assertEquals(1, reqMetric.requestEnded.get());
    assertEquals(0, reqMetric.responseBegin.get());
    beginResponse.complete(null);
    awaitLatch(responseBeginLatch);
    assertEquals(1, reqMetric.requestEnded.get());
    assertEquals(1, reqMetric.responseBegin.get());
    endResponse.complete(null);
    awaitLatch(responseEndLatch);
    assertNull(clientMetrics.getMetric(req));
    assertEquals(1, reqMetric.requestEnded.get());
    assertEquals(1, reqMetric.responseBegin.get());
}
Also used : Context(io.vertx.core.Context) FakeMetricsBase(io.vertx.test.fakemetrics.FakeMetricsBase) FakeMetricsFactory(io.vertx.test.fakemetrics.FakeMetricsFactory) HttpServerMetric(io.vertx.test.fakemetrics.HttpServerMetric) HttpServer(io.vertx.core.http.HttpServer) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Context(io.vertx.core.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientRequest(io.vertx.core.http.HttpClientRequest) FakeHttpServerMetrics(io.vertx.test.fakemetrics.FakeHttpServerMetrics) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpClientMetric(io.vertx.test.fakemetrics.HttpClientMetric) HttpClient(io.vertx.core.http.HttpClient) FakeHttpClientMetrics(io.vertx.test.fakemetrics.FakeHttpClientMetrics) CompletableFuture(java.util.concurrent.CompletableFuture) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClient(io.vertx.core.http.HttpClient) HttpServer(io.vertx.core.http.HttpServer) FakeHttpClientMetrics(io.vertx.test.fakemetrics.FakeHttpClientMetrics) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpClientMetric(io.vertx.test.fakemetrics.HttpClientMetric)

Example 5 with HttpServer

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

HttpServer (io.vertx.core.http.HttpServer)81 Router (io.vertx.ext.web.Router)37 HttpServerOptions (io.vertx.core.http.HttpServerOptions)33 Test (org.junit.Test)22 JsonObject (io.vertx.core.json.JsonObject)17 HttpClient (io.vertx.core.http.HttpClient)13 Future (io.vertx.core.Future)12 Vertx (io.vertx.core.Vertx)12 HttpServerResponse (io.vertx.core.http.HttpServerResponse)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 Buffer (io.vertx.core.buffer.Buffer)11 HttpMethod (io.vertx.core.http.HttpMethod)10 Handler (io.vertx.core.Handler)9 VertxOptions (io.vertx.core.VertxOptions)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 HttpClientOptions (io.vertx.core.http.HttpClientOptions)8 List (java.util.List)8 AbstractVerticle (io.vertx.core.AbstractVerticle)7 File (java.io.File)7 AsyncResult (io.vertx.core.AsyncResult)6