Example usage for org.springframework.mock.web MockHttpServletResponse getOutputStream

List of usage examples for org.springframework.mock.web MockHttpServletResponse getOutputStream

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletResponse getOutputStream.

Prototype

@Override
    public ServletOutputStream getOutputStream() 

Source Link

Usage

From source file:com.fiveamsolutions.nci.commons.kmplot.KMPlotServiceTest.java

@Test
public void testGeneratePlot() throws Exception {
    KMPlot plot = getPlot();/*from   ww  w. j  av  a2s . co  m*/
    MockHttpServletResponse response = new MockHttpServletResponse();
    plot.writePlotImage(response.getOutputStream());
    assertNotNull(response.getContentAsString());
    assertTrue(StringUtils.isNotBlank(response.getContentAsString()));
}

From source file:it.geosolutions.httpproxy.service.BaseProxyServiceTest.java

/**
 * Test IProxyService execute as HTTP GET
 *///  w  w  w. j a  v a2s  . c om
public void executeGet() {
    try {
        // Generate mocked request and response
        MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", "/proxy/");
        mockRequest.addParameter("url", testUrl);
        MockHttpServletResponse mockResponse = new MockHttpServletResponse();

        // Call proxy execute
        proxy.execute(mockRequest, mockResponse);

        // Assert the response
        assertNotNull(mockResponse);
        assertEquals(mockResponse.getStatus(), HttpStatus.SC_OK);
        assertNotNull(mockResponse.getOutputStream());
        assertNotNull(mockResponse.getContentType());
        assertTrue(mockResponse.getContentType().contains("text/xml"));

        LOGGER.info("Success proxy GET in '" + testUrl + "'");
        LOGGER.info("************************ Response ************************");
        LOGGER.info(mockResponse.getContentAsString());
        LOGGER.info("********************** EoF Response **********************");

    } catch (Exception e) {
        fail("Exception executing proxy");
    }
}

From source file:it.geosolutions.httpproxy.service.ProxyServiceDefaultTest.java

/**
 * Test IProxyService execute as HTTP GET
 *///from   w  w  w.j av a 2  s  . c o  m
@Test
public void testExecuteGet() {
    try {
        // Generate mocked request and response
        MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", "/proxy/");
        mockRequest.addParameter("url", TEST_URL);
        MockHttpServletResponse mockResponse = new MockHttpServletResponse();

        // Call proxy execute
        proxy.execute(mockRequest, mockResponse);

        // Assert the response
        assertNotNull(mockResponse);
        assertEquals(mockResponse.getStatus(), HttpStatus.SC_OK);
        assertNotNull(mockResponse.getOutputStream());
        assertNotNull(mockResponse.getContentType());
        assertTrue(mockResponse.getContentType().contains("application/vnd.ogc.wms_xml"));

        LOGGER.info("Success proxy GET in '" + TEST_URL + "'");
        LOGGER.info("************************ Response ************************");
        LOGGER.info(mockResponse.getContentAsString());
        LOGGER.info("********************** EoF Response **********************");

    } catch (Exception e) {
        fail("Exception executing proxy-->\t" + e.getLocalizedMessage());
    }
}

From source file:com.nebhale.cyclinglibrary.web.GzipFilterTest.java

@Test
public void gzipResponse() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Accept-Encoding", "gzip");
    request.setContent("test-request-content".getBytes("UTF-8"));

    MockHttpServletResponse response = new MockHttpServletResponse();

    FilterChain filterChain = mock(FilterChain.class);
    doAnswer(new Answer<Void>() {

        @Override//from w  w  w  .  j  av a 2 s.co m
        public Void answer(InvocationOnMock invocation) throws Throwable {
            HttpServletRequest request = (HttpServletRequest) invocation.getArguments()[0];
            assertEquals("test-request-content", readContent(request.getInputStream()));

            HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1];
            writeContent("test-response-content", response.getOutputStream());
            return null;
        }
    }).when(filterChain).doFilter(any(ServletRequest.class), any(ServletResponse.class));

    this.filter.doFilterInternal(request, response, filterChain);

    assertEquals("test-response-content", gunzipContent(response.getContentAsByteArray()));
}

From source file:org.openmrs.module.atomfeed.AtomFeedUtilTest.java

/**
 * @see AtomFeedUtil#getAtomFeedStream(java.io.OutputStream, java.util.Date)
 * @verifies download full stream with null date
 *///from  w w  w.ja  v a 2  s .  co  m
@Test
public void getAtomFeedStream_shouldDownloadFullStreamWithNullDate() throws Exception {
    MockHttpServletResponse response = new MockHttpServletResponse();
    // if atom feed file exists, just get rid of it 
    File feedFile = AtomFeedUtil.getFeedEntriesFile();
    if (feedFile.exists()) {
        feedFile.delete();
    }
    // write couple of entries to atom feed 
    AtomFeedUtil.writeToFeed("test1", new Encounter());
    AtomFeedUtil.writeToFeed("test2", new Patient());

    AtomFeedUtil.getAtomFeedStream(response.getOutputStream(), null);

    // get response content to use it when asserting
    String responseContent = response.getContentAsString();

    // test if response contains header file content
    String atomHeader = FileUtils.readFileToString(AtomFeedUtil.getFeedHeaderFile());
    // truncate "</feed>" from the atom header string
    if (StringUtils.isNotBlank(atomHeader)) {
        atomHeader = StringUtils.substringBeforeLast(atomHeader, "</feed>");
    }
    Assert.assertTrue(StringUtils.contains(responseContent, atomHeader));

    // test that response content also contains both entries
    Assert.assertTrue(StringUtils.contains(responseContent, "<action>test1</action>"));
    Assert.assertTrue(StringUtils.contains(responseContent, "<action>test2</action>"));

    // test that response content also contains closing tag </feed>
    Assert.assertTrue(StringUtils.endsWith(responseContent, "</feed>"));
}

