Java Code Examples for com.linecorp.armeria.client.WebClient#of()

The following examples show how to use com.linecorp.armeria.client.WebClient#of() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ObservableResponseConverterFunctionTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void failure() {
    final WebClient client = WebClient.of(rule.httpUri() + "/failure");

    AggregatedHttpResponse res;

    res = client.get("/immediate1").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.BAD_REQUEST);

    res = client.get("/immediate2").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.BAD_REQUEST);

    res = client.get("/defer1").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.BAD_REQUEST);

    res = client.get("/defer2").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.BAD_REQUEST);
}
 
Example 2
Source File: AnnotatedServiceResponseConverterTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void multipleObjectPublisherBasedResponseConverter() throws Exception {
    final WebClient client = WebClient.of(rule.httpUri() + "/publish/multi");

    AggregatedHttpResponse res;

    res = aggregated(client.get("/string"));
    assertThat(res.contentType()).isEqualTo(MediaType.JSON_UTF_8);
    assertThatJson(res.contentUtf8())
            .isArray().ofLength(3)
            .thatContains("a").thatContains("b").thatContains("c");

    res = aggregated(client.get("/jsonNode"));
    assertThat(res.contentType()).isEqualTo(MediaType.JSON_UTF_8);
    assertThatJson(res.contentUtf8())
            .isEqualTo("[{\"a\":\"1\"},{\"b\":\"2\"},{\"c\":\"3\"}]");
}
 
Example 3
Source File: ArmeriaAutoConfigurationTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void testGrpcServiceRegistrationBean() throws Exception {
    final HelloServiceBlockingStub client = Clients.newClient(newUrl("gproto+h2c") + '/',
                                                              HelloServiceBlockingStub.class);
    final HelloRequest request = HelloRequest.newBuilder()
                                             .setName("world")
                                             .build();
    assertThat(client.hello(request).getMessage()).isEqualTo("Hello, world");

    final WebClient webClient = WebClient.of(newUrl("h1c"));
    final HttpResponse response = webClient.get("/internal/docs/specification.json");

    final AggregatedHttpResponse res = response.aggregate().get();
    assertThat(res.status()).isEqualTo(HttpStatus.OK);
    assertThatJson(res.contentUtf8()).node("services[1].name").isStringEqualTo(
            "com.linecorp.armeria.spring.test.grpc.main.HelloService");
    assertThatJson(res.contentUtf8())
            .node("services[1].exampleHttpHeaders[0].x-additional-header").isStringEqualTo("headerVal");
    assertThatJson(res.contentUtf8())
            .node("services[1].methods[0].exampleHttpHeaders[0].x-additional-header")
            .isStringEqualTo("headerVal");
}
 
Example 4
Source File: ArmeriaAutoConfigurationTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void testThriftServiceRegistrationBean() throws Exception {
    final HelloService.Iface client = Clients.newClient(newUrl("tbinary+h1c") + "/thrift",
                                                        HelloService.Iface.class);
    assertThat(client.hello("world")).isEqualTo("hello world");

    final WebClient webClient = WebClient.of(newUrl("h1c"));
    final HttpResponse response = webClient.get("/internal/docs/specification.json");

    final AggregatedHttpResponse res = response.aggregate().get();
    assertThat(res.status()).isEqualTo(HttpStatus.OK);
    assertThatJson(res.contentUtf8()).node("services[2].name").isStringEqualTo(
            "com.linecorp.armeria.spring.test.thrift.main.HelloService");
    assertThatJson(res.contentUtf8())
            .node("services[2].exampleHttpHeaders[0].x-additional-header").isStringEqualTo("headerVal");
    assertThatJson(res.contentUtf8())
            .node("services[0].methods[0].exampleHttpHeaders[0].x-additional-header")
            .isStringEqualTo("headerVal");
}
 
Example 5
Source File: ObservableResponseConverterFunctionTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void maybe() {
    final WebClient client = WebClient.of(server.httpUri() + "/maybe");

    AggregatedHttpResponse res;

    res = client.get("/string").aggregate().join();
    assertThat(res.contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
    assertThat(res.contentUtf8()).isEqualTo("a");

    res = client.get("/json").aggregate().join();
    assertThat(res.contentType()).isEqualTo(MediaType.JSON_UTF_8);
    assertThatJson(res.contentUtf8()).isStringEqualTo("a");

    res = client.get("/empty").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.OK);
    assertThat(res.content().isEmpty()).isTrue();

    res = client.get("/error").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);

    res = client.get("/http-response").aggregate().join();
    assertThat(res.contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
    assertThat(res.contentUtf8()).isEqualTo("a");
}
 
