use of org.openmrs.web.filter.GZIPFilter in project openmrs-core by openmrs.
the class GZIPFilterTest method zipRequestWrapperTest_shouldReturnTrueIfUnzippedContentReadFromWrapperIsTheSameAsContentBeforeZipping.
/**
* @see org.openmrs.web.filter.GZIPFilter#doFilterInternal(HttpServletRequest,HttpServletResponse, javax.servlet.FilterChain)
*/
@Test
public void zipRequestWrapperTest_shouldReturnTrueIfUnzippedContentReadFromWrapperIsTheSameAsContentBeforeZipping() throws Exception {
GlobalProperty property = new GlobalProperty("gzip.acceptCompressedRequestsForPaths", ".*");
Context.getAdministrationService().saveGlobalProperty(property);
MockHttpServletRequest req = new MockHttpServletRequest();
req.setContextPath("http://gzipservletpath");
req.addHeader("Content-encoding", "gzip");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
GZIPOutputStream gzOutput = new GZIPOutputStream(stream);
PrintWriter pwriter = new PrintWriter(gzOutput);
pwriter.write("message string");
pwriter.flush();
gzOutput.finish();
req.setContent(stream.toByteArray());
MockHttpServletResponse resp = new MockHttpServletResponse();
FilterChain fil = mock(FilterChain.class);
GZIPFilter gzipFilter = new GZIPFilter();
gzipFilter.doFilterInternal(req, resp, fil);
final ArgumentCaptor<HttpServletRequest> argumentCaptor = ArgumentCaptor.forClass(HttpServletRequest.class);
Mockito.verify(fil).doFilter(argumentCaptor.capture(), Mockito.any(HttpServletResponse.class));
HttpServletRequest requestArgument = argumentCaptor.getValue();
try {
InputStream iStream = requestArgument.getInputStream();
InputStreamReader iReader = new InputStreamReader(iStream);
BufferedReader bufReader = new BufferedReader(iReader);
String outputMessage = bufReader.readLine();
Assert.assertThat(outputMessage, is("message string"));
} catch (IOException e) {
throw new RuntimeException();
}
}
Aggregations