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

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

Introduction

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

Prototype

public int getContentLength() 

Source Link

Usage

From source file:org.kuali.mobility.shared.controllers.FileControllerTest.java

@Test
public void testGetFile() {
    String viewName;//from ww w . j  a  v a 2s .  c  om
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    MockHttpServletResponse response = new MockHttpServletResponse();
    try {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(FILE_NAME);

        MockMultipartFile mockFile = new MockMultipartFile(FORM_FILE_NAME, FILE_NAME, CONTENT_TYPE, in);

        File file = new File(mockFile);

        when(getFileService().findFileById(file.getId())).thenReturn(file);

        getController().getFile(file.getId(), request, response);

        assertTrue("Content type of response is not text/plain.",
                CONTENT_TYPE.equals(response.getContentType()));
        assertTrue("Response content length does not match file length.",
                file.getFileSize() == response.getContentLength());
        String responseText = IOUtils.toString(response.getContentAsByteArray(), CONTENT_ENCODING);

        String fileContent = IOUtils.toString(file.getBytes(), CONTENT_ENCODING);
        assertTrue("Response content does not match file content.", fileContent.equals(responseText));

    } catch (IOException ioe) {
        LOG.error(ioe.getLocalizedMessage(), ioe);
        fail("Could not get file because of an IOException.");
    } catch (Exception e) {
        LOG.error(e.getLocalizedMessage(), e);
        fail("Could not get file because of an Exception.");
    }
}

From source file:org.ppwcode.vernacular.l10n_III.dojo.DojoDjConfigFilterTest.java

private void testDojoDjConfigFilterHelper(String type, String input, String output, String bestLocale)
        throws UnsupportedEncodingException, IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index.html");
    MockHttpServletResponse response = new MockHttpServletResponse();

    Servlet testServlet = new SimpleServlet(type, input);
    PassThroughFilterChain chain = new PassThroughFilterChain(testServlet);

    HttpSession session = request.getSession();
    session.setAttribute(HttpRequestLocaleFilter.ATTRIBUTE_PREFERRED_LOCALE,
            LocaleHelpers.constructLocaleFromString(bestLocale));

    Filter djConfigFilter = new DojoDjConfigFilter();
    djConfigFilter.doFilter(request, response, chain);

    LOG.debug("input\n" + input);
    LOG.debug("output\n" + response.getContentAsString());

    Assert.assertEquals(output, response.getContentAsString());
    Assert.assertEquals(output.length(), response.getContentLength());
}