Example 6
Source File: ObservableResponseConverterFunctionTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void failure() {
    final WebClient client = WebClient.of(server.httpUri() + "/failure");

    AggregatedHttpResponse res;

    res = client.get("/immediate1").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.BAD_REQUEST);

    res = client.get("/immediate2").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.BAD_REQUEST);

    res = client.get("/defer1").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.BAD_REQUEST);

    res = client.get("/defer2").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.BAD_REQUEST);
}
 
Example 7
Source File: TokenBucketThrottlingStrategyTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void serve1() throws Exception {
    final WebClient client = WebClient.of(serverRule.httpUri());
    final AggregatedHttpResponse response = client.get("/http-serve").aggregate().get();
    assertThat(response.status()).isEqualTo(HttpStatus.OK);

    assertThat(response.headers().contains(HttpHeaderNames.RETRY_AFTER)).isFalse();
    assertThat(response.headers().contains("RateLimit-Remaining")).isFalse();
    assertThat(response.headers().contains("X-RateLimit-Remaining")).isFalse();
    assertThat(response.headers().contains("X-Rate-Limit-Remaining")).isFalse();
    assertThat(response.headers().contains("RateLimit-Reset")).isFalse();
    assertThat(response.headers().contains("X-RateLimit-Reset")).isFalse();
    assertThat(response.headers().contains("X-Rate-Limit-Reset")).isFalse();
    assertThat(response.headers().contains("RateLimit-Limit")).isFalse();
    assertThat(response.headers().contains("X-RateLimit-Limit")).isFalse();
    assertThat(response.headers().contains("X-Rate-Limit-Limit")).isFalse();
}
 
Example 8
Source File: ThrottlingServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
public void serveWithCustomHeader() throws Exception {
    final WebClient client = WebClient.of(serverRule.httpUri());
    final AggregatedHttpResponse response = client.get("/http-always-custom").aggregate().get();
    assertThat(response.status()).isEqualTo(HttpStatus.OK);
    assertThat(response.headers().get("X-RateLimit-Limit"))
            .isEqualTo("10, 10;window=1;burst=1000, 1000;window=3600");
}
 
Example 9
Source File: HttpServerAutoRedirectTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void redirection() {
    final WebClient client = WebClient.of(server.httpUri());
    AggregatedHttpResponse res;

    res = client.get("/a").aggregate().join();
    assertThat(res.status()).isSameAs(HttpStatus.TEMPORARY_REDIRECT);
    assertThat(res.headers().get(HttpHeaderNames.LOCATION)).isEqualTo("/a/");

    res = client.get("/b").aggregate().join();
    assertThat(res.status()).isSameAs(HttpStatus.TEMPORARY_REDIRECT);
    assertThat(res.headers().get(HttpHeaderNames.LOCATION)).isEqualTo("/b/");

    res = client.get("/c/1").aggregate().join();
    assertThat(res.status()).isSameAs(HttpStatus.TEMPORARY_REDIRECT);
    assertThat(res.headers().get(HttpHeaderNames.LOCATION)).isEqualTo("/c/1/");

    res = client.get("/d").aggregate().join();
    assertThat(res.status()).isSameAs(HttpStatus.OK);

    res = client.get("/e").aggregate().join();
    assertThat(res.status()).isSameAs(HttpStatus.TEMPORARY_REDIRECT);
    assertThat(res.headers().get(HttpHeaderNames.LOCATION)).isEqualTo("/e/");

    res = client.delete("/e").aggregate().join();
    assertThat(res.status()).isSameAs(HttpStatus.NOT_FOUND);

    res = client.get("/f").aggregate().join();
    assertThat(res.status()).isSameAs(HttpStatus.TEMPORARY_REDIRECT);
    assertThat(res.headers().get(HttpHeaderNames.LOCATION)).isEqualTo("/f/");

    res = client.get("/f/").aggregate().join();
    assertThat(res.status()).isSameAs(HttpStatus.ACCEPTED);
}
 
