package io.quarkus.resteasy.test;

import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class ContentLengthTestCase {

    @RegisterExtension
    static QuarkusUnitTest runner = new QuarkusUnitTest()
            .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
                    .addClasses(ContentLengthResource.class))
            .overrideConfigKey("quarkus.resteasy.vertx.response-buffer-size", "100");

    @Test
    public void testContentLengthSet() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10000; ++i) {
            sb.append("Hello World");
        }
        RestAssured.given().body(sb.toString()).post("/length/cl").then().header("Content-Length",
                Matchers.equalTo(Integer.toString(sb.length())));
    }

    @Test
    public void testClForSmallResponse() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10; ++i) {
            sb.append("A");
        }
        RestAssured.given().body(sb.toString()).post("/length").then().header("Content-Length",
                Matchers.equalTo(Integer.toString(sb.length())));
    }

    @Test
    public void testResponseBufferSize() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 101; ++i) {
            sb.append("A");
        }
        RestAssured.given().body(sb.toString()).post("/length").then().header("Content-Length", Matchers.nullValue());
    }
}

@@ -919,32 +919,49 @@
 ngth/cl%22).then()
+%0A                
 .header(%22Content
@@ -969,32 +969,16 @@
 Length%22,
-%0A               
  Matcher
@@ -1010,32 +1010,108 @@
 g(sb.length())))
+.and()%0A                .header(%22Transfer-Encoding%22, Matchers.not(%22chunked%22))
 ;%0A    %7D%0A%0A    @Te
@@ -1343,32 +1343,49 @@
 /length%22).then()
+%0A                
 .header(%22Content
@@ -1393,32 +1393,16 @@
 Length%22,
-%0A               
  Matcher
@@ -1442,16 +1442,92 @@
 gth())))
+.and()%0A                .header(%22Transfer-Encoding%22, Matchers.not(%22chunked%22))
 ;%0A    %7D%0A
@@ -1776,16 +1776,33 @@
 ).then()
+%0A                
 .header(
@@ -1840,16 +1840,78 @@
 Value())
+.and()%0A                .header(%22Transfer-Encoding%22, %22chunked%22)
 ;%0A    %7D%0A

Check for Transfer-Encoding in responses with explicit Content-Length header