1515 */
1616package io .fusionauth .http ;
1717
18+ import java .io .ByteArrayInputStream ;
19+ import java .io .ByteArrayOutputStream ;
1820import java .io .IOException ;
1921import java .io .InputStream ;
2022import java .io .OutputStream ;
2123import java .math .BigDecimal ;
2224import java .net .URI ;
2325import java .net .http .HttpRequest ;
26+ import java .net .http .HttpRequest .BodyPublishers ;
2427import java .net .http .HttpResponse .BodySubscribers ;
2528import java .nio .charset .StandardCharsets ;
2629import java .nio .file .Files ;
2730import java .nio .file .Path ;
2831import java .nio .file .Paths ;
2932import java .text .DecimalFormat ;
3033import java .util .List ;
34+ import java .util .zip .DeflaterOutputStream ;
3135import java .util .zip .GZIPInputStream ;
36+ import java .util .zip .GZIPOutputStream ;
3237import java .util .zip .InflaterInputStream ;
3338
3439import io .fusionauth .http .HTTPValues .ContentEncodings ;
3742import io .fusionauth .http .server .HTTPHandler ;
3843import org .testng .annotations .DataProvider ;
3944import org .testng .annotations .Test ;
45+ import org .testng .internal .protocols .Input ;
4046import static org .testng .Assert .assertEquals ;
4147import static org .testng .Assert .assertNull ;
4248import 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