From 7d629bf994957d7fcfd5574e603c5f5af9b81e46 Mon Sep 17 00:00:00 2001 From: jrhee17 Date: Wed, 11 Dec 2024 17:59:59 +0900 Subject: [PATCH] asf --- .../HttpServerTooLargeContent2Test.java | 93 +++++++++++++++++++ .../src/main/resources/logback-test.xml | 4 +- 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 core/src/test/java/com/linecorp/armeria/server/HttpServerTooLargeContent2Test.java diff --git a/core/src/test/java/com/linecorp/armeria/server/HttpServerTooLargeContent2Test.java b/core/src/test/java/com/linecorp/armeria/server/HttpServerTooLargeContent2Test.java new file mode 100644 index 00000000000..0059cf1df1c --- /dev/null +++ b/core/src/test/java/com/linecorp/armeria/server/HttpServerTooLargeContent2Test.java @@ -0,0 +1,93 @@ +/* + * Copyright 2021 LINE Corporation + * + * LINE Corporation licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package com.linecorp.armeria.server; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +import com.linecorp.armeria.client.ClientFactory; +import com.linecorp.armeria.client.WebClient; +import com.linecorp.armeria.common.AggregatedHttpResponse; +import com.linecorp.armeria.common.ClosedSessionException; +import com.linecorp.armeria.common.HttpData; +import com.linecorp.armeria.common.HttpHeaderNames; +import com.linecorp.armeria.common.HttpResponse; +import com.linecorp.armeria.common.HttpResponseWriter; +import com.linecorp.armeria.common.HttpStatus; +import com.linecorp.armeria.common.MediaType; +import com.linecorp.armeria.common.ResponseHeaders; +import com.linecorp.armeria.common.SessionProtocol; +import com.linecorp.armeria.testing.junit5.server.ServerExtension; + +class HttpServerTooLargeContent2Test { + + @RegisterExtension + static ServerExtension server = new ServerExtension() { + @Override + protected void configure(ServerBuilder sb) { + sb.tlsSelfSigned(); + sb.https(0); + sb.http(0); + sb.maxRequestLength(5); + sb.decorator((delegate, ctx, req) -> { + if (req.headers().contentLength() > 5) { + return HttpResponse.of(HttpStatus.OK, + MediaType.PLAIN_TEXT_UTF_8, "custom response"); + } + return delegate.serve(ctx, req); + }); + sb.service("/", (ctx, req) -> { + final HttpResponseWriter streaming = HttpResponse.streaming(); + streaming.write(ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_LENGTH, 7)); + req.aggregate().handle((aggregatedHttpRequest, cause) -> { + if (cause != null) { + streaming.close(cause); + } else { + streaming.write(HttpData.ofUtf8("content")); + streaming.close(); + } + return null; + }); + return streaming; + }); + } + }; + + @ParameterizedTest + @EnumSource(value = SessionProtocol.class, names = { "H1", "H1C" }) + void closedSessionForHttp1_contentTooLargeAfterResponseHeadersSent(SessionProtocol sessionProtocol) { + final WebClient client = WebClient.builder(server.uri(sessionProtocol)) + .factory(ClientFactory.insecure()) + .build(); + assertThatThrownBy(() -> client.post("/", "abcdefgh").aggregate().join()).hasCauseInstanceOf( + ClosedSessionException.class); + } + + @ParameterizedTest + @EnumSource(value = SessionProtocol.class, names = { "H2C" }) + void closedStreamForHttp2_contentTooLargeAfterResponseHeadersSent(SessionProtocol sessionProtocol) { + final WebClient client = WebClient.builder(server.uri(sessionProtocol)) + .factory(ClientFactory.insecure()) + .build(); + final AggregatedHttpResponse res = client.post("/", "abcdefgh").aggregate().join(); + assertThat(res.status().code()).isEqualTo(HttpStatus.OK.code()); + assertThat(res.contentUtf8()).isEqualTo("custom response"); + } +} diff --git a/testing-internal/src/main/resources/logback-test.xml b/testing-internal/src/main/resources/logback-test.xml index f1bd6c984bc..b7d222057bc 100644 --- a/testing-internal/src/main/resources/logback-test.xml +++ b/testing-internal/src/main/resources/logback-test.xml @@ -30,9 +30,9 @@ - + - +