Search in sources :

Example 1 with GZIPFilter

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();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) InputStream(java.io.InputStream) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GlobalProperty(org.openmrs.GlobalProperty) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) GZIPFilter(org.openmrs.web.filter.GZIPFilter) GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedReader(java.io.BufferedReader) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) BaseWebContextSensitiveTest(org.openmrs.web.test.BaseWebContextSensitiveTest)

Aggregations

BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 FilterChain (javax.servlet.FilterChain)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Test (org.junit.Test)1 GlobalProperty (org.openmrs.GlobalProperty)1 GZIPFilter (org.openmrs.web.filter.GZIPFilter)1 BaseWebContextSensitiveTest (org.openmrs.web.test.BaseWebContextSensitiveTest)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1