Java Code Examples for com.linecorp.armeria.common.HttpRequest#streaming()

The following examples show how to use com.linecorp.armeria.common.HttpRequest#streaming() . 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: HttpClientIntegrationTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@ValueSource(booleans = { true, false })
void requestAbortWithException(boolean isAbort) {
    final WebClient client = WebClient.of(server.httpUri());
    final HttpRequestWriter request = HttpRequest.streaming(HttpMethod.GET, "/client-aborted");
    final HttpResponse response = client.execute(request);

    final IllegalStateException badState = new IllegalStateException("bad state");
    if (isAbort) {
        request.abort(badState);
    } else {
        request.close(badState);
    }
    assertThatThrownBy(() -> response.aggregate().join())
            .isInstanceOf(CompletionException.class)
            .hasCause(badState);
}
 
Example 2
Source File: HttpServerStreamingTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@ArgumentsSource(ClientProvider.class)
void testTooLargeContent(WebClient client) throws Exception {
    final int maxContentLength = 65536;
    serverMaxRequestLength = maxContentLength;

    final HttpRequestWriter req = HttpRequest.streaming(HttpMethod.POST, "/count");
    final CompletableFuture<AggregatedHttpResponse> f = client.execute(req).aggregate();

    stream(req, maxContentLength + 1, 1024);

    final AggregatedHttpResponse res = f.get();

    assertThat(res.status()).isEqualTo(HttpStatus.REQUEST_ENTITY_TOO_LARGE);
    assertThat(res.contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
    assertThat(res.contentUtf8()).isEqualTo("413 Request Entity Too Large");
}
 
Example 3
Source File: HttpServerStreamingTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static void runStreamingRequestTest(WebClient client, String path)
        throws InterruptedException, ExecutionException {
    final HttpRequestWriter req = HttpRequest.streaming(HttpMethod.POST, path);
    final CompletableFuture<AggregatedHttpResponse> f = client.execute(req).aggregate();

    // Stream a large of the max memory.
    // This test will fail if the implementation keep the whole content in memory.
    final long expectedContentLength = STREAMING_CONTENT_LENGTH;

    try {
        stream(req, expectedContentLength, STREAMING_CONTENT_CHUNK_LENGTH);

        final AggregatedHttpResponse res = f.get();

        assertThat(res.status()).isEqualTo(HttpStatus.OK);
        assertThat(res.contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
        assertThat(res.contentUtf8()).isEqualTo(
                String.valueOf(expectedContentLength));
    } finally {
        // Make sure the stream is closed even when this test fails due to timeout.
        req.close();
    }
}
 
Example 4
Source File: HttpServerTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@ArgumentsSource(ClientAndProtocolProvider.class)
void testStreamRequestLongerThanTimeout(WebClient client) throws Exception {
    // Disable timeouts and length limits so that test does not fail due to slow transfer.
    clientWriteTimeoutMillis = 0;
    clientResponseTimeoutMillis = 0;
    clientMaxResponseLength = 0;
    serverRequestTimeoutMillis = 0;

    final HttpRequestWriter request = HttpRequest.streaming(HttpMethod.POST, "/echo");
    final HttpResponse response = client.execute(request);
    request.write(HttpData.ofUtf8("a"));
    Thread.sleep(2000);
    request.write(HttpData.ofUtf8("b"));
    Thread.sleep(2000);
    request.write(HttpData.ofUtf8("c"));
    Thread.sleep(2000);
    request.write(HttpData.ofUtf8("d"));
    Thread.sleep(2000);
    request.write(HttpData.ofUtf8("e"));
    request.close();
    assertThat(response.aggregate().get().contentUtf8()).isEqualTo("abcde");
}
 
Example 5
Source File: DefaultHttpRequestTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void ignoresAfterTrailersIsWritten() {
    final HttpRequestWriter req = HttpRequest.streaming(HttpMethod.GET, "/foo");
    req.write(HttpData.ofUtf8("foo"));
    req.write(HttpHeaders.of(HttpHeaderNames.of("a"), "b"));
    req.write(HttpHeaders.of(HttpHeaderNames.of("c"), "d")); // Ignored.
    req.close();

    final AggregatedHttpRequest aggregated = req.aggregate().join();
    // Request headers
    assertThat(aggregated.headers()).isEqualTo(
            RequestHeaders.of(HttpMethod.GET, "/foo",
                              HttpHeaderNames.CONTENT_LENGTH, "3"));
    // Content
    assertThat(aggregated.contentUtf8()).isEqualTo("foo");
    // Trailers
    assertThat(aggregated.trailers()).isEqualTo(HttpHeaders.of(HttpHeaderNames.of("a"), "b"));
}
 
Example 6
Source File: ThriftServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void invoke0(THttpService service, HttpData content,
                            CompletableFuture<HttpData> promise) throws Exception {
    final HttpRequestWriter req = HttpRequest.streaming(HttpMethod.POST, "/");
    req.write(content);
    req.close();

    final ServiceRequestContext ctx =
            ServiceRequestContext.builder(req)
                                 .eventLoop(eventLoop.get())
                                 .serverConfigurator(builder -> {
                                     builder.blockingTaskExecutor(ImmediateEventExecutor.INSTANCE, false);
                                     builder.verboseResponses(true);
                                 })
                                 .build();

    final HttpResponse res = service.serve(ctx, req);
    res.aggregate().handle(voidFunction((aReq, cause) -> {
        if (cause == null) {
            if (aReq.status().code() == 200) {
                promise.complete(aReq.content());
            } else {
                promise.completeExceptionally(
                        new AssertionError(aReq.status() + ", " + aReq.contentUtf8()));
            }
        } else {
            promise.completeExceptionally(cause);
        }
    })).exceptionally(CompletionActions::log);
}
 
Example 7
Source File: HttpClientIntegrationTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void testCloseClientFactory() throws Exception {
    final ClientFactory factory = ClientFactory.builder().build();
    final WebClient client = WebClient.builder(server.httpUri()).factory(factory).build();
    final HttpRequestWriter req = HttpRequest.streaming(RequestHeaders.of(HttpMethod.GET,
                                                                          "/stream-closed"));
    final HttpResponse res = client.execute(req);
    final AtomicReference<HttpObject> obj = new AtomicReference<>();
    res.subscribe(new Subscriber<HttpObject>() {
        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(HttpObject httpObject) {
            obj.set(httpObject);
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onComplete() {
        }
    });
    req.write(HttpData.ofUtf8("not finishing this stream, sorry."));
    await().untilAsserted(() -> assertThat(obj).hasValue(ResponseHeaders.of(HttpStatus.OK)));
    factory.close();
    await().until(() -> completed);
}
 
Example 8
Source File: HttpClientIntegrationTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void responseAbortWithException() throws InterruptedException {
    final WebClient client = WebClient.of(server.httpUri());
    final HttpRequest request = HttpRequest.streaming(HttpMethod.GET, "/client-aborted");
    final HttpResponse response = client.execute(request);

    await().until(() -> completed);
    final IllegalStateException badState = new IllegalStateException("bad state");
    response.abort(badState);
    assertThatThrownBy(() -> response.aggregate().join())
            .isInstanceOf(CompletionException.class)
            .hasCause(badState);
}
 
Example 9
Source File: ArmeriaChannel.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public <I, O> ClientCall<I, O> newCall(
        MethodDescriptor<I, O> method, CallOptions callOptions) {
    final HttpRequestWriter req = HttpRequest.streaming(
            RequestHeaders.of(HttpMethod.POST, uri().getPath() + method.getFullMethodName(),
                              HttpHeaderNames.CONTENT_TYPE, serializationFormat.mediaType(),
                              HttpHeaderNames.TE, HttpHeaderValues.TRAILERS));
    final DefaultClientRequestContext ctx = newContext(HttpMethod.POST, req);

    final String fullMethodName = method.getFullMethodName();
    final int methodIndex = fullMethodName.lastIndexOf('/') + 1;
    ctx.logBuilder().name(method.getServiceName(), fullMethodName.substring(methodIndex));
    ctx.logBuilder().serializationFormat(serializationFormat);
    ctx.logBuilder().defer(RequestLogProperty.REQUEST_CONTENT,
                           RequestLogProperty.RESPONSE_CONTENT);

    final ClientOptions options = options();
    final int maxOutboundMessageSizeBytes = options.get(GrpcClientOptions.MAX_OUTBOUND_MESSAGE_SIZE_BYTES);
    final int maxInboundMessageSizeBytes = options.get(GrpcClientOptions.MAX_INBOUND_MESSAGE_SIZE_BYTES);
    final boolean unsafeWrapResponseBuffers = options.get(GrpcClientOptions.UNSAFE_WRAP_RESPONSE_BUFFERS);

    final PooledHttpClient client;

    final CallCredentials credentials = callOptions.getCredentials();
    if (credentials != null) {
        client = new CallCredentialsDecoratingClient(httpClient, credentials, method, authority());
    } else {
        client = httpClient;
    }

    return new ArmeriaClientCall<>(
            ctx,
            params.endpointGroup(),
            client,
            req,
            method,
            maxOutboundMessageSizeBytes,
            maxInboundMessageSizeBytes > 0 ? maxInboundMessageSizeBytes
                                           : Ints.saturatedCast(options.maxResponseLength()),
            callOptions,
            CompressorRegistry.getDefaultInstance(),
            DecompressorRegistry.getDefaultInstance(),
            serializationFormat,
            jsonMarshaller,
            unsafeWrapResponseBuffers,
            advertisedEncodingsHeader);
}
 
Example 10
Source File: CircuitBreakerClientIntegrationTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
void abortOnFailFast() throws Exception {
    final AtomicLong tickerValue = new AtomicLong();
    final CircuitBreaker circuitBreaker = CircuitBreaker.builder()
                                                        .ticker(tickerValue::get)
                                                        .counterUpdateInterval(Duration.ofSeconds(1))
                                                        .minimumRequestThreshold(0)
                                                        .build();

    final WebClient client =
            WebClient.builder()
                     .decorator(CircuitBreakerClient.newDecorator(
                             circuitBreaker,
                             CircuitBreakerRule.onException()))
                     .build();

    for (int i = 0; i < 3; i++) {
        final HttpRequestWriter req = HttpRequest.streaming(HttpMethod.POST, "h2c://127.0.0.1:1");
        final PooledHttpData data = PooledHttpData.wrap(Unpooled.wrappedBuffer(new byte[] { 0 }))
                                                  .withEndOfStream();
        req.write(data);

        switch (i) {
            case 0:
            case 1:
                assertThat(circuitBreaker.canRequest()).isTrue();
                assertThatThrownBy(() -> client.execute(req).aggregate().join())
                        .isInstanceOfSatisfying(CompletionException.class, cause -> {
                            assertThat(cause.getCause()).isInstanceOf(UnprocessedRequestException.class)
                                                        .hasCauseInstanceOf(ConnectException.class);
                        });
                await().untilAsserted(() -> {
                    assertThat(req.whenComplete()).hasFailedWithThrowableThat()
                                                  .isInstanceOf(UnprocessedRequestException.class);
                });
                break;
            default:
                await().until(() -> !circuitBreaker.canRequest());
                assertThatThrownBy(() -> client.execute(req).aggregate().join())
                        .isInstanceOf(CompletionException.class)
                        .hasCauseInstanceOf(FailFastException.class);

                await().untilAsserted(() -> {
                    assertThat(req.whenComplete()).hasFailedWithThrowableThat()
                                                  .isInstanceOf(FailFastException.class);
                });
        }

        assertThat(data.refCnt()).isZero();

        tickerValue.addAndGet(TimeUnit.SECONDS.toNanos(1));
    }
}