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

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

Introduction

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

Prototype

@Override
    public void setContentType(@Nullable String contentType) 

Source Link

Usage

From source file:guru.nidi.ramltester.HighlevelTestBase.java

protected MockHttpServletResponse jsonResponse(int code, String json, String contentType)
        throws UnsupportedEncodingException {
    final MockHttpServletResponse response = new MockHttpServletResponse();
    response.setStatus(code);//from  w  ww .jav  a 2  s .  co  m
    response.setContentType(contentType);
    response.getWriter().print(json);
    return response;
}

From source file:org.jasig.portal.portlet.rendering.PortletRendererImplTest.java

/**
 * No cached data exists, but mock a {@link CacheControl} that will trigger the portletContainer#doServeResource, 
 * capture the output, and give to the portlet cachecontrol service.
 * /*from  w  ww.j  a v a  2  s.  c  om*/
 * @throws PortletException
 * @throws IOException
 * @throws PortletContainerException
 */
@SuppressWarnings("unchecked")
@Test
public void doServeResourceCapture() throws PortletException, IOException, PortletContainerException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setContentType("application/octet-stream");
    CacheControlImpl cacheControl = new CacheControlImpl();
    cacheControl.setUseCachedContent(false);
    cacheControl.setExpirationTime(300);

    setupPortletExecutionMocks(request);

    when(portletCacheControlService.getPortletResourceCacheControl(portletWindowId, request, response))
            .thenReturn(cacheControl);
    when(portletCacheControlService.getCachedPortletResourceOutput(portletWindowId, request)).thenReturn(null);
    when(portletCacheControlService.shouldOutputBeCached(cacheControl)).thenReturn(true);

    portletRenderer.doServeResource(portletWindowId, request, response);

    verify(portletContainer, times(1)).doServeResource(isA(PortletWindow.class),
            isA(PortletHttpServletRequestWrapper.class), isA(PortletHttpServletResponseWrapper.class));

    verify(portletCacheControlService, times(1)).cachePortletResourceOutput(isA(IPortletWindowId.class),
            isA(HttpServletRequest.class), isA(CachedPortletData.class), isA(CacheControl.class));
}

From source file:org.jasig.portal.portlet.rendering.PortletRendererImplTest.java

/**
 * No cached data exists, but mock a {@link CacheControl} with a negative value for expirationtime.
 * Will trigger the portletContainer#doServeResource, 
 * capture the output, and give to the portlet cachecontrol service.
 * /*from  w  w w .  ja  v  a 2  s .c o  m*/
 * negative value for cacheControl expiration time means "cache forever."
 * 
 * @throws PortletException
 * @throws IOException
 * @throws PortletContainerException
 */
@SuppressWarnings("unchecked")
@Test
public void doServeResourceMarkupCaptureNegativeExpirationTime()
        throws PortletException, IOException, PortletContainerException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setContentType("application/octet-stream");
    CacheControlImpl cacheControl = new CacheControlImpl();
    cacheControl.setUseCachedContent(false);
    cacheControl.setExpirationTime(-1);

    setupPortletExecutionMocks(request);

    when(portletCacheControlService.getPortletResourceCacheControl(portletWindowId, request, response))
            .thenReturn(cacheControl);
    when(portletCacheControlService.getCachedPortletRenderOutput(portletWindowId, request)).thenReturn(null);
    when(portletCacheControlService.shouldOutputBeCached(cacheControl)).thenReturn(true);

    portletRenderer.doServeResource(portletWindowId, request, response);

    verify(portletContainer, times(1)).doServeResource(isA(PortletWindow.class),
            isA(PortletHttpServletRequestWrapper.class), isA(PortletHttpServletResponseWrapper.class));

    verify(portletCacheControlService, times(1)).cachePortletResourceOutput(isA(IPortletWindowId.class),
            isA(HttpServletRequest.class), isA(CachedPortletData.class), isA(CacheControl.class));
}