本文整理了Java中io.vertx.core.http.HttpClient.request()
方法的一些代码示例,展示了HttpClient.request()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpClient.request()
方法的具体详情如下:
包路径:io.vertx.core.http.HttpClient
类名称:HttpClient
方法名:request
[英]Create an HTTP request to send to the server at the specified host and port.
[中]创建HTTP请求以发送到指定主机和端口的服务器。
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testOtherMethodWithRawMethod() throws Exception {
try {
client.request(HttpMethod.OTHER, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
}).end();
fail();
} catch (IllegalStateException expected) {
}
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testRequestNPE() {
String uri = "/some-uri?foo=bar";
TestUtils.assertNullPointerException(() -> client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, uri, null));
TestUtils.assertNullPointerException(() -> client.request((HttpMethod)null, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, uri, resp -> {}));
TestUtils.assertNullPointerException(() -> client.requestAbs((HttpMethod) null, "http://someuri", resp -> {
}));
TestUtils.assertNullPointerException(() -> client.request(HttpMethod.GET, 8080, "localhost", "/somepath", null));
TestUtils.assertNullPointerException(() -> client.request((HttpMethod) null, 8080, "localhost", "/somepath", resp -> {
}));
TestUtils.assertNullPointerException(() -> client.request(HttpMethod.GET, 8080, null, "/somepath", resp -> {
}));
TestUtils.assertNullPointerException(() -> client.request(HttpMethod.GET, 8080, "localhost", null, resp -> {
}));
}
代码示例来源:origin: eclipse-vertx/vert.x
@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();
}
代码示例来源:origin: eclipse-vertx/vert.x
private void testAccessNetSocketPendingResponseData(boolean pause) {
server.requestHandler(req -> {
NetSocket so = req.netSocket();
so.write("hello");
});
server.listen(onSuccess(s -> {
HttpClientRequest req = client.request(HttpMethod.CONNECT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> {
NetSocket so = resp.netSocket();
assertNotNull(so);
so.handler(buff -> {
// With HTTP/1.1 the buffer is received immediately but delivered asynchronously
assertEquals("hello", buff.toString());
testComplete();
});
if (pause) {
so.pause();
vertx.setTimer(100, id -> {
so.resume();
});
}
}));
req.sendHead();
}));
await();
}
代码示例来源:origin: eclipse-vertx/vert.x
public void testRawMinCompression() {
clientraw.request(HttpMethod.GET, DEFAULT_HTTP_PORT - 1, DEFAULT_HTTP_HOST, "some-uri",
onSuccess(resp -> {
resp.bodyHandler(responseBuffer -> {
String responseCompressedBody = responseBuffer.toString(CharsetUtil.UTF_8);
Integer responseByteCount = responseCompressedBody.getBytes(CharsetUtil.UTF_8).length;
// assertEquals((Integer)1642,responseByteCount);
rawMinCompressionResponseByteCount = responseByteCount;
terminateTestWhenAllPassed();
});
})).putHeader(HttpHeaders.ACCEPT_ENCODING, HttpHeaders.DEFLATE_GZIP).end();
}
代码示例来源:origin: eclipse-vertx/vert.x
@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();
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testRequestTimeoutCanceledWhenRequestHasAnOtherError() {
AtomicReference<Throwable> exception = new AtomicReference<>();
// There is no server running, should fail to connect
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(exception::set))
.setTimeout(800)
.end();
vertx.setTimer(1500, id -> {
assertNotNull("Expected an exception to be set", exception.get());
assertFalse("Expected to not end with timeout exception, but did: " + exception.get(), exception.get() instanceof TimeoutException);
testComplete();
});
await();
}
代码示例来源:origin: eclipse-vertx/vert.x
private void testRawMinCompression() {
clientraw.request(HttpMethod.GET, DEFAULT_HTTP_PORT - 1, DEFAULT_HTTP_HOST, "some-uri",
onSuccess(resp -> {
resp.bodyHandler(responseBuffer -> {
String responseCompressedBody = responseBuffer.toString(CharsetUtil.UTF_8);
Integer responseByteCount = responseCompressedBody.getBytes(CharsetUtil.UTF_8).length;
// assertEquals((Integer)1642,responseByteCount);
rawMinCompressionResponseByteCount = responseByteCount;
terminateTestWhenAllPassed();
});
})).putHeader(HttpHeaders.ACCEPT_ENCODING, HttpHeaders.DEFLATE_GZIP).end();
}
代码示例来源:origin: eclipse-vertx/vert.x
@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();
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testConnectInvalidHost() {
client.request(HttpMethod.GET, 9998, "255.255.255.255", DEFAULT_TEST_URI, onFailure(resp -> testComplete()))
.exceptionHandler(t -> fail("Exception handler should not be called"))
.end();
await();
}
代码示例来源:origin: eclipse-vertx/vert.x
public void testRawMaxCompression() {
clientraw.request(HttpMethod.GET, DEFAULT_HTTP_PORT + 1, DEFAULT_HTTP_HOST, "some-uri",
onSuccess(resp -> {
resp.bodyHandler(responseBuffer -> {
String responseCompressedBody = responseBuffer.toString(CharsetUtil.UTF_8);
Integer responseByteCount = responseCompressedBody.getBytes(CharsetUtil.UTF_8).length;
//1606
// assertEquals((Integer)1606,responseByteCount);
// assertEquals(LARGE_HTML_STRING, responseBody);
rawMaxCompressionResponseByteCount = responseByteCount;
terminateTestWhenAllPassed();
});
})).putHeader(HttpHeaders.ACCEPT_ENCODING, HttpHeaders.DEFLATE_GZIP).end();
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testDefaultHttpVersion() {
server.requestHandler(req -> {
assertEquals(HttpVersion.HTTP_1_1, req.version());
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();
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testConnectInvalidPort() {
client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> testComplete()))
.exceptionHandler(t -> fail("Exception handler should not be called"))
.end();
await();
}
代码示例来源:origin: eclipse-vertx/vert.x
private void testRawMaxCompression() {
clientraw.request(HttpMethod.GET, DEFAULT_HTTP_PORT + 1, DEFAULT_HTTP_HOST, "some-uri",
onSuccess(resp -> {
resp.bodyHandler(responseBuffer -> {
String responseCompressedBody = responseBuffer.toString(CharsetUtil.UTF_8);
Integer responseByteCount = responseCompressedBody.getBytes(CharsetUtil.UTF_8).length;
//1606
// assertEquals((Integer)1606,responseByteCount);
// assertEquals(LARGE_HTML_STRING, responseBody);
rawMaxCompressionResponseByteCount = responseByteCount;
terminateTestWhenAllPassed();
});
})).putHeader(HttpHeaders.ACCEPT_ENCODING, HttpHeaders.DEFLATE_GZIP).end();
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testServerChaining() {
server.requestHandler(req -> {
assertTrue(req.response().setChunked(true) == req.response());
assertTrue(req.response().write("foo", "UTF-8") == req.response());
assertTrue(req.response().write("foo") == req.response());
testComplete();
});
server.listen(onSuccess(server -> {
client.request(HttpMethod.PUT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler()).end();
}));
await();
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testClientExceptionHandlerCalledWhenFailingToConnect() throws Exception {
client.request(HttpMethod.GET, 9998, "255.255.255.255", DEFAULT_TEST_URI, onFailure(err ->
testComplete()))
.exceptionHandler(error -> fail("Exception handler should not be called"))
.end();
await();
}
代码示例来源:origin: eclipse-vertx/vert.x
private void testMinCompression() {
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT - 1, DEFAULT_HTTP_HOST, "some-uri",
onSuccess(resp -> {
resp.bodyHandler(responseBuffer -> {
String responseBody = responseBuffer.toString(CharsetUtil.UTF_8);
assertEquals(COMPRESS_TEST_STRING, responseBody);
minCompressionTestPassed = true;
terminateTestWhenAllPassed();
});
})).end();
}
代码示例来源:origin: eclipse-vertx/vert.x
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();
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testServerDrainHandler() {
drainingServer(resumeFuture -> {
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> {
resp.pause();
resumeFuture.setHandler(ar -> resp.resume());
})).end();
});
await();
}
代码示例来源:origin: eclipse-vertx/vert.x
public void testMinCompression() {
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT - 1, DEFAULT_HTTP_HOST, "some-uri",
onSuccess(resp -> {
resp.bodyHandler(responseBuffer -> {
String responseBody = responseBuffer.toString(CharsetUtil.UTF_8);
assertEquals(COMPRESS_TEST_STRING, responseBody);
minCompressionTestPassed = true;
terminateTestWhenAllPassed();
});
})).end();
}
内容来源于网络,如有侵权,请联系作者删除!