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.motechproject.mobile.web.ivr.intellivr.IntellIVRControllerTest.java

@Before
public void setUp() throws JAXBException {
    mockRequest = new MockHttpServletRequest();
    mockResponse = new MockHttpServletResponse();
    mockIVRConfigHandler = createMock(GetIVRConfigRequestHandler.class);
    mockReportHandler = createMock(ReportHandler.class);

    jaxbContext = JAXBContext.newInstance("org.motechproject.mobile.omp.manager.intellivr");
    unmarshaller = jaxbContext.createUnmarshaller();

}

From source file:git.irbis.spring3.controllerusageexample.customers.web.ListCustomersControllerTest.java

/**
 * Test of request: show customers started from lastname, result success.
 *//*  ww  w  . jav  a2 s. c  om*/
@Test
public void testListCustomersByLastNameSuccess() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    ListCustomersController listCustomersController = new ListCustomersController(
            new CustomerRepositoryMockImpl());
    request.setMethod("GET");
    request.setRequestURI("/listcustomers/last");

    try {
        ModelAndView mv = new AnnotationMethodHandlerAdapter().handle(request, response,
                listCustomersController);

        ModelAndViewAssert.assertViewName(mv, "listcustomers");
        List<Customer> customers = (List<Customer>) mv.getModel().get("customers");

        assertNotNull(customers);
        assertEquals(5, customers.size());
    } catch (Exception ex) {
        fail();
    }
}

From source file:nl.lumc.nanopub.store.api.NanopubControllerIntegrationTest.java

@DirtiesContext
@Test//www .j  a va  2s.  co  m
public void testStoreNanopub()
        throws MalformedNanopubException, OpenRDFException, IOException, NanopubDaoException, Exception {

    MockHttpServletRequest request;
    MockHttpServletResponse response;

    String nanopub = getNanopubAsString("example");
    String contentType = "application/x-trig";

    request = new MockHttpServletRequest();
    request.setContentType(contentType);
    response = new MockHttpServletResponse();

    request.setMethod("POST");
    request.setRequestURI("/nanopubs/");
    request.setContent(nanopub.getBytes());
    Object handler;

    handler = handlerMapping.getHandler(request).getHandler();
    handlerAdapter.handle(request, response, handler);

    assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
    assertNotNull(response.getHeaderValue("Location"));
    assertNotNull(response.getHeaderValue("Content-Type"));
}

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

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

    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:ch.silviowangler.dox.web.DocumentControllerTest.java

@Test
public void getTheDocumentContentCausesAccessDenied()
        throws DocumentNotFoundException, DocumentNotInStoreException, UnsupportedEncodingException {

    MockHttpServletResponse response = new MockHttpServletResponse();

    PhysicalDocument physicalDocument = new PhysicalDocument(new DocumentClass("hhh"), "hello".getBytes(), null,
            "hello.txt");
    physicalDocument.setMimeType("aaa/bbb");
    when(documentService.findPhysicalDocument(1L)).thenThrow(new AccessDeniedException("YOLO"));

    controller.getDocument(1L, response);

    assertThat(response.getStatus(), is(SC_FORBIDDEN));
}

From source file:com.tasktop.c2c.server.common.service.tests.ajp.AjpProxyWebTest.java

@Test
public void testProxyHandlesCookies() throws Exception {
    final String ajpBaseUri = String.format("ajp://localhost:%s", container.getAjpPort());
    Payload payload = new Payload(HttpServletResponse.SC_OK, "some content\none two three\n\nfour");
    payload.getResponseHeaders().put("foo", "bar");
    payload.getSessionVariables().put("s1", "v1");
    TestServlet.setResponsePayload(payload);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/test");
    request.setQueryString("a=b");
    request.setParameter("a", new String[] { "b" });
    request.addHeader("c", "d ef");
    MockHttpServletResponse response = new MockHttpServletResponse();
    proxy.proxyRequest(ajpBaseUri + "/foo", request, response);

    Request firstRequest = null;//www  . j  a  v a 2 s.c  o m
    for (int i = 0; i < 100; i++) {
        firstRequest = TestServlet.getLastRequest();

        // If our request is not yet there, then pause and retry shortly - proxying is an async process, and this
        // request was sometimes coming back as null which was causing test failures on the first assert below.
        if (firstRequest == null) {
            Thread.sleep(10);
        } else {
            // Got our request, so break now.
            break;
        }
    }

    Assert.assertTrue(firstRequest.isNewSession());
    Assert.assertEquals("v1", firstRequest.getSessionAttributes().get("s1"));

    List<org.apache.commons.httpclient.Cookie> cookies = new ArrayList<org.apache.commons.httpclient.Cookie>();
    for (String headerName : response.getHeaderNames()) {
        if (headerName.equalsIgnoreCase("set-cookie") || headerName.equalsIgnoreCase("set-cookie2")) {
            cookies.addAll(Arrays.asList(new RFC2965Spec().parse("localhost", container.getPort(), "/", false,
                    response.getHeader(headerName).toString())));
        }
    }
    Assert.assertEquals(1, cookies.size());
    Cookie cookie = cookies.get(0);
    Assert.assertEquals("almp.JSESSIONID", cookie.getName());

    MockHttpServletRequest request2 = new MockHttpServletRequest();
    request2.setMethod("GET");
    request2.setRequestURI("/test");
    request2.addHeader("Cookie", cookie.toExternalForm());
    MockHttpServletResponse response2 = new MockHttpServletResponse();

    payload = new Payload(HttpServletResponse.SC_OK, "test");
    TestServlet.setResponsePayload(payload);

    proxy.proxyRequest(ajpBaseUri + "/foo", request2, response2);

    Request secondRequest = TestServlet.getLastRequest();
    Assert.assertFalse(secondRequest.isNewSession());
    Assert.assertEquals(firstRequest.getSessionId(), secondRequest.getSessionId());
    Assert.assertEquals("v1", secondRequest.getSessionAttributes().get("s1"));
}