Example 10
Source File: ObservableResponseConverterFunctionTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
public void flowable() {
    final WebClient client = WebClient.of(rule.httpUri() + "/flowable");

    AggregatedHttpResponse res;

    res = client.get("/string").aggregate().join();
    assertThat(res.contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
    assertThat(res.contentUtf8()).isEqualTo("a");

    res = client.get("/json/1").aggregate().join();
    assertThat(res.contentType()).isEqualTo(MediaType.JSON_UTF_8);
    assertThatJson(res.contentUtf8())
            .isArray().ofLength(1).thatContains("a");

    res = client.get("/json/3").aggregate().join();
    assertThat(res.contentType()).isEqualTo(MediaType.JSON_UTF_8);
    assertThatJson(res.contentUtf8())
            .isArray().ofLength(3).thatContains("a").thatContains("b").thatContains("c");

    res = client.get("/error").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);

    res = client.post("/defer-post", "").aggregate().join();
    assertThat(res.contentType()).isEqualTo(MediaType.JSON_UTF_8);
    assertThatJson(res.contentUtf8())
            .isArray().ofLength(3).thatContains("a").thatContains("b").thatContains("c");
}
 
Example 11
Source File: AnnotatedServiceBlockingTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@CsvSource({
        "/myEvenLoop/httpResponse, 0",
        "/myEvenLoop/aggregatedHttpResponse, 0",
        "/myEvenLoop/jsonNode, 0",
        "/myEvenLoop/completionStage, 0"
})
void testOnlyEventLoopWithoutBlockingAnnotation(String path, Integer count) throws Exception {
    final WebClient client = WebClient.of(server.httpUri());

    final RequestHeaders headers = RequestHeaders.of(HttpMethod.GET, path);
    final AggregatedHttpResponse res = client.execute(headers).aggregate().join();
    assertThat(res.status()).isSameAs(HttpStatus.OK);
    assertThat(blockingCount).hasValue(count);
}
 
