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

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

Introduction

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

Prototype

public String getContentAsString() throws UnsupportedEncodingException 

Source Link

Document

Get the content of the response body as a String , using the charset specified for the response by the application, either through HttpServletResponse methods or through a charset parameter on the Content-Type .

Usage

From source file:be.dnsbelgium.rdap.spring.security.RDAPErrorHandlerTest.java

@Test
public void testHandler() throws IOException, ServletException {
    RDAPErrorHandler errorHandler = new RDAPErrorHandler();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RDAPErrorException exception = new RDAPErrorException(499, "title", "desc1", "desc2");
    errorHandler.handle(request, response, exception);
    Assert.assertEquals("{\"errorCode\":499,\"title\":\"title\",\"description\":[\"desc1\",\"desc2\"]}",
            response.getContentAsString());
}

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

@DirtiesContext
@Test/*from  w w  w .j a v a  2 s.c  om*/
public void testListZeroNanopubs() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/nanopubs");

    MockHttpServletResponse response = new MockHttpServletResponse();

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

    assertEquals("[]", response.getContentAsString());
}

From source file:org.craftercms.security.authorization.impl.RestAccessDeniedHandlerTest.java

@Test
public void testHandle() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/admin.json");
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);

    handler.handle(context, new AccessDeniedException(ERROR_MESSAGE));

    assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getStatus());
    assertEquals(EXPECTED_RESPONSE_CONTENT, response.getContentAsString());
}

From source file:com.gu.management.manifest.ManifestReportingControllerTest.java

@Test
public void shouldWriteManifestToResponseStream() throws Exception {
    when(manifest.getRevisionNumber()).thenReturn(666L);

    MockHttpServletResponse responseMock = new MockHttpServletResponse();

    ManifestReportingServlet servlet = new ManifestReportingServlet(Arrays.asList(manifest));
    servlet.doGet(null, responseMock);/*  w w w  .j a  va2  s . c  o  m*/

    assertThat(responseMock.getContentType(), equalTo("text/plain"));
    assertThat(responseMock.getContentAsString(), equalTo("Code Manifest Information\n"));

    verify(manifest).reload();
}

From source file:com.gu.management.manifest.ManifestReportingControllerTest.java

@Test
public void shouldNotThrowExceptionWhenCodeOrViewRevisionIsNullJustLikeItWillBeOnDeveloperMachines()
        throws Exception {
    when(manifest.getRevisionNumber()).thenReturn(null);

    MockHttpServletResponse responseMock = new MockHttpServletResponse();

    ManifestReportingServlet servlet = new ManifestReportingServlet(Arrays.asList(manifest));
    servlet.doGet(null, responseMock);//www .  j  av a 2 s  .co m

    assertThat(responseMock.getContentType(), equalTo("text/plain"));
    assertThat(responseMock.getContentAsString(), equalTo("Code Manifest Information\n"));

    verify(manifest).reload();
}

From source file:org.floggy.synchronization.jme.server.SynchronizationServletTest.java

/**
* DOCUMENT ME!/*from  w  w w .  ja va2 s. c  om*/
*
* @throws Exception DOCUMENT ME!
*/
@Test
public void testDoGetServletRequestServletResponseReceive() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    SynchronizationServlet servlet = new SynchronizationServlet();

    request.setContent(getContent(Person.class));

    servlet.doGet(request, response);

    JSONObject jsonObject = JSONObject.fromObject(response.getContentAsString());

    Person person = (Person) JSONObject.toBean(jsonObject, Person.class);

    Assert.assertEquals(23, person.getAge());
    Assert.assertEquals("Floggy", person.getName());
}

From source file:org.craftercms.security.authentication.impl.RestLoginFailureHandlerTest.java

@Test
public void testHandle() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/login.json");
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);

    handler.handle(context, new BadCredentialsException(ERROR_MESSAGE));

    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
    assertEquals(EXPECTED_RESPONSE_CONTENT, response.getContentAsString());
}

From source file:org.craftercms.security.authentication.impl.RestAuthenticationRequiredHandlerTest.java

@Test
public void testHandle() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/profile.json");
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);

    handler.handle(context, new AuthenticationRequiredException(ERROR_MESSAGE));

    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
    assertEquals(EXPECTED_RESPONSE_CONTENT, response.getContentAsString());
}

From source file:org.jasig.cas.web.view.Saml10FailureResponseViewTests.java

public void testResponse() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    request.addParameter("TARGET", "service");

    final String description = "Validation failed";
    this.view.renderMergedOutputModel(Collections.<String, Object>singletonMap("description", description),
            request, response);/*from ww w. j av a  2s. c  om*/

    final String responseText = response.getContentAsString();
    assertTrue(responseText.contains("Status"));
    assertTrue(responseText.contains(description));
}

From source file:org.openmrs.web.servlet.DownloadDictionaryServletTest.java

private String runServletWithConcepts(Concept... concepts) throws Exception {
    DownloadDictionaryServlet downloadServlet = new DownloadDictionaryServlet();
    MockHttpServletRequest request = new MockHttpServletRequest("GET",
            "/moduleServlet/legacyui/downloadDictionaryServlet");
    request.setContextPath("/somecontextpath");
    MockHttpServletResponse response = new MockHttpServletResponse();

    List<Concept> conceptList = Arrays.asList(concepts);
    Mockito.when(conceptService.conceptIterator()).thenReturn(conceptList.iterator());

    downloadServlet.service(request, response);
    return response.getContentAsString();
}