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

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

Introduction

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

Prototype

MockHttpServletResponse

Source Link

Usage

From source file:org.openmrs.module.clinicalsummary.web.service.PatientSummaryControllerTest.java

/**
 * @verifies return summary data for patient and summary
 * @see org.openmrs.module.clinicalsummary.web.controller.service.PatientSummaryController#searchSummary(String, String, String, Integer, javax.servlet.http.HttpServletResponse)
 *///from   ww  w.  j a v  a 2  s . c  o m
@Test
public void searchSummary_shouldReturnSummaryDataForPatientAndSummary() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/module/clinicalsummary/service/patient/summary");
    request.setParameter("username", "admin");
    request.setParameter("password", "test");
    request.setParameter("patientId", "7");
    request.setParameter("summaryId", "3");

    MockHttpServletResponse response = new MockHttpServletResponse();
    HandlerAdapter handlerAdapter = new AnnotationMethodHandlerAdapter();
    handlerAdapter.handle(request, response, controller);

    Assert.assertFalse(StringUtils.isNotEmpty(response.getContentAsString()));
}

From source file:org.openmrs.module.feedback.web.SeverityFormControllerTest.java

@Before
public void setUp() throws Exception {

    /* executed before the test is run */
    this.service = Context.getService(FeedbackService.class);
    this.controller = new SeverityFormController();
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();

    /* this file is in the same folder of test resources where the hibernate mapping file is located */
    initializeInMemoryDatabase();//from   w  w  w.  j  a v  a2 s. c  om
    executeDataSet("SeverityDataset.xml");

    /* Sample data is loaded into the system */
    authenticate();
}

From source file:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java

@Test
public void test_bad_symbols_in_range() throws Exception {
    final MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    httpServletRequest.setMethod("GET");
    httpServletRequest.setPathInfo("/input.dat");
    httpServletRequest.addHeader(HttpHeaders.RANGE, "bytes=bad");

    final MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
    HttpServletRangeUtil.processRequest(httpServletRequest, mockHttpServletResponse, "input.dat",
            "application/pdf", INPUT_FILE, false);

    assertEquals("", mockHttpServletResponse.getContentAsString());

    assertEquals(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE, mockHttpServletResponse.getStatus());
}

From source file:io.jmnarloch.spring.request.correlation.filter.RequestCorrelationFilterTest.java

@Test
public void shouldInitiateCorrelationId() throws IOException, ServletException {

    // given//from  w w  w. ja va2  s  . c  o  m
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockFilterChain chain = new MockFilterChain();

    // when
    instance.doFilter(request, response, chain);

    // then
    assertNotNull(request.getAttribute(RequestCorrelationConsts.ATTRIBUTE_NAME));
    assertNotNull(((HttpServletRequest) chain.getRequest()).getHeader(RequestCorrelationConsts.HEADER_NAME));
}

From source file:org.jasig.cas.support.oauth.web.OAuth20RevokeClientPrincipalTokensControllerTests.java

@Test
public void verifyNoTokenOrAuthHeader() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST",
            CONTEXT + OAuthConstants.REVOKE_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertNull(modelAndView);/*from   w  w  w .  j  a  v a  2  s .  c  o m*/
    assertEquals(HttpStatus.SC_BAD_REQUEST, mockResponse.getStatus());
    assertEquals(CONTENT_TYPE, mockResponse.getContentType());

    final String expected = "{\"error\":\"" + OAuthConstants.INVALID_REQUEST + "\",\"error_description\":\""
            + OAuthConstants.MISSING_ACCESS_TOKEN_DESCRIPTION + "\"}";

    final ObjectMapper mapper = new ObjectMapper();
    final JsonNode expectedObj = mapper.readTree(expected);
    final JsonNode receivedObj = mapper.readTree(mockResponse.getContentAsString());
    assertEquals(expectedObj.get("error").asText(), receivedObj.get("error").asText());
    assertEquals(expectedObj.get("error_description").asText(), receivedObj.get("error_description").asText());
}

From source file:org.parancoe.web.test.junit4.AbstractControllerTest.java

/**
 * Reset the request and the response, maintaining the same session. Useful, for example, to
 * call a post after calling the get of the form.
 *//*ww w. j av a2s .  c o m*/
