-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathIssue1656.java
More file actions
67 lines (61 loc) · 2.21 KB
/
Copy pathIssue1656.java
File metadata and controls
67 lines (61 loc) · 2.21 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.test;
import static java.util.stream.LongStream.range;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.zip.GZIPInputStream;
import io.jooby.ServerOptions;
import io.jooby.junit.ServerTest;
import io.jooby.junit.ServerTestRunner;
import okhttp3.ResponseBody;
public class Issue1656 {
@ServerTest
public void gzip(ServerTestRunner runner) {
runner
.options(new ServerOptions().setCompressionLevel(ServerOptions.DEFAULT_COMPRESSION_LEVEL))
.define(
app -> {
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();
}
}