From source file:org.sventon.web.ctrl.SubmitConfigurationsControllerTest.java

@Test
public void testHandleRequestInternal() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final SubmitConfigurationsController ctrl = new SubmitConfigurationsController();
    final MockServletContext servletContext = new MockServletContext();
    servletContext.setContextPath("sventon-test");
    configDirectory.setServletContext(servletContext);

    ctrl.setScheduler(new StdScheduler(null, null) {
        public void triggerJob(final String string, final String string1) {
        }//from   w  ww.  java  2 s.  com
    });

    final RepositoryConfiguration repositoryConfiguration1 = new RepositoryConfiguration("testrepos1");
    repositoryConfiguration1.setRepositoryUrl("http://localhost/1");
    repositoryConfiguration1.setUserCredentials(new Credentials("user1", "abc123"));
    repositoryConfiguration1.setCacheUsed(false);
    repositoryConfiguration1.setZippedDownloadsAllowed(false);

    final RepositoryConfiguration repositoryConfiguration2 = new RepositoryConfiguration("testrepos2");
    repositoryConfiguration2.setRepositoryUrl("http://localhost/2");
    repositoryConfiguration2.setUserCredentials(new Credentials("user2", "abc123"));
    repositoryConfiguration2.setCacheUsed(false);
    repositoryConfiguration2.setZippedDownloadsAllowed(false);

    application.addConfiguration(repositoryConfiguration1);
    application.addConfiguration(repositoryConfiguration2);
    application.setConfigured(false);
    ctrl.setApplication(application);
    ctrl.setServletContext(new MockServletContext());

    final File configFile1 = new File(configDirectory.getRepositoriesDirectory(), "testrepos1");
    final File configFile2 = new File(configDirectory.getRepositoriesDirectory(), "testrepos2");

    assertFalse(configFile1.exists());
    assertFalse(configFile2.exists());

    final ModelAndView modelAndView = ctrl.handleRequestInternal(request, response);
    assertNotNull(modelAndView);
    assertNull(modelAndView.getViewName()); // Will be null as it is a redirect view.

    //File should now be written
    assertTrue(configFile1.exists());
    assertTrue(configFile2.exists());
    FileUtils.deleteDirectory(configDirectory.getConfigRootDirectory());
    assertFalse(configFile1.exists());
    assertFalse(configFile2.exists());
    assertTrue(application.isConfigured());
}

From source file:com.hp.autonomy.frontend.find.idol.view.IdolViewControllerTest.java

@Test
public void viewServerError() {
    assertNotNull(viewController.handleViewServerErrorException(
            new ViewServerErrorException("some reference", new AciServiceException("It broke")),
            new MockHttpServletRequest(), new MockHttpServletResponse()));
}

From source file:io.lavagna.web.security.SecurityFilterTest.java

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

    SecurityFilter sf = new SecurityFilter();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");

    Map<Key, String> conf = new EnumMap<>(Key.class);
    conf.put(Key.SETUP_COMPLETE, "true");
    when(configurationRepository.findConfigurationFor(Mockito.<Set<Key>>any())).thenReturn(conf);

    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();

    sf.init(filterConfig);/*from   www  .j  a va 2  s  .  co m*/

    sf.doFilter(request, response, chain);
}

From source file:fi.okm.mpass.shibboleth.authn.impl.ShibbolethSpAuthnServletTest.java

@Test
public void testEmptyRequest() throws Exception {
    final MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    final MockHttpServletResponse servletResponse = new MockHttpServletResponse();
    servlet.doGet(servletRequest, servletResponse);
    Assert.assertNull(servletResponse.getRedirectedUrl());
    final AuthenticationContext authnContext = prc.getSubcontext(AuthenticationContext.class, false);
    final ExternalAuthenticationContext extContext = authnContext
            .getSubcontext(ExternalAuthenticationContext.class, true);
    Assert.assertNull(extContext.getSubject());
}