From source file:org.openmrs.module.atomfeed.AtomFeedUtilTest.java

/**
 * @see AtomFeedUtil#getAtomFeedStream(java.io.OutputStream, java.util.Date)
 * @verifies stream multiline entry/*from w  w w .j  a  v a 2s .c o m*/
 */
@Test
public void getAtomFeedStream_shouldStreamMultiLineEntry() throws Exception {
    MockHttpServletResponse response = new MockHttpServletResponse();
    // if atom feed file exists, just get rid of it 
    File feedFile = AtomFeedUtil.getFeedEntriesFile();
    if (feedFile.exists()) {
        feedFile.delete();
    }
    // do some sleep to have asOfDate parameter for filtering entries
    Calendar asOfDate = Calendar.getInstance();
    Thread.sleep(1500);
    // write couple of entries to atom feed 
    AtomFeedUtil.writeToFeed("test1\ntest2", new Encounter());
    AtomFeedUtil.writeToFeed("test3", new Encounter());
    AtomFeedUtil.writeToFeed("test4", new Patient());

    AtomFeedUtil.getAtomFeedStream(response.getOutputStream(), asOfDate.getTime());

    // get response content to use it when asserting
    String responseContent = response.getContentAsString();

    // test if response contains header file content
    String atomHeader = FileUtils.readFileToString(AtomFeedUtil.getFeedHeaderFile());
    // truncate "</feed>" from the atom header string
    if (StringUtils.isNotBlank(atomHeader)) {
        atomHeader = StringUtils.substringBeforeLast(atomHeader, "</feed>");
    }
    Assert.assertTrue(StringUtils.contains(responseContent, atomHeader));

    // test that response content also contains entries
    Assert.assertTrue(StringUtils.contains(responseContent, "test1\ntest2"));
    Assert.assertTrue(StringUtils.contains(responseContent, "<action>test3</action>"));
    Assert.assertTrue(StringUtils.contains(responseContent, "<action>test4</action>"));

    // test that response content also contains closing tag </feed>
    Assert.assertTrue(StringUtils.endsWith(responseContent, "</feed>"));

}

From source file:org.openmrs.module.atomfeed.AtomFeedUtilTest.java

/**
 * @see AtomFeedUtil#getAtomFeedStream(java.io.OutputStream, java.util.Date)
 * @verifies download partial stream by given date
 *///w w w  .  j  av  a  2 s .com
@Test
public void getAtomFeedStream_shouldDownloadPartialStreamByGivenDate() throws Exception {
    MockHttpServletResponse response = new MockHttpServletResponse();
    // if atom feed file exists, just get rid of it 
    File feedFile = AtomFeedUtil.getFeedEntriesFile();
    if (feedFile.exists()) {
        feedFile.delete();
    }
    // write couple of entries to atom feed 
    AtomFeedUtil.writeToFeed("test1", new Encounter());
    AtomFeedUtil.writeToFeed("test2", new Patient());
    // do some sleep to have asOfDate parameter for filtering entries
    Calendar asOfDate = Calendar.getInstance();
    Thread.sleep(1500);
    // add another entries (ones which will be filtered to stream)
    AtomFeedUtil.writeToFeed("test3", new Encounter());
    AtomFeedUtil.writeToFeed("test4", new Patient());

    AtomFeedUtil.getAtomFeedStream(response.getOutputStream(), asOfDate.getTime());

    // get response content to use it when asserting
    String responseContent = response.getContentAsString();

    // test if response contains header file content
    String atomHeader = FileUtils.readFileToString(AtomFeedUtil.getFeedHeaderFile());
    // truncate "</feed>" from the atom header string
    if (StringUtils.isNotBlank(atomHeader)) {
        atomHeader = StringUtils.substringBeforeLast(atomHeader, "</feed>");
    }
    Assert.assertTrue(StringUtils.contains(responseContent, atomHeader));

    // test that response content also contains both entries, added after sleep
    Assert.assertTrue(StringUtils.contains(responseContent, "<action>test3</action>"));
    Assert.assertTrue(StringUtils.contains(responseContent, "<action>test4</action>"));

    // test that response content also contains both entries, added after sleep
    Assert.assertFalse(StringUtils.contains(responseContent, "<action>test1</action>"));
    Assert.assertFalse(StringUtils.contains(responseContent, "<action>test2</action>"));

    // test that response content also contains closing tag </feed>
    Assert.assertTrue(StringUtils.endsWith(responseContent, "</feed>"));

}