Skip to content

Commit c04befd

Browse files
committed
add AutomaticDecompressionTest.java
1 parent bfc445a commit c04befd

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package org.asynchttpclient;
2+
3+
import com.aayushatharva.brotli4j.encoder.BrotliOutputStream;
4+
import com.aayushatharva.brotli4j.encoder.Encoder;
5+
import com.sun.net.httpserver.Headers;
6+
import com.sun.net.httpserver.HttpExchange;
7+
import com.sun.net.httpserver.HttpHandler;
8+
import com.sun.net.httpserver.HttpServer;
9+
import java.io.IOException;
10+
import java.io.OutputStream;
11+
import java.net.InetSocketAddress;
12+
import java.nio.charset.StandardCharsets;
13+
import java.util.zip.GZIPOutputStream;
14+
import org.junit.jupiter.api.AfterAll;
15+
import org.junit.jupiter.api.BeforeAll;
16+
import org.junit.jupiter.api.Test;
17+
import com.github.luben.zstd.Zstd;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
public class AutomaticDecompressionTest {
22+
private static final String UNCOMPRESSED_PAYLOAD = "a".repeat(500);
23+
24+
private static HttpServer HTTP_SERVER;
25+
26+
private static AsyncHttpClient createClient() {
27+
AsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder()
28+
.setEnableAutomaticDecompression(true)
29+
.build();
30+
return new DefaultAsyncHttpClient(config);
31+
}
32+
33+
@BeforeAll
34+
static void setupServer() throws Exception {
35+
HTTP_SERVER = HttpServer.create(new InetSocketAddress(0), 0);
36+
37+
HTTP_SERVER.createContext("/br").setHandler(new HttpHandler() {
38+
@Override
39+
public void handle(HttpExchange exchange)
40+
throws IOException {
41+
exchange.getResponseHeaders().set("Content-Encoding", "br");
42+
exchange.sendResponseHeaders(200, 0);
43+
OutputStream out = exchange.getResponseBody();
44+
Encoder.Parameters params = new Encoder.Parameters();
45+
BrotliOutputStream brotliOutputStream = new BrotliOutputStream(out, params);
46+
brotliOutputStream.write(UNCOMPRESSED_PAYLOAD.getBytes(StandardCharsets.UTF_8));
47+
brotliOutputStream.flush();
48+
brotliOutputStream.close();
49+
}
50+
});
51+
52+
HTTP_SERVER.createContext("/zstd").setHandler(new HttpHandler() {
53+
@Override
54+
public void handle(HttpExchange exchange)
55+
throws IOException {
56+
exchange.getResponseHeaders().set("Content-Encoding", "zstd");
57+
byte[] compressedData = new byte[UNCOMPRESSED_PAYLOAD.length()];
58+
long n = Zstd.compress(compressedData, UNCOMPRESSED_PAYLOAD.getBytes(StandardCharsets.UTF_8), 2, true);
59+
exchange.sendResponseHeaders(200, n);
60+
OutputStream out = exchange.getResponseBody();
61+
out.write(compressedData, 0, (int) n);
62+
out.flush();
63+
out.close();
64+
exchange.close();
65+
}
66+
});
67+
68+
HTTP_SERVER.createContext("/gzip").setHandler(new HttpHandler() {
69+
@Override
70+
public void handle(HttpExchange exchange)
71+
throws IOException {
72+
exchange.getResponseHeaders().set("Content-Encoding", "gzip");
73+
exchange.sendResponseHeaders(200, 0);
74+
OutputStream out = exchange.getResponseBody();
75+
try {
76+
GZIPOutputStream gzip = new GZIPOutputStream(out);
77+
gzip.write(UNCOMPRESSED_PAYLOAD.getBytes(StandardCharsets.UTF_8));
78+
gzip.flush();
79+
gzip.close();
80+
} catch (Exception exception) {
81+
exception.printStackTrace();
82+
}
83+
}
84+
});
85+
86+
HTTP_SERVER.start();
87+
}
88+
89+
@AfterAll
90+
static void stopServer() {
91+
if (HTTP_SERVER != null) {
92+
HTTP_SERVER.stop(0);
93+
}
94+
}
95+
96+
@Test
97+
void zstd() throws Throwable {
98+
io.netty.handler.codec.compression.Zstd.ensureAvailability();
99+
try (AsyncHttpClient client = createClient()) {
100+
Request request = new RequestBuilder("GET")
101+
.setUrl("http://localhost:" + HTTP_SERVER.getAddress().getPort() + "/zstd")
102+
.build();
103+
Response response = client.executeRequest(request).get();
104+
assertEquals(200, response.getStatusCode());
105+
assertEquals(UNCOMPRESSED_PAYLOAD, response.getResponseBody());
106+
}
107+
}
108+
109+
@Test
110+
void brotli() throws Throwable {
111+
io.netty.handler.codec.compression.Brotli.ensureAvailability();
112+
try (AsyncHttpClient client = createClient()) {
113+
Request request = new RequestBuilder("GET")
114+
.setUrl("http://localhost:" + HTTP_SERVER.getAddress().getPort() + "/br")
115+
.build();
116+
Response response = client.executeRequest(request).get();
117+
assertEquals(200, response.getStatusCode());
118+
assertEquals(UNCOMPRESSED_PAYLOAD, response.getResponseBody());
119+
}
120+
}
121+
122+
@Test
123+
void gzip() throws Throwable {
124+
try (AsyncHttpClient client = createClient()) {
125+
Request request = new RequestBuilder("GET")
126+
.setUrl("http://localhost:" + HTTP_SERVER.getAddress().getPort() + "/gzip")
127+
.build();
128+
Response response = client.executeRequest(request).get();
129+
assertEquals(200, response.getStatusCode());
130+
assertEquals(UNCOMPRESSED_PAYLOAD, response.getResponseBody());
131+
}
132+
}
133+
134+
135+
}

0 commit comments

Comments
 (0)