@Override public HttpServer apply(HttpServer server) { if (!this.compression.getMinResponseSize().isNegative()) { server = server .compress((int) this.compression.getMinResponseSize().toBytes()); } CompressionPredicate mimeTypes = getMimeTypesPredicate( this.compression.getMimeTypes()); CompressionPredicate excludedUserAgents = getExcludedUserAgentsPredicate( this.compression.getExcludedUserAgents()); server = server.compress(mimeTypes.and(excludedUserAgents)); return server; }
HttpServer server = HttpServer.create() .port(0) .compress(true);
HttpServer server = HttpServer.create() .port(0) .compress(4);
HttpServer server = HttpServer.create() .port(0) .compress((req, res) -> true) .wiretap(true);
@Test public void compressionActivatedOnClientAddsHeader() { AtomicReference<String> zip = new AtomicReference<>("fail"); HttpServer server = HttpServer.create() .port(0) .compress(true); DisposableServer runningServer = server.handle((in, out) -> out.sendString(Mono.just("reply"))) .wiretap(true) .bindNow(Duration.ofSeconds(10)); HttpClient client = HttpClient.create() .addressSupplier(() -> address(runningServer)) .wiretap(true) .compress(true); ByteBuf resp = client.headers(h -> zip.set(h.get("accept-encoding"))) .get() .uri("/test") .responseContent() .blockLast(); assertThat(zip.get()).isEqualTo("gzip"); runningServer.dispose(); runningServer.onDispose() .block(); }
@Test public void gettingOptionsDuplicates() { HttpServer server = HttpServer.create() .port(123) .host(("foo")) .compress(true); assertThat(server.tcpConfiguration().configure()) .isNotSameAs(HttpServer.DEFAULT_TCP_SERVER) .isNotSameAs(server.tcpConfiguration().configure()); }
HttpServer server = HttpServer.create() .port(0) .compress((req, res) -> false);
@Test public void compressionServerEnabledClientDisabledIsNone() { HttpServer server = HttpServer.create() .port(0) .compress(true); String serverReply = "reply"; DisposableServer runningServer = server.handle((in, out) -> out.sendString(Mono.just(serverReply))) .wiretap(true) .bindNow(Duration.ofSeconds(10)); HttpClient client = HttpClient.create() .addressSupplier(() -> address(runningServer)) .wiretap(true) .compress(false); Tuple2<String, HttpHeaders> resp = client.get() .uri("/test") .response((res, buf) -> buf.asString() .zipWith(Mono.just(res.responseHeaders()))) .blockFirst(); assertThat(resp.getT2().get("Content-Encoding")).isNull(); assertThat(resp.getT1()).isEqualTo(serverReply); runningServer.dispose(); runningServer.onDispose() .block(); }
HttpServer.create() .port(0) .compress(true) .handle((req, res) -> res.sendWebsocket((in, out) -> out.sendString(Mono.just("test"))))
@Test public void serverCompressionDisabled() { HttpServer server = HttpServer.create() .port(0) .compress(false); DisposableServer runningServer = server.handle((in, out) -> out.sendString(Mono.just("reply"))) .wiretap(true) .bindNow(Duration.ofSeconds(10)); //don't activate compression on the client options to avoid auto-handling (which removes the header) HttpClient client = HttpClient.create() .addressSupplier(() -> address(runningServer)) .wiretap(true); Tuple2<String, HttpHeaders> resp = //edit the header manually to attempt to trigger compression on server side client.headers(h -> h.add("Accept-Encoding", "gzip")) .get() .uri("/test") .response((res, buf) -> buf.asString() .zipWith(Mono.just(res.responseHeaders()))) .blockFirst(); assertThat(resp.getT2().get("content-encoding")).isNull(); Assert.assertEquals("reply", resp.getT1()); runningServer.dispose(); runningServer.onDispose() .block(); }
.compress(true) .port(0) .handle((req, res) -> res.sendString(Mono.just(content)))
@Test public void serverCompressionEnabledSmallResponse() { HttpServer server = HttpServer.create() .port(0) .compress(25); DisposableServer connection = server.handle((in, out) -> out.header("content-length", "5") .sendString(Mono.just("reply"))) .bindNow(); //don't activate compression on the client options to avoid auto-handling (which removes the header) HttpClient client = HttpClient.create() .addressSupplier(() -> address(connection)); Tuple2<String, HttpHeaders> resp = //edit the header manually to attempt to trigger compression on server side client.headers(h -> h.add("Accept-Encoding", "gzip")) .get() .uri("/test") .response((res, byteBufFlux) -> byteBufFlux.asString() .zipWith(Mono.just(res.responseHeaders()))) .blockFirst(); //check the server didn't send the gzip header, only transfer-encoding HttpHeaders headers = resp.getT2(); assertThat(headers.get("conTENT-encoding")).isNull(); //check the server sent plain text String reply = resp.getT1(); Assert.assertEquals("reply", reply); connection.dispose(); connection.onDispose() .block(); }
@Test public void testIssue282() { DisposableServer server = HttpServer.create() .compress(2048) .port(0) .handle((req, res) -> res.sendString(Mono.just("testtesttesttesttest"))) .bindNow(); Mono<String> response = HttpClient.create() .port(server.address().getPort()) .get() .uri("/") .responseContent() .aggregate() .asString(); StepVerifier.create(response) .expectNextMatches(s -> "testtesttesttesttest".equals(s)) .expectComplete() .verify(); server.disposeNow(); }
@Test public void trueEnabledIncludeContentEncoding() { HttpServer server = HttpServer.create() .port(0) .compress(true); DisposableServer runningServer = server.handle((in, out) -> out.sendString(Mono.just("reply"))) .wiretap(true) .bindNow(Duration.ofSeconds(10)); HttpClient client = HttpClient.create() .addressSupplier(() -> address(runningServer)) .wiretap(true) .compress(true); ByteBuf res = client.headers(h -> Assert.assertTrue(h.contains("Accept-Encoding", "gzip", true))) .get() .uri("/test") .responseContent() .blockLast(); runningServer.dispose(); runningServer.onDispose() .block(); }
DisposableServer server = HttpServer.create() .compress(10) .port(0) .wiretap(true)
HttpServer.create() .port(0) .compress(true) .route(r -> r.post("/hi", (req, res) -> req.receive() .aggregate()