@Test public void testServerActualPortWhenSet() { server .requestHandler(request -> { request.response().end("hello"); }) .listen(ar -> { assertEquals(ar.result().actualPort(), DEFAULT_HTTP_PORT); vertx.createHttpClient(createBaseClientOptions()).getNow(ar.result().actualPort(), DEFAULT_HTTP_HOST, "/", onSuccess(response -> { assertEquals(response.statusCode(), 200); response.bodyHandler(body -> { assertEquals(body.toString("UTF-8"), "hello"); testComplete(); }); })); }); await(); }
@Test public void test100ContinueRejectedManually() throws Exception { server.close(); server = vertx.createHttpServer(createBaseServerOptions()); server.requestHandler(req -> { req.response().setStatusCode(405).end(); req.bodyHandler(data -> { fail("body should not be received"); }); }); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.PUT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(405, resp.statusCode()); testComplete(); })); req.headers().set("Expect", "100-continue"); req.setChunked(true); req.continueHandler(v -> { fail("should not be called"); }); req.sendHead(); })); await(); }
@Override public void start() throws Exception { // Build the Jax-RS hello world deployment VertxResteasyDeployment deployment = new VertxResteasyDeployment(); deployment.start(); deployment.getRegistry().addPerInstanceResource(HelloWorldService.class); // Start the front end server using the Jax-RS controller vertx.createHttpServer() .requestHandler(new VertxRequestHandler(vertx, deployment)) .listen(8080, ar -> { System.out.println("Server started on port "+ ar.result().actualPort()); }); } }
@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()); } }); } }
@Test public void testSendFileNotFound() throws Exception { server.requestHandler(req -> { req.response().putHeader("Content-Type", "wibble"); req.response().sendFile("nosuchfile.html"); }); server.listen(onSuccess(s -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> {})).end(); vertx.setTimer(100, tid -> testComplete()); })); await(); }
.addEnabledCipherSuite("TLS_RSA_WITH_AES_128_CBC_SHA") // Non Diffie-helman -> debuggable in wireshark .setPemKeyCertOptions(Cert.SERVER_PEM.get()).setSslEngineOptions(new OpenSSLEngineOptions()); server.close(); client.close(); client = vertx.createHttpClient(createBaseClientOptions()); server = vertx.createHttpServer(opts); server.requestHandler(req -> { req.response().end(); }); CountDownLatch latch = new CountDownLatch(1); System.out.println("starting"); try { server.listen(onSuccess(v -> latch.countDown())); } catch (Throwable e) { e.printStackTrace(); client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); })).exceptionHandler(this::fail).end(); await();
@Test public void testPutHeaderReplacesPreviousHeaders() throws Exception { server.requestHandler(req -> req.response() .putHeader("Location", "http://example1.org") .putHeader("location", "http://example2.org") .end()); server.listen(onSuccess(server -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(singletonList("http://example2.org"), resp.headers().getAll("LocatioN")); testComplete(); })).end(); })); await(); }
System.setProperty("vertx.disableH2c", "true"); try { server.close(); server = vertx.createHttpServer(serverOptions.setUseAlpn(false).setSsl(false).setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT)); server.requestHandler(req -> { MultiMap headers = req.headers(); String upgrade = headers.get("upgrade"); assertEquals(DEFAULT_HTTP_HOST + ":" + DEFAULT_HTTP_PORT, req.host()); if ("h2c".equals(upgrade)) { req.response().setStatusCode(400).end(); } else { req.response().end("wibble"); assertEquals(HttpVersion.HTTP_1_1, req.version()); }); startServer(); client.close(); client = vertx.createHttpClient(clientOptions.setUseAlpn(false).setSsl(false)); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { assertEquals(400, resp.statusCode()); assertEquals(HttpVersion.HTTP_1_1, resp.version()); resp.bodyHandler(body -> { testComplete(); }); })).exceptionHandler(this::fail).end(); await(); } finally {
@Test public void testClientMaxHeaderSizeOption() { String longHeader = TestUtils.randomAlphaString(9000); // min 9023 = 9000 for longHeader and 23 for "Content-Length: 0 t: " vertx.createHttpServer(new HttpServerOptions().setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> { // Add longHeader req.response().putHeader("t", longHeader).end(); }).listen(onSuccess(res -> { HttpClientRequest req = vertx.createHttpClient(new HttpClientOptions().setMaxHeaderSize(10000)) .request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(resp.getHeader("t"), longHeader); testComplete(); })); req.end(); })); await(); }
private void testServerMaxInitialLineLength(int maxInitialLength) { String longParam = TestUtils.randomAlphaString(5000); server.close(); server = vertx.createHttpServer(new HttpServerOptions().setMaxInitialLineLength(maxInitialLength) .setHost("localhost").setPort(8080)).requestHandler(req -> { assertEquals(req.getParam("t"), longParam); req.response().end(); }).listen(onSuccess(res -> { HttpClientRequest req = vertx.createHttpClient(new HttpClientOptions()) .request(HttpMethod.GET, 8080, "localhost", "/?t=" + longParam, onSuccess(resp -> { if (maxInitialLength > HttpServerOptions.DEFAULT_MAX_INITIAL_LINE_LENGTH) { assertEquals(200, resp.statusCode()); testComplete(); } else { assertEquals(414, resp.statusCode()); resp.request().connection().closeHandler(v -> { testComplete(); }); } })); req.end(); })); await(); }
@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, onSuccess(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(); }
private void testResponseCompression(boolean enabled) throws Exception { byte[] expected = TestUtils.randomAlphaString(1000).getBytes(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream in = new GZIPOutputStream(baos); in.write(expected); in.close(); byte[] compressed = baos.toByteArray(); server.close(); server = vertx.createHttpServer(serverOptions); server.requestHandler(req -> { assertEquals(enabled ? "deflate, gzip" : null, req.getHeader(HttpHeaderNames.ACCEPT_ENCODING)); req.response().putHeader(HttpHeaderNames.CONTENT_ENCODING.toLowerCase(), "gzip").end(Buffer.buffer(compressed)); }); startServer(); client.close(); client = vertx.createHttpClient(clientOptions.setTryUseCompression(enabled)); client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> { String encoding = resp.getHeader(HttpHeaderNames.CONTENT_ENCODING); assertEquals(enabled ? null : "gzip", encoding); resp.bodyHandler(buff -> { assertEquals(Buffer.buffer(enabled ? expected : compressed), buff); testComplete(); }); })).end(); await(); }
@Test public void testSendFileFromClasspath() { vertx.createHttpServer(new HttpServerOptions().setPort(8080)).requestHandler(res -> { res.response().sendFile(webRoot + "/somefile.html"); }).listen(onSuccess(res -> { vertx.createHttpClient(new HttpClientOptions()).request(HttpMethod.GET, 8080, "localhost", "/", onSuccess(resp -> { resp.bodyHandler(buff -> { assertTrue(buff.toString().startsWith("<html><body>blah</body></html>")); testComplete(); }); })).end(); })); await(); }
@Test public void testSharedServersRoundRobin() throws Exception { client.close(); server.close(); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false)); int numServers = VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE / 2- 1; int numRequests = numServers * 100; Set<Context> contexts = new ConcurrentHashSet<>(); for (int i = 0; i < numServers; i++) { HttpServer theServer = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)); servers.add(theServer); final AtomicReference<Context> context = new AtomicReference<>(); theServer.requestHandler(req -> { Context ctx = Vertx.currentContext(); if (context.get() != null) { assertSame(ctx, context.get()); requestCount.put(theServer, icnt); latchConns.countDown(); req.response().end(); }).listen(onSuccess(s -> { assertEquals(DEFAULT_HTTP_PORT, s.actualPort()); latchListen.countDown(); })); server.close(ar -> { assertTrue(ar.succeeded()); closeLatch.countDown();
@Test public void testDisableIdleTimeoutClearTextUpgrade() throws Exception { server.close(); server = vertx.createHttpServer(new HttpServerOptions() .setPort(8080) .setHost("localhost")); server.requestHandler(req -> { req.response().end(); }); startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setIdleTimeout(2) .setProtocolVersion(HttpVersion.HTTP_2) .setDefaultPort(8080) .setDefaultHost("localhost")); client.get("/somepath", onSuccess(resp1 -> { resp1.exceptionHandler(this::fail); resp1.endHandler(v1 -> { vertx.setTimer(10, id1 -> { client.get("/somepath", onSuccess(resp2 -> { resp2.exceptionHandler(this::fail); resp2.endHandler(v2 -> { testComplete(); }); })).exceptionHandler(this::fail).end(); }); }); })).exceptionHandler(this::fail).end(); await();
@Test public void test100Continue() throws Exception { AtomicInteger status = new AtomicInteger(); server.close(); server = vertx.createHttpServer(serverOptions.setHandle100ContinueAutomatically(true)); server.requestHandler(req -> { status.getAndIncrement(); HttpServerResponse resp = req.response(); req.bodyHandler(body -> { assertEquals(2, status.getAndIncrement()); assertEquals("request-body", body.toString()); resp.putHeader("wibble", "wibble-value").end("response-body"); }); }); startServer(); HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> { assertEquals(3, status.getAndIncrement()); resp.bodyHandler(body -> { assertEquals(4, status.getAndIncrement()); assertEquals("response-body", body.toString());
@Test public void testContentDecompression() throws Exception { server.close(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setDecompressionSupported(true)); String expected = TestUtils.randomAlphaString(1000); byte[] dataGzipped = TestUtils.compressGzip(expected); server.requestHandler(req -> { assertEquals("localhost:" + DEFAULT_HTTP_PORT, req.headers().get("host")); req.bodyHandler(buffer -> { assertEquals(expected, buffer.toString()); req.response().end(); }); }); server.listen(onSuccess(server -> { client .request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "some-uri", onSuccess(resp -> testComplete())) .putHeader("Content-Encoding", "gzip") .end(Buffer.buffer(dataGzipped)); })); await(); }
@Test public void testHttp10RequestNonKeepAliveConnectionClosed() throws Exception { client.close(); server.requestHandler(req -> { assertEquals(HttpVersion.HTTP_1_0, req.version()); assertNull(req.getHeader("Connection")); req.response().end(); assertTrue(req.response().closed()); }); server.listen(onSuccess(s -> { client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_1_0).setKeepAlive(false)); HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { resp.endHandler(v -> { assertNull(resp.getHeader("Connection")); testComplete(); }); })); req.end(); })); await(); }
@Test public void testServerOptionsCopiedBeforeUse() { server.close(); HttpServerOptions options = new HttpServerOptions().setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT); HttpServer server = vertx.createHttpServer(options); // Now change something - but server should still listen at previous port options.setPort(DEFAULT_HTTP_PORT + 1); server.requestHandler(req -> { req.response().end(); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/uri", onSuccess(res -> { assertEquals(200, res.statusCode()); testComplete(); })).end(); }); await(); }
@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", onSuccess(resp -> { Buffer buffer = Buffer.buffer(); resp.handler(buffer::appendBuffer); resp.endHandler(v -> { assertEquals(Buffer.buffer("foo"), buffer); testComplete(); }); })); await(); } finally { client.close(); server.close(); } }