本文整理了Java中com.linecorp.armeria.client.HttpClient
类的一些代码示例,展示了HttpClient
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpClient
类的具体详情如下:
包路径:com.linecorp.armeria.client.HttpClient
类名称:HttpClient
[英]An HTTP client.
[中]HTTP客户端。
代码示例来源:origin: line/armeria
@Override
public CompletableFuture<Boolean> isHealthy(Endpoint endpoint) {
return httpClient.get(healthCheckPath)
.aggregate()
.thenApply(message -> HttpStatus.OK.equals(message.status()));
}
}
代码示例来源:origin: line/armeria
/**
* Creates a new HTTP client that connects to the specified {@code uri} using the default
* {@link ClientFactory}.
*
* @param uri the URI of the server endpoint
* @param options the {@link ClientOptionValue}s
*
* @throws IllegalArgumentException if the scheme of the specified {@code uri} is not an HTTP scheme
*/
static HttpClient of(String uri, ClientOptionValue<?>... options) {
return of(ClientFactory.DEFAULT, uri, options);
}
代码示例来源:origin: line/armeria
/**
* Sends an HTTP OPTIONS request.
*/
default HttpResponse options(String path) {
return execute(HttpHeaders.of(HttpMethod.OPTIONS, path));
}
代码示例来源:origin: line/armeria
@Test
public void https() throws Exception {
final ClientFactory clientFactory = new ClientFactoryBuilder()
.sslContextCustomizer(b -> b.trustManager(InsecureTrustManagerFactory.INSTANCE))
.build();
final HttpClient client = HttpClient.of(clientFactory, server().httpsUri("/"));
final AggregatedHttpMessage response = client.get("/jsp/index.jsp").aggregate().get();
final String actualContent = CR_OR_LF.matcher(response.content().toStringUtf8())
.replaceAll("");
assertThat(actualContent).isEqualTo(
"<html><body>" +
"<p>Hello, Armerian World!</p>" +
"<p>Have you heard about the class 'org.slf4j.Logger'?</p>" +
"<p>Context path: </p>" + // ROOT context path
"<p>Request URI: /index.jsp</p>" +
"<p>Scheme: https</p>" +
"</body></html>");
}
代码示例来源:origin: line/armeria
private static AggregatedHttpMessage makeMetricsRequest() throws ExecutionException,
InterruptedException {
final HttpClient client = HttpClient.of("http://127.0.0.1:" + server.httpPort());
return client.execute(HttpHeaders.of(HttpMethod.GET, "/internal/prometheus/metrics")
.setObject(HttpHeaderNames.ACCEPT, MediaType.PLAIN_TEXT_UTF_8))
.aggregate().get();
}
代码示例来源:origin: line/armeria
@Test
public void shouldGetNotFound() {
protocols.forEach(scheme -> {
final HttpClient client = HttpClient.of(clientFactory, scheme + "://example.com:" + port);
assertThat(client.get("/route2").aggregate().join().status()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(client.execute(
HttpHeaders.of(HttpMethod.POST, "/route2")
.contentType(com.linecorp.armeria.common.MediaType.PLAIN_TEXT_UTF_8),
HttpData.of("text".getBytes())).aggregate().join().status())
.isEqualTo(HttpStatus.NOT_FOUND);
});
}
}
代码示例来源:origin: line/armeria
@Test
public void testInjectionService() {
AggregatedHttpMessage res;
res = client.get("/injection/param/armeria/1?gender=male").aggregate().join();
assertThat(res.status()).isEqualTo(HttpStatus.OK);
assertThatJson(res.content().toStringUtf8()).isArray()
.ofLength(3)
.thatContains("armeria")
.thatContains(1)
.thatContains("MALE");
final HttpHeaders headers = HttpHeaders.of(HttpMethod.GET, "/injection/header")
.add(HttpHeaderNames.of("x-armeria-text"), "armeria")
.add(HttpHeaderNames.of("x-armeria-sequence"), "1")
.add(HttpHeaderNames.of("x-armeria-sequence"), "2")
.add(HttpHeaderNames.COOKIE, "a=1")
.add(HttpHeaderNames.COOKIE, "b=1");
res = client.execute(headers).aggregate().join();
assertThat(res.status()).isEqualTo(HttpStatus.OK);
assertThatJson(res.content().toStringUtf8()).isArray()
.ofLength(3)
.thatContains("armeria")
.thatContains(Arrays.asList(1, 2))
.thatContains(Arrays.asList("a", "b"));
}
代码示例来源:origin: line/armeria
@Test
public void shouldReturnBadRequestDueToException() {
final ArmeriaReactiveWebServerFactory factory = factory();
runServer(factory, AlwaysFailureHandler.INSTANCE, server -> {
final HttpClient client = httpClient(server);
final AggregatedHttpMessage res1 = client.post("/hello", "hello").aggregate().join();
assertThat(res1.status()).isEqualTo(com.linecorp.armeria.common.HttpStatus.BAD_REQUEST);
final AggregatedHttpMessage res2 = client.get("/hello").aggregate().join();
assertThat(res2.status()).isEqualTo(com.linecorp.armeria.common.HttpStatus.BAD_REQUEST);
});
}
代码示例来源:origin: line/armeria
private static void makeUnframedRequest(String name) throws Exception {
final HttpClient client = new ClientBuilder(server.uri(SerializationFormat.NONE, "/"))
.factory(clientFactory)
.addHttpHeader(HttpHeaderNames.CONTENT_TYPE, MediaType.PROTOBUF.toString())
.build(HttpClient.class);
final SimpleRequest request =
SimpleRequest.newBuilder()
.setPayload(Payload.newBuilder()
.setBody(ByteString.copyFromUtf8(name)))
.build();
try {
client.post("/armeria.grpc.testing.TestService/UnaryCall2", request.toByteArray());
} catch (Throwable t) {
// Ignore, we will count these up
}
}
}
代码示例来源:origin: line/armeria
@Test
public void shouldGetHelloFromRestController() throws Exception {
protocols.forEach(scheme -> {
final HttpClient client = HttpClient.of(clientFactory, scheme + "://example.com:" + port);
final AggregatedHttpMessage response = client.get("/hello").aggregate().join();
assertThat(response.content().toStringUtf8()).isEqualTo("hello");
});
}
代码示例来源:origin: line/armeria
@Test
public void withoutTracking() throws Exception {
final HttpClient client = HttpClient.of(rule.uri("/"));
assertThat(client.execute(HttpHeaders.of(HttpMethod.GET, "/foo")).aggregate().get().status())
.isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
代码示例来源:origin: line/armeria
@Test
public void shouldGetHelloFromRouter() throws Exception {
protocols.forEach(scheme -> {
final HttpClient client = HttpClient.of(clientFactory, scheme + "://example.com:" + port);
final AggregatedHttpMessage res = client.get("/route").aggregate().join();
assertThat(res.content().toStringUtf8()).isEqualTo("route");
final AggregatedHttpMessage res2 =
client.execute(HttpHeaders.of(HttpMethod.POST, "/route2")
.contentType(com.linecorp.armeria.common.MediaType.JSON_UTF_8),
HttpData.of("{\"a\":1}".getBytes())).aggregate().join();
assertThatJson(res2.content().toStringUtf8()).isArray()
.ofLength(1)
.thatContains("route");
});
}
代码示例来源:origin: line/centraldogma
@Test
public void test() {
final HttpClient client = new HttpClientBuilder(rule.uri("/"))
.addHttpHeader(HttpHeaderNames.AUTHORIZATION, "bearer " + secret).build();
AggregatedHttpMessage response;
response = client.get("/projects/" + projectName).aggregate().join();
assertThat(response.status())
.isEqualTo(role == ProjectRole.OWNER || role == ProjectRole.MEMBER ? HttpStatus.OK
: expectedFailureStatus);
response = client.post("/projects/" + projectName + "/repos/" + repoName, HttpData.EMPTY_DATA)
.aggregate().join();
assertThat(response.status()).isEqualTo(permission.contains(Permission.WRITE) ? HttpStatus.OK
: expectedFailureStatus);
response = client.get("/projects/" + projectName + "/repos/" + repoName)
.aggregate().join();
assertThat(response.status()).isEqualTo(permission.isEmpty() ? expectedFailureStatus
: HttpStatus.OK);
}
}
代码示例来源:origin: line/armeria
@Test
public void shouldGetHelloFromRestController() throws Exception {
final HttpClient client = HttpClient.of("http://127.0.0.1:" + port);
final AggregatedHttpMessage response = client.get("/proxy?port=" + port).aggregate().join();
assertThat(response.content().toStringUtf8()).isEqualTo("hello");
}
}
代码示例来源:origin: line/armeria
@Test
public void enableTracking() throws Exception {
try {
RequestContextAssembly.enable();
final HttpClient client = HttpClient.of(rule.uri("/"));
assertThat(client.execute(HttpHeaders.of(HttpMethod.GET, "/foo")).aggregate().get().status())
.isEqualTo(HttpStatus.OK);
} finally {
RequestContextAssembly.disable();
}
}
代码示例来源:origin: line/armeria
@Test
public void shouldRespondAuthnRequest_HttpRedirect() throws Exception {
final AggregatedHttpMessage resp = client.get("/redirect").aggregate().join();
assertThat(resp.status()).isEqualTo(HttpStatus.FOUND);
// Check the order of the parameters in the quest string.
final String location = resp.headers().get(HttpHeaderNames.LOCATION);
final Pattern p = Pattern.compile(
"http://idp\\.example\\.com/saml/sso/redirect\\?" +
"SAMLRequest=([^&]+)&RelayState=([^&]+)&SigAlg=([^&]+)&Signature=(.+)$");
assertThat(p.matcher(location).matches()).isTrue();
final QueryStringDecoder decoder = new QueryStringDecoder(location, true);
assertThat(decoder.parameters().get(SIGNATURE_ALGORITHM).get(0)).isEqualTo(signatureAlgorithm);
}
代码示例来源:origin: line/armeria
/**
* Sends an HTTP HEAD request.
*/
default HttpResponse head(String path) {
return execute(HttpHeaders.of(HttpMethod.HEAD, path));
}
代码示例来源:origin: line/armeria
/**
* Creates a new HTTP client that connects to the specified {@link URI} using the default
* {@link ClientFactory}.
*
* @param uri the URI of the server endpoint
* @param options the {@link ClientOptionValue}s
*
* @throws IllegalArgumentException if the scheme of the specified {@code uri} is not an HTTP scheme
*/
static HttpClient of(URI uri, ClientOptionValue<?>... options) {
return of(ClientFactory.DEFAULT, uri, options);
}
代码示例来源:origin: line/armeria
@Override
protected void configure(ServerBuilder sb) {
sb.service("/trailers", (ctx, req) -> {
HttpClient client = HttpClient.of(backendServer.uri("/"));
return client.get("/trailers");
});
sb.service("/trailers-only", (ctx, req) -> {
HttpClient client = HttpClient.of(backendServer.uri("/"));
return client.get("/trailers-only");
});
sb.decorator(LoggingService.newDecorator());
}
};
代码示例来源:origin: line/armeria
@Test
public void composeWithOtherHook() throws Exception {
final AtomicInteger calledFlag = new AtomicInteger();
RxJavaPlugins.setOnSingleAssembly(single -> {
calledFlag.incrementAndGet();
return single;
});
final HttpClient client = HttpClient.of(rule.uri("/"));
client.execute(HttpHeaders.of(HttpMethod.GET, "/single")).aggregate().get();
assertThat(calledFlag.get()).isEqualTo(3);
try {
RequestContextAssembly.enable();
client.execute(HttpHeaders.of(HttpMethod.GET, "/single")).aggregate().get();
assertThat(calledFlag.get()).isEqualTo(6);
} finally {
RequestContextAssembly.disable();
}
client.execute(HttpHeaders.of(HttpMethod.GET, "/single")).aggregate().get();
assertThat(calledFlag.get()).isEqualTo(9);
RxJavaPlugins.setOnSingleAssembly(null);
client.execute(HttpHeaders.of(HttpMethod.GET, "/single")).aggregate().get();
assertThat(calledFlag.get()).isEqualTo(9);
}
内容来源于网络,如有侵权,请联系作者删除!