@Test public void testClientConnectInvalidPort() { client.getNow(-1, "localhost", "/someuri", onFailure(err -> { assertEquals(err.getClass(), IllegalArgumentException.class); assertEquals(err.getMessage(), "port p must be in range 0 <= p <= 65535"); testComplete(); })); await(); }
private void testURIAndPath(String uri, String path) { server.requestHandler(req -> { assertEquals(uri, req.uri()); assertEquals(path, req.path()); req.response().end(); }); server.listen(onSuccess(server -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, uri, resp -> testComplete()).end(); })); await(); }
private void testParams(char delim) { Map<String, String> params = genMap(10); String query = generateQueryString(params, delim); server.requestHandler(req -> { assertEquals(query, req.query()); assertEquals(params.size(), req.params().size()); for (Map.Entry<String, String> entry : req.params()) { assertEquals(entry.getValue(), params.get(entry.getKey())); } req.response().end(); }); server.listen(onSuccess(server -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "some-uri/?" + query, resp -> testComplete()).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(); }
@Test public void testPutHeadersOnRequest() { server.requestHandler(req -> { assertEquals("bar", req.headers().get("foo")); assertEquals("bar", req.getHeader("foo")); req.response().end(); }); server.listen(onSuccess(server -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); })).putHeader("foo", "bar").end(); })); await(); }
@Test public void testRequestBodyBufferAtEnd() { Buffer body = TestUtils.randomBuffer(1000); server.requestHandler(req -> req.bodyHandler(buffer -> { assertEquals(body, buffer); req.response().end(); })); server.listen(onSuccess(server -> { client.request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> testComplete()).end(body); })); await(); }
@Test public void testHostHeaderOverridePossible() { server.requestHandler(req -> { assertEquals("localhost:4444", req.host()); req.response().end(); }); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> testComplete()); req.setHost("localhost:4444"); req.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(); }
@Test public void testRemoteAddress() { server.requestHandler(req -> { assertEquals("127.0.0.1", req.remoteAddress().host()); req.response().end(); }); server.listen(onSuccess(s -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> resp.endHandler(v -> testComplete()))).end(); })); await(); }
@Test public void testGetAbsoluteURI() { server.requestHandler(req -> { assertEquals(req.scheme() + "://localhost:" + DEFAULT_HTTP_PORT + "/foo/bar", req.absoluteURI()); req.response().end(); }); server.listen(onSuccess(s -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/foo/bar", onSuccess(resp -> 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 testResponseBodyBufferAtEnd() { Buffer body = TestUtils.randomBuffer(1000); server.requestHandler(req -> { req.response().end(body); }); server.listen(onSuccess(s -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { resp.bodyHandler(buff -> { assertEquals(body, buff); testComplete(); }); })).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(); }
@Test public void testClientLocalAddress() throws Exception { String expectedAddress = TestUtils.loopbackAddress(); client.close(); client = vertx.createHttpClient(createBaseClientOptions().setLocalAddress(expectedAddress)); server.requestHandler(req -> { assertEquals(expectedAddress, req.remoteAddress().host()); req.response().end(); }); startServer(); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); })); await(); }
@Test public void testResetContentSetsContentLengthHeader() throws Exception { MultiMap respHeaders = checkEmptyHttpResponse(HttpMethod.GET, 205, MultiMap.caseInsensitiveMultiMap()); assertEquals("0", respHeaders.get("content-length")); assertNull(respHeaders.get("transfer-encoding")); }
@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 testHeadAllowsContentLengthHeader() throws Exception { MultiMap respHeaders = checkEmptyHttpResponse(HttpMethod.HEAD, 200, MultiMap.caseInsensitiveMultiMap().set("content-length", "34")); assertEquals("34", respHeaders.get("content-length")); assertNull(respHeaders.get("transfer-encoding")); }
@Test public void testNotModifiedAllowsContentLengthHeader() throws Exception { MultiMap respHeaders = checkEmptyHttpResponse(HttpMethod.GET, 304, MultiMap.caseInsensitiveMultiMap().set("content-length", "34")); assertEquals("34", respHeaders.get("Content-Length")); assertNull(respHeaders.get("transfer-encoding")); }
@Test public void testResetContentRemovesTransferEncodingHeader() throws Exception { MultiMap respHeaders = checkEmptyHttpResponse(HttpMethod.GET, 205, MultiMap.caseInsensitiveMultiMap().set("transfer-encoding", "chunked")); assertEquals("0", respHeaders.get("content-length")); assertNull(respHeaders.get("transfer-encoding")); }
@Test public void testSendRangeFileFromClasspath() { vertx.createHttpServer(new HttpServerOptions().setPort(8080)).requestHandler(res -> { res.response().sendFile("webroot/somefile.html", 6, 6); }).listen(onSuccess(res -> { vertx.createHttpClient(new HttpClientOptions()).request(HttpMethod.GET, 8080, "localhost", "/", onSuccess(resp -> { resp.bodyHandler(buff -> { assertEquals("<body>", buff.toString()); testComplete(); }); })).end(); })); await(); }