Skip to content

Commit 4a8d8b3

Browse files
committed
working
1 parent ced766d commit 4a8d8b3

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

java-http.ipr

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,31 +1409,6 @@
14091409
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="Java 21" project-jdk-type="JavaSDK">
14101410
<output url="file://$PROJECT_DIR$/out" />
14111411
</component>
1412-
<component name="ProjectRunConfigurationManager">
1413-
<configuration default="true" type="TestNG">
1414-
<shortenClasspath name="NONE" />
1415-
<useClassPathOnly />
1416-
<option name="SUITE_NAME" value="" />
1417-
<option name="PACKAGE_NAME" value="" />
1418-
<option name="MAIN_CLASS_NAME" value="" />
1419-
<option name="GROUP_NAME" value="" />
1420-
<option name="TEST_OBJECT" value="CLASS" />
1421-
<option name="VM_PARAMETERS" value="-ea --add-exports java.base/sun.security.x509=ALL-UNNAMED --add-exports java.base/sun.security.util=ALL-UNNAMED --add-opens java.base/java.net=ALL-UNNAMED" />
1422-
<option name="PARAMETERS" value="" />
1423-
<option name="OUTPUT_DIRECTORY" value="" />
1424-
<option name="TEST_SEARCH_SCOPE">
1425-
<value defaultName="moduleWithDependencies" />
1426-
</option>
1427-
<option name="PROPERTIES_FILE" value="" />
1428-
<properties />
1429-
<listeners>
1430-
<listener class="io.fusionauth.http.BaseTest$TestListener" />
1431-
</listeners>
1432-
<method v="2">
1433-
<option name="Make" enabled="true" />
1434-
</method>
1435-
</configuration>
1436-
</component>
14371412
<component name="VcsDirectoryMappings">
14381413
<mapping directory="$PROJECT_DIR$" vcs="Git" />
14391414
</component>
@@ -1450,4 +1425,4 @@
14501425
</SOURCES>
14511426
</library>
14521427
</component>
1453-
</project>
1428+
</project>

src/test/java/io/fusionauth/http/CompressionTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,25 @@
1515
*/
1616
package io.fusionauth.http;
1717

18+
import java.io.ByteArrayInputStream;
19+
import java.io.ByteArrayOutputStream;
1820
import java.io.IOException;
1921
import java.io.InputStream;
2022
import java.io.OutputStream;
2123
import java.math.BigDecimal;
2224
import java.net.URI;
2325
import java.net.http.HttpRequest;
26+
import java.net.http.HttpRequest.BodyPublishers;
2427
import java.net.http.HttpResponse.BodySubscribers;
2528
import java.nio.charset.StandardCharsets;
2629
import java.nio.file.Files;
2730
import java.nio.file.Path;
2831
import java.nio.file.Paths;
2932
import java.text.DecimalFormat;
3033
import java.util.List;
34+
import java.util.zip.DeflaterOutputStream;
3135
import java.util.zip.GZIPInputStream;
36+
import java.util.zip.GZIPOutputStream;
3237
import java.util.zip.InflaterInputStream;
3338

3439
import io.fusionauth.http.HTTPValues.ContentEncodings;
@@ -37,6 +42,7 @@
3742
import io.fusionauth.http.server.HTTPHandler;
3843
import org.testng.annotations.DataProvider;
3944
import org.testng.annotations.Test;
45+
import org.testng.internal.protocols.Input;
4046
import static org.testng.Assert.assertEquals;
4147
import static org.testng.Assert.assertNull;
4248
import static org.testng.AssertJUnit.fail;
@@ -62,6 +68,11 @@ public Object[][] chunkedSchemes() {
6268
@Test(dataProvider = "compressedChunkedSchemes")
6369
public void compress(String encoding, boolean chunked, String scheme) throws Exception {
6470
HTTPHandler handler = (req, res) -> {
71+
72+
// Ensure we can read the body regardless of the Content-Encoding
73+
String body = new String(req.getInputStream().readAllBytes());
74+
assertEquals(body, "Hello world!");
75+
6576
// Testing an indecisive user, can't make up their mind... this is allowed as long as you have not written ay bytes.
6677
res.setCompress(true);
6778
res.setCompress(false);
@@ -94,11 +105,25 @@ public void compress(String encoding, boolean chunked, String scheme) throws Exc
94105
}
95106
};
96107

108+
ByteArrayOutputStream baseOutputStream = new ByteArrayOutputStream();
109+
DeflaterOutputStream out = encoding.equals(ContentEncodings.Deflate)
110+
? new DeflaterOutputStream(baseOutputStream)
111+
: new GZIPOutputStream(baseOutputStream);
112+
out.write("Hello world!".getBytes(StandardCharsets.UTF_8));
113+
out.finish();
114+
byte[] payload = baseOutputStream.toByteArray();
115+
97116
CountingInstrumenter instrumenter = new CountingInstrumenter();
98117
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler, instrumenter).start()) {
99118
URI uri = makeURI(scheme, "");
100119
var response = client.send(
101-
HttpRequest.newBuilder().header(Headers.AcceptEncoding, encoding).uri(uri).GET().build(),
120+
HttpRequest.newBuilder()
121+
// Request the response be compressed using the provided encodings
122+
.header(Headers.AcceptEncoding, encoding)
123+
// Send the request using the same encoding
124+
.header(Headers.ContentEncoding, encoding)
125+
.POST(BodyPublishers.ofInputStream(() -> new ByteArrayInputStream(payload)))
126+
.uri(uri).GET().build(),
102127
r -> BodySubscribers.ofInputStream()
103128
);
104129

0 commit comments

Comments
 (0)