@Override public void start() throws Exception { super.start(); HttpServer server = vertx.createHttpServer(); server.requestHandler(req -> { if (req.method() == HttpMethod.GET) { req.response().setChunked(true); if (req.path().equals("/products")) { vertx.eventBus().<String>send(SpringDemoVerticle.ALL_PRODUCTS_ADDRESS, "", result -> { if (result.succeeded()) { req.response().setStatusCode(200).write(result.result().body()).end(); } else { req.response().setStatusCode(500).write(result.cause().toString()).end(); } }); } else { req.response().setStatusCode(200).write("Hello from vert.x").end(); } } else { // We only support GET for now req.response().setStatusCode(405).end(); } }); server.listen(8080); } }
private void testSimpleRequest(String uri, HttpMethod method, HttpClientRequest request, boolean absolute) { int index = uri.indexOf('?'); String query; String path; if (index == -1) { path = uri; query = null; } else { path = uri.substring(0, index); query = uri.substring(index + 1); } String resource = absolute && path.isEmpty() ? "/" + path : path; server.requestHandler(req -> { String expectedPath = req.method() == HttpMethod.CONNECT && req.version() == HttpVersion.HTTP_2 ? null : resource; String expectedQuery = req.method() == HttpMethod.CONNECT && req.version() == HttpVersion.HTTP_2 ? null : query; assertEquals(expectedPath, req.path()); assertEquals(method, req.method()); assertEquals(expectedQuery, req.query()); req.response().end(); }); server.listen(onSuccess(server -> request.end())); await(); }
@Test public void testOtherMethodRequest() throws Exception { server.requestHandler(r -> { assertEquals(HttpMethod.OTHER, r.method()); assertEquals("COPY", r.rawMethod()); r.response().end(); }).listen(onSuccess(s -> { client.request(HttpMethod.OTHER, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { testComplete(); }).setRawMethod("COPY").end(); })); await(); }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); vertx.createHttpServer().requestHandler(req -> { System.out.println("Proxying request: " + req.uri()); HttpClientRequest c_req = client.request(req.method(), 8282, "localhost", req.uri(), c_res -> { System.out.println("Proxying response: " + c_res.statusCode()); req.response().setChunked(true); req.response().setStatusCode(c_res.statusCode()); req.response().headers().setAll(c_res.headers()); c_res.handler(data -> { System.out.println("Proxying response body: " + data.toString("ISO-8859-1")); req.response().write(data); }); c_res.endHandler((v) -> req.response().end()); }); c_req.setChunked(true); c_req.headers().setAll(req.headers()); req.handler(data -> { System.out.println("Proxying request body " + data.toString("ISO-8859-1")); c_req.write(data); }); req.endHandler((v) -> c_req.end()); }).listen(8080); } }
private void testFollowRedirectWithBody(Function<Buffer, Buffer> translator) throws Exception { Buffer expected = TestUtils.randomBuffer(2048); AtomicBoolean redirected = new AtomicBoolean(); server.requestHandler(req -> { if (redirected.compareAndSet(false, true)) { assertEquals(HttpMethod.PUT, req.method()); req.bodyHandler(body -> { assertEquals(body, expected); String scheme = createBaseServerOptions().isSsl() ? "https" : "http"; req.response().setStatusCode(303).putHeader(HttpHeaders.LOCATION, scheme + "://localhost:8080/whatever").end(); }); } else { assertEquals(HttpMethod.GET, req.method()); req.response().end(); } }); startServer(); client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); })).setFollowRedirects(true).end(translator.apply(expected)); await(); }
@Test public void testHeadHasNoContentLengthByDefault() { server.requestHandler(req -> { assertEquals(HttpMethod.HEAD, req.method()); // By default HEAD does not have a content-length header req.response().end(); }); server.listen(onSuccess(s -> { client.request(HttpMethod.HEAD, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertNull(resp.headers().get(HttpHeaders.CONTENT_LENGTH)); resp.endHandler(v -> testComplete()); })).end(); })); await(); }
@Test public void testHeadCanSetContentLength() { server.requestHandler(req -> { assertEquals(HttpMethod.HEAD, req.method()); // Head never contains a body but it can contain a Content-Length header // Since headers from HEAD must correspond EXACTLY with corresponding headers for GET req.response().headers().set("Content-Length", String.valueOf(41)); req.response().end(); }); server.listen(onSuccess(s -> { client.request(HttpMethod.HEAD, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals("41", resp.headers().get("Content-Length")); resp.endHandler(v -> testComplete()); })).end(); })); await(); }
@Test public void testHeadButCanSetContentLength() { server.requestHandler(req -> { assertEquals(HttpMethod.HEAD, req.method()); // By default HEAD does not have a content-length header but it can contain a content-length header // if explicitly set req.response().putHeader(HttpHeaders.CONTENT_LENGTH, "41").end(); }); server.listen(onSuccess(s -> { client.request(HttpMethod.HEAD, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals("41", resp.headers().get(HttpHeaders.CONTENT_LENGTH)); resp.endHandler(v -> testComplete()); })).end(); })); await(); }
try { if (method) { req.method(); } else { req.version();
private void testPost(String expected) throws Exception { Buffer content = Buffer.buffer(); AtomicInteger count = new AtomicInteger(); server.requestHandler(req -> { assertEquals(HttpMethod.POST, req.method()); req.handler(buff -> { content.appendBuffer(buff); count.getAndIncrement(); }); req.endHandler(v -> { assertTrue(count.get() > 0); req.response().end(); }); }); startServer(); client.post(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> { resp.endHandler(v -> { assertEquals(expected, content.toString()); testComplete(); }); })).exceptionHandler(err -> { fail(); }).end(expected); await(); }
@Test public void testFollowRedirectSendHeadThenBody() throws Exception { Buffer expected = Buffer.buffer(TestUtils.randomAlphaString(2048)); AtomicBoolean redirected = new AtomicBoolean(); server.requestHandler(req -> { if (redirected.compareAndSet(false, true)) { assertEquals(HttpMethod.PUT, req.method()); req.bodyHandler(body -> { assertEquals(body, expected); req.response().setStatusCode(303).putHeader(HttpHeaders.LOCATION, "/whatever").end(); }); } else { assertEquals(HttpMethod.GET, req.method()); req.response().end(); } }); startServer(); HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); })).setFollowRedirects(true); req.putHeader("Content-Length", "" + expected.length()); req.exceptionHandler(this::fail); req.sendHead(v -> { req.end(expected); }); await(); }
assertEquals(t, req.absoluteURI()); assertEquals("foo_value", req.getHeader("foo")); assertEquals(expectedMethod, req.method()); resp.end();
@Test public void testBeginPipelinedRequestByResponseSentBeforeRequestCompletion() throws Exception { server.requestHandler(req -> { if (req.method() == HttpMethod.POST) { req.pause(); vertx.setTimer(100, id1 -> { req.response().end(); vertx.setTimer(100, id2 -> { req.resume(); }); }); } else if (req.method() == HttpMethod.GET) { req.response().end(); } }); startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setPipelining(true).setMaxPoolSize(1).setKeepAlive(true)); client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { }).end(TestUtils.randomAlphaString(1024)); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { testComplete(); }); await(); }
server = vertx.createHttpServer(options); server.requestHandler(request -> { HttpMethod method = request.method(); String uri = request.uri(); if (username != null) {
AtomicInteger attributeCount = new AtomicInteger(); server.requestHandler(req -> { if (req.method() == HttpMethod.POST) { assertEquals(req.path(), "/form"); req.response().setChunked(true);
AtomicInteger attributeCount = new AtomicInteger(); server.requestHandler(req -> { if (req.method() == HttpMethod.POST) { assertEquals(req.path(), "/form"); req.setExpectMultipart(true);
@Test public void testBeginPipelinedRequestByResponseSentOnRequestCompletion() throws Exception { server.requestHandler(req -> { if (req.method() == HttpMethod.POST) { req.pause(); vertx.setTimer(100, id -> { req.resume(); }); } req.endHandler(v -> { req.response().end(); }); }); startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setPipelining(true).setMaxPoolSize(1).setKeepAlive(true)); client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { }).end(TestUtils.randomAlphaString(1024)); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { testComplete(); }); await(); }
if (req.method() == HttpMethod.POST) { assertEquals(req.path(), "/form"); req.response().setChunked(true);
server.requestHandler(req -> { assertEquals("https", req.scheme()); assertEquals(HttpMethod.GET, req.method()); assertEquals("/somepath", req.path()); assertEquals(DEFAULT_HTTPS_HOST_AND_PORT, req.host());