@@ -102,13 +102,72 @@ public void testContentEncodedResource() throws IOException, URISyntaxException
102102 generatePreCompressedResource ("gz" );
103103
104104 //assert compressed response that was pre compressed
105- assertResponse (compClient .execute (get ), true , plainResponse , "gz" );
105+ assertResponse (compClient .execute (get ), true , plainResponse , "gz" , "text/html" );
106106
107107 } finally {
108108 client .getConnectionManager ().shutdown ();
109109 }
110110 }
111111
112+ @ Test
113+ public void testContentEncodedJsonResource () throws IOException , URISyntaxException {
114+ HttpGet get = new HttpGet (DefaultServer .getDefaultServerURL () + "/path/data1.json" );
115+ TestHttpClient client = new TestHttpClient ();
116+ Path rootPath = Paths .get (getClass ().getResource ("data1.json" ).toURI ()).getParent ();
117+
118+ try (CloseableHttpClient compClient = HttpClientBuilder .create ().build ()){
119+ DefaultServer .setRootHandler (new CanonicalPathHandler ()
120+ .setNext (new PathHandler ()
121+ .addPrefixPath ("/path" , new ResourceHandler (new PreCompressedResourceSupplier (new PathResourceManager (rootPath , 10485760 )).addEncoding ("gzip" , ".gz" ))
122+ .setDirectoryListingEnabled (true ))));
123+
124+ //assert response without compression
125+ final String plainResponse = assertResponse (client .execute (get ), false , null , "web" , "application/json" );
126+
127+ //assert compressed response, that doesn't exists, so returns plain
128+ assertResponse (compClient .execute (get ), false , plainResponse , "web" , "application/json" );
129+
130+ //generate compressed resource with extension .gz
131+ Path json = rootPath .resolve ("data1.json" );
132+ generateGZipFile (json , rootPath .resolve ("data1.json.gz" ));
133+
134+ //assert compressed response that was pre compressed
135+ assertResponse (compClient .execute (get ), true , plainResponse , "gz" , "application/json" );
136+
137+ } finally {
138+ client .getConnectionManager ().shutdown ();
139+ }
140+ }
141+
142+ @ Test
143+ public void testContentEncodedJsonResourceWithoutUncompressed () throws IOException , URISyntaxException {
144+ HttpGet get = new HttpGet (DefaultServer .getDefaultServerURL () + "/path/data3.json" );
145+ TestHttpClient client = new TestHttpClient ();
146+ Path rootPath = Paths .get (getClass ().getResource ("data2.json" ).toURI ()).getParent ();
147+
148+ try (CloseableHttpClient compClient = HttpClientBuilder .create ().build ()){
149+ DefaultServer .setRootHandler (new CanonicalPathHandler ()
150+ .setNext (new PathHandler ()
151+ .addPrefixPath ("/path" , new ResourceHandler (new PreCompressedResourceSupplier (new PathResourceManager (rootPath , 10485760 )).addEncoding ("gzip" , ".gz" ))
152+ .setDirectoryListingEnabled (true ))));
153+
154+ //generate compressed resource with extension .gz and delete the uncompressed
155+ Path json = rootPath .resolve ("data2.json" );
156+ Path jsonFileToBeZippedAndDeleted = rootPath .resolve ("data3.json" );
157+ Files .copy (json , jsonFileToBeZippedAndDeleted );
158+ // data3.json.gz has no corresponding data3.json in the filesystem (UNDERTOW-1950)
159+ generateGZipFile (jsonFileToBeZippedAndDeleted , rootPath .resolve ("data3.json.gz" ));
160+ Files .delete (jsonFileToBeZippedAndDeleted );
161+
162+ //assert compressed response even with missing uncompressed
163+ assertResponse (compClient .execute (get ), true , null , "gz" , "application/json" );
164+
165+ } finally {
166+ client .getConnectionManager ().shutdown ();
167+ }
168+ }
169+
170+
112171 @ Test
113172 public void testCorrectResourceSelected () throws IOException , URISyntaxException {
114173 HttpGet get = new HttpGet (DefaultServer .getDefaultServerURL () + "/path/page.html" );
@@ -136,7 +195,7 @@ public void testCorrectResourceSelected() throws IOException, URISyntaxException
136195 generatePreCompressedResource ("gzip.nonsense" );
137196
138197 //assert compressed response that was pre compressed
139- assertResponse (compClient .execute (get ), true , plainResponse , "gzip" );
198+ assertResponse (compClient .execute (get ), true , plainResponse , "gzip" , "text/html" );
140199
141200 } finally {
142201 client .getConnectionManager ().shutdown ();
@@ -166,21 +225,21 @@ private void replaceStringInFile(Path file, String original, String replacement)
166225 }
167226
168227 private String assertResponse (HttpResponse response , boolean encoding ) throws IOException {
169- return assertResponse (response , encoding , null , null );
228+ return assertResponse (response , encoding , null , null , "text/html" );
170229 }
171230
172231 private String assertResponse (HttpResponse response , boolean encoding , String compareWith ) throws IOException {
173- return assertResponse (response , encoding , compareWith , "web" );
232+ return assertResponse (response , encoding , compareWith , "web" , "text/html" );
174233 }
175234
176235 /**
177236 * Series of assertions checking response code, headers and response content
178237 */
179- private String assertResponse (HttpResponse response , boolean encoding , String compareWith , String extension ) throws IOException {
238+ private String assertResponse (HttpResponse response , boolean encoding , String compareWith , String extension , String contentType ) throws IOException {
180239 Assert .assertEquals (StatusCodes .OK , response .getStatusLine ().getStatusCode ());
181240 String body = HttpClientUtils .readResponse (response );
182241 Header [] headers = response .getHeaders (Headers .CONTENT_TYPE_STRING );
183- Assert .assertEquals ("text/html" , headers [0 ].getValue ());
242+ Assert .assertEquals (contentType , headers [0 ].getValue ());
184243
185244 if (encoding ) {
186245 assert response .getEntity () instanceof DecompressingEntity ; //no other nice way to be sure we get back gzipped content
0 commit comments