protected void resetRequestAndResponse() {
    HttpSession httpSession = null;
    // prepare the multipart request
    if (multipartRequest != null) {
        httpSession = multipartRequest.getSession();
    }
    multipartRequest = new MockMultipartHttpServletRequest();
    multipartRequest.setSession(httpSession);
    multipartRequest.setMethod("GET");
    // preparing the normal request
    if (request != null) {
        httpSession = request.getSession();
    }
    request = new MockHttpServletRequest();
    request.setSession(httpSession);
    request.setMethod("GET");

    response = new MockHttpServletResponse();
}

From source file:org.openmrs.web.controller.encounter.LocationFormControllerTest.java

/**
 * @see LocationFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)
 *///from w  w  w .j  ava 2s . com
@Test
@Verifies(value = "should not retire location if reason is empty", method = "onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)")
public void onSubmit_shouldNotRetireLocationIfReasonIsEmpty() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
    request.setParameter("locationId", "1");
    request.setParameter("retireReason", "");
    request.setParameter("retired", "true");
    request.setParameter("retireLocation", "true");

    HttpServletResponse response = new MockHttpServletResponse();

    LocationFormController controller = getLocationFormController();

    ModelAndView modelAndView = controller.handleRequest(request, response);

    // make sure an error is returned because of the empty retire reason
    BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel()
            .get("org.springframework.validation.BindingResult.location");
    Assert.assertTrue(bindingResult.hasFieldErrors("retireReason"));
}

From source file:org.terasoluna.gfw.web.exception.SystemExceptionResolverTest.java

/**
 * setup all test case.//w  ww. j  a  v a  2 s  .  c  o  m
 */
@Before
public void setup() {

    // create test target.
    this.testTarget = new SystemExceptionResolver();

    this.mockRequest = new MockHttpServletRequest();
    this.mockResponse = new MockHttpServletResponse();
}

From source file:no.dusken.aranea.control.TestImageController.java

@Test
public void testGetImage() throws Exception {
    //creating image object.
    Image image = new Image();
    image.setID(123L);/*  w  w  w. j a  v  a  2  s .c o  m*/
    //mocking imageSernice.getEntity method to return image object I just created.
    when(imageService.getEntity(123L)).thenReturn(image);

    //mocking imageFile.
    File imageFile = mock(File.class);
    //mocking imageFile.exists method to return true.
    when(imageFile.exists()).thenReturn(true);

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.addParameter("ID", "123");

    //mocking imageUtils.getImageFile(Image)
    when(imageUtils.getImageFile(image)).thenReturn(imageFile);

    ModelAndView mav = imgController.handleRequestInternal(request, response);

    assertEquals("Did not return correct file.", mav.getModel().get("file"), imageFile);
}

From source file:org.craftercms.security.processors.impl.AddSecurityCookiesProcessorTest.java

@Test
public void testAddCookiesLoggedIn() throws Exception {
    String ticket = new ObjectId().toString();
    Date lastModified = new Date();

    Profile profile = new Profile();
    profile.setLastModified(lastModified);

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);
    RequestSecurityProcessor flushResponseProcessor = new RequestSecurityProcessor() {

        @Override/*from   w  ww  . ja va  2s. co  m*/
        public void processRequest(RequestContext context, RequestSecurityProcessorChain processorChain)
                throws Exception {
            context.getResponse().getOutputStream().flush();
        }

    };

    RequestSecurityProcessorChain chain = new RequestSecurityProcessorChainImpl(
            Arrays.asList(processor, flushResponseProcessor).iterator());

    Authentication auth = new DefaultAuthentication(ticket, profile);
    SecurityUtils.setAuthentication(request, auth);

    processor.processRequest(context, chain);

    Cookie ticketCookie = response.getCookie(SecurityUtils.TICKET_COOKIE_NAME);

    assertNotNull(ticketCookie);
    assertEquals(ticket, ticketCookie.getValue());

    Cookie profileLastModifiedCookie = response.getCookie(SecurityUtils.PROFILE_LAST_MODIFIED_COOKIE_NAME);

    assertNotNull(profileLastModifiedCookie);
    assertEquals(profile.getLastModified().getTime(), Long.parseLong(profileLastModifiedCookie.getValue()));
}