forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssue1656.java
More file actions
49 lines (43 loc) · 1.57 KB
/
Copy pathIssue1656.java
File metadata and controls
49 lines (43 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package io.jooby;
import io.jooby.junit.ServerTest;
import io.jooby.junit.ServerTestRunner;
import okhttp3.ResponseBody;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.zip.GZIPInputStream;
import static java.util.stream.LongStream.range;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class Issue1656 {
@ServerTest
public void gzip(ServerTestRunner runner) {
runner.define(app -> {
app.setServerOptions(new ServerOptions().setGzip(true));
app.assets("/static/*", "/files");
}).ready(client -> {
client.get("/static/fileupload.js").prepare(req -> {
req.addHeader("Accept-Encoding", "gzip");
}).execute(rsp -> {
ResponseBody body = rsp.body();
long len = body.contentLength();
if (len == -1) {
assertEquals("chunked", rsp.header("Transfer-Encoding"));
} else {
assertTrue(range(63, 66).anyMatch(value -> value == rsp.body().contentLength()));
}
assertEquals("gzip", rsp.header("content-encoding"));
assertEquals("(function () { console.log('ready');})();", ungzip(body.bytes()).trim());
});
});
}
private String ungzip(byte[] buff) throws IOException {
GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(buff));
Scanner scanner = new Scanner(gzip);
StringBuilder str = new StringBuilder();
while (scanner.hasNext()) {
str.append(scanner.nextLine());
}
return str.toString();
}
}