Search in sources :

Example 1 with Compression

use of cn.taketoday.framework.web.server.Compression in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method setUpFactoryForCompression.

private String setUpFactoryForCompression(int contentSize, String[] mimeTypes, String[] excludedUserAgents) {
    char[] chars = new char[contentSize];
    Arrays.fill(chars, 'F');
    String testContent = new String(chars);
    AbstractServletWebServerFactory factory = getFactory();
    Compression compression = new Compression();
    compression.setEnabled(true);
    if (mimeTypes != null) {
        compression.setMimeTypes(mimeTypes);
    }
    if (excludedUserAgents != null) {
        compression.setExcludedUserAgents(excludedUserAgents);
    }
    factory.setCompression(compression);
    factory.addInitializers(new ServletRegistrationBean<HttpServlet>(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            resp.setContentType("text/plain");
            resp.setContentLength(testContent.length());
            resp.getWriter().write(testContent);
            resp.getWriter().flush();
        }
    }, "/test.txt"));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    return testContent;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Compression(cn.taketoday.framework.web.server.Compression) HttpServlet(jakarta.servlet.http.HttpServlet) HttpServletResponse(jakarta.servlet.http.HttpServletResponse)

Example 2 with Compression

use of cn.taketoday.framework.web.server.Compression in project today-infrastructure by TAKETODAY.

the class AbstractReactiveWebServerFactoryTests method noCompressionForResponseWithInvalidContentType.

@Test
void noCompressionForResponseWithInvalidContentType() {
    Compression compression = new Compression();
    compression.setEnabled(true);
    compression.setMimeTypes(new String[] { "application/json" });
    WebClient client = prepareCompressionTest(compression, "test~plain");
    ResponseEntity<Void> response = client.get().retrieve().toBodilessEntity().block(Duration.ofSeconds(30));
    assertResponseIsNotCompressed(response);
}
Also used : Compression(cn.taketoday.framework.web.server.Compression) WebClient(cn.taketoday.web.reactive.function.client.WebClient) Test(org.junit.jupiter.api.Test)

Example 3 with Compression

use of cn.taketoday.framework.web.server.Compression in project today-infrastructure by TAKETODAY.

the class AbstractReactiveWebServerFactoryTests method prepareCompressionTest.

protected WebClient prepareCompressionTest() {
    Compression compression = new Compression();
    compression.setEnabled(true);
    return prepareCompressionTest(compression);
}
Also used : Compression(cn.taketoday.framework.web.server.Compression)

Example 4 with Compression

use of cn.taketoday.framework.web.server.Compression in project today-infrastructure by TAKETODAY.

the class AbstractReactiveWebServerFactoryTests method noCompressionForSmallResponse.

@Test
void noCompressionForSmallResponse() {
    Compression compression = new Compression();
    compression.setEnabled(true);
    compression.setMinResponseSize(DataSize.ofBytes(3001));
    WebClient client = prepareCompressionTest(compression);
    ResponseEntity<Void> response = client.get().retrieve().toBodilessEntity().block(Duration.ofSeconds(30));
    assertResponseIsNotCompressed(response);
}
Also used : Compression(cn.taketoday.framework.web.server.Compression) WebClient(cn.taketoday.web.reactive.function.client.WebClient) Test(org.junit.jupiter.api.Test)

Example 5 with Compression

use of cn.taketoday.framework.web.server.Compression in project today-framework by TAKETODAY.

the class AbstractReactiveWebServerFactoryTests method noCompressionForMimeType.

@Test
void noCompressionForMimeType() {
    Compression compression = new Compression();
    compression.setEnabled(true);
    compression.setMimeTypes(new String[] { "application/json" });
    WebClient client = prepareCompressionTest(compression);
    ResponseEntity<Void> response = client.get().retrieve().toBodilessEntity().block(Duration.ofSeconds(30));
    assertResponseIsNotCompressed(response);
}
Also used : Compression(cn.taketoday.framework.web.server.Compression) WebClient(cn.taketoday.web.reactive.function.client.WebClient) Test(org.junit.jupiter.api.Test)

Aggregations

Compression (cn.taketoday.framework.web.server.Compression)18 Test (org.junit.jupiter.api.Test)12 WebClient (cn.taketoday.web.reactive.function.client.WebClient)8 ServletRegistrationBean (cn.taketoday.framework.web.servlet.ServletRegistrationBean)2 AbstractServletWebServerFactory (cn.taketoday.framework.web.servlet.server.AbstractServletWebServerFactory)2 HttpComponentsClientHttpRequestFactory (cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory)2 ExampleServlet (cn.taketoday.test.web.servlet.ExampleServlet)2 HttpServlet (jakarta.servlet.http.HttpServlet)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 InputStreamFactory (org.apache.http.client.entity.InputStreamFactory)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2