Example 12
Source File: AnnotatedServiceResponseConverterTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
public void produceTypeAnnotationBasedDefaultResponseConverter() throws Exception {
    final WebClient client = WebClient.of(rule.httpUri() + "/produce");

    AggregatedHttpResponse res;

    res = aggregated(client.get("/string"));
    assertThat(res.contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
    assertThat(res.content().array()).isEqualTo("100".getBytes());

    res = aggregated(client.get("/byteArray"));
    assertThat(res.contentType()).isEqualTo(MediaType.OCTET_STREAM);
    assertThat(res.content().array()).isEqualTo(BYTEARRAY);

    res = aggregated(client.get("/httpData"));
    assertThat(res.contentType()).isEqualTo(MediaType.OCTET_STREAM);
    assertThat(res.content().array()).isEqualTo(BYTEARRAY);

    res = aggregated(client.get("/byteArrayGif"));
    assertThat(res.contentType()).isEqualTo(MediaType.GIF);
    assertThat(res.content().array()).isEqualTo(BYTEARRAY);

    res = aggregated(client.get("/httpDataPng"));
    assertThat(res.contentType()).isEqualTo(MediaType.PNG);
    assertThat(res.content().array()).isEqualTo(BYTEARRAY);

    res = aggregated(client.get("/byteArrayTxt"));
    assertThat(res.contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
    assertThat(res.content().array()).isEqualTo(BYTEARRAY);

    res = aggregated(client.get("/httpDataTxt"));
    assertThat(res.contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
    assertThat(res.content().array()).isEqualTo(BYTEARRAY);

    res = aggregated(client.get("/jsonNode"));
    assertThat(res.contentType()).isEqualTo(MediaType.JSON_UTF_8);
    assertThat(res.content().array()).isEqualTo(mapper.writeValueAsBytes(JSONNODE));
}
 
Example 13
Source File: ObservableResponseConverterFunctionTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void completable() {
    final WebClient client = WebClient.of(server.httpUri() + "/completable");

    AggregatedHttpResponse res;

    res = client.get("/done").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.OK);

    res = client.get("/error").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
 
Example 14
Source File: HttpServerHandlerTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void methodNotAllowed() {
    final WebClient client = WebClient.of(server.httpUri());
    final AggregatedHttpResponse res = client.delete("/hello").aggregate().join();
    assertThat(res.status()).isSameAs(HttpStatus.METHOD_NOT_ALLOWED);
    await().untilAsserted(() -> {
        assertThat(logHolder.get().requestHeaders().path()).isEqualTo("/hello");
    });
    assertThat(logHolder.get().requestCause()).isNull();
}
 
Example 15
Source File: MainTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void beforeClass() throws Exception {
    final AtomicLong sequence = new AtomicLong();

    // The server emits only 5 events here because this test is to show how the events are encoded.
    server = Main.newServer(0, 0,
                            Duration.ofMillis(200), 5, () -> Long.toString(sequence.getAndIncrement()));
    server.start().join();
    client = WebClient.of("http://127.0.0.1:" + server.activeLocalPort());
}
 
Example 16
Source File: ArmeriaAutoConfigurationWithConsumerTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
public void normal() throws Exception {
    final WebClient client = WebClient.of(newUrl("h1c"));

    final HttpResponse response = client.get("/customizer");

    final AggregatedHttpResponse msg = response.aggregate().get();
    assertThat(msg.status()).isEqualTo(HttpStatus.OK);
}
 
Example 17
Source File: GrpcServiceServerTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void unframed_noContentType() throws Exception {
    final WebClient client = WebClient.of(server.httpUri());
    final AggregatedHttpResponse response = client.execute(
            RequestHeaders.of(HttpMethod.POST,
                              UnitTestServiceGrpc.getStaticUnaryCallMethod().getFullMethodName()),
            REQUEST_MESSAGE.toByteArray()).aggregate().get();
    assertThat(response.status()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    assertNoRpcContent();
}
 
Example 18
Source File: AnnotatedServiceRequestLogNameTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@BeforeEach
void setUp() {
    client = WebClient.of(server.httpUri());
}
 
Example 19
Source File: AbstractPooledHttpServiceTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@BeforeEach
void setUp() {
    client = WebClient.of(server.httpUri());
}
 
Example 20
Source File: AnnotatedServiceExceptionHandlerTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
public void testExceptionHandler() throws Exception {
    final WebClient client = WebClient.of(rule.httpUri());

    AggregatedHttpResponse response;

    NoExceptionHandler.counter.set(0);

    response = client.execute(RequestHeaders.of(HttpMethod.GET, "/1/sync")).aggregate().join();
    assertThat(response.status()).isEqualTo(HttpStatus.OK);
    assertThat(response.contentUtf8()).isEqualTo("handler1");

    assertThat(NoExceptionHandler.counter.get()).isEqualTo(1);

    response = client.execute(RequestHeaders.of(HttpMethod.GET, "/1/async")).aggregate().join();
    assertThat(response.status()).isEqualTo(HttpStatus.OK);
    assertThat(response.contentUtf8()).isEqualTo("handler1");

    assertThat(NoExceptionHandler.counter.get()).isEqualTo(2);

    response = client.execute(RequestHeaders.of(HttpMethod.GET, "/1/resp1")).aggregate().join();
    assertThat(response.status()).isEqualTo(HttpStatus.OK);
    assertThat(response.contentUtf8()).isEqualTo("handler1");

    assertThat(NoExceptionHandler.counter.get()).isEqualTo(3);

    response = client.execute(RequestHeaders.of(HttpMethod.GET, "/1/resp2")).aggregate().join();
    assertThat(response.status()).isEqualTo(HttpStatus.OK);
    assertThat(response.contentUtf8()).isEqualTo("handler2");

    assertThat(NoExceptionHandler.counter.get()).isEqualTo(4);

    // By default exception handler.
    response = client.execute(RequestHeaders.of(HttpMethod.GET, "/2/sync")).aggregate().join();
    assertThat(response.status()).isEqualTo(HttpStatus.BAD_REQUEST);

    // The method returns CompletionStage<?>. It throws an exception immediately if it is called.
    response = client.execute(RequestHeaders.of(HttpMethod.GET, "/2/async")).aggregate().join();
    assertThat(response.status()).isEqualTo(HttpStatus.BAD_REQUEST);

    // The method returns CompletionStage<?>. It throws an exception after the request is aggregated.
    response = client.execute(RequestHeaders.of(HttpMethod.GET, "/2/async/aggregation"))
                     .aggregate().join();
    assertThat(response.status()).isEqualTo(HttpStatus.BAD_REQUEST);

    // Timeout because of bad exception handler.
    response = client.execute(RequestHeaders.of(HttpMethod.GET, "/3/bad1")).aggregate().join();
    assertThat(response.status()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);

    // Internal server error would be returned due to invalid response.
    response = client.execute(RequestHeaders.of(HttpMethod.GET, "/3/bad2")).aggregate().join();
    assertThat(response.status()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);

    NoExceptionHandler.counter.set(0);

    response = client.execute(RequestHeaders.of(HttpMethod.GET, "/4/handler3")).aggregate().join();
    assertThat(response.status()).isEqualTo(HttpStatus.OK);
    assertThat(response.contentUtf8()).isEqualTo("handler3");

    assertThat(NoExceptionHandler.counter.get()).isZero();

    // A decorator throws an exception.
    response = client.execute(RequestHeaders.of(HttpMethod.GET, "/5/handler3")).aggregate().join();
    assertThat(response.status()).isEqualTo(HttpStatus.OK);
    assertThat(response.contentUtf8()).isEqualTo("handler3");
}