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:newcontroller.handler.impl.HttpMessageConverterHandlerBridgeTest.java

@Test
public void testBridge() throws Exception {
    HttpServletRequest request = MockMvcRequestBuilders.get("/").param("foo", "aaa").param("bar", "100")
            .buildRequest(new MockServletContext());
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpMessageConverterHandlerBridge<String> bridge = new HttpMessageConverterHandlerBridge<>("Hello World!",
            new StringHttpMessageConverter());
    bridge.bridge(new DefaultRequest(request), new DefaultResponse(response));
    assertThat(response.getContentAsString(), is("Hello World!"));
}

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

public void testResponseWithoutAuthMethod() throws Exception {
    final Map<String, Object> model = new HashMap<String, Object>();

    final Map<String, Object> attributes = new HashMap<String, Object>();
    attributes.put("testAttribute", "testValue");
    final SimplePrincipal principal = new SimplePrincipal("testPrincipal", attributes);

    final MutableAuthentication authentication = new MutableAuthentication(principal);
    final List<Authentication> authentications = new ArrayList<Authentication>();
    authentications.add(authentication);

    final Assertion assertion = new ImmutableAssertionImpl(authentications, TestUtils.getService(), true);

    model.put("assertion", assertion);

    final MockHttpServletResponse servletResponse = new MockHttpServletResponse();

    this.response.renderMergedOutputModel(model, new MockHttpServletRequest(), servletResponse);
    final String written = servletResponse.getContentAsString();

    assertTrue(written.contains("testPrincipal"));
    assertTrue(written.contains("testAttribute"));
    assertTrue(written.contains("testValue"));
    assertTrue(written.contains("urn:oasis:names:tc:SAML:1.0:am:unspecified"));
}

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

public void testResponseWithNoAttributes() throws Exception {
    final Map<String, Object> model = new HashMap<String, Object>();

    final SimplePrincipal principal = new SimplePrincipal("testPrincipal");

    final MutableAuthentication authentication = new MutableAuthentication(principal);
    authentication.getAttributes().put(SamlAuthenticationMetaDataPopulator.ATTRIBUTE_AUTHENTICATION_METHOD,
            SamlAuthenticationMetaDataPopulator.AUTHN_METHOD_SSL_TLS_CLIENT);
    authentication.getAttributes().put("testSamlAttribute", "value");

    final List<Authentication> authentications = new ArrayList<Authentication>();
    authentications.add(authentication);

    final Assertion assertion = new ImmutableAssertionImpl(authentications, TestUtils.getService(), true);

    model.put("assertion", assertion);

    final MockHttpServletResponse servletResponse = new MockHttpServletResponse();

    this.response.renderMergedOutputModel(model, new MockHttpServletRequest(), servletResponse);
    final String written = servletResponse.getContentAsString();

    assertTrue(written.contains("testPrincipal"));
    assertTrue(written.contains(SamlAuthenticationMetaDataPopulator.AUTHN_METHOD_SSL_TLS_CLIENT));
    assertTrue(written.contains("AuthenticationMethod"));
}

From source file:org.geomajas.gwt.server.mvc.GeomajasControllerTest.java

@Test
public void testMockWebContext() throws ServletException, IOException {
    // create mock context that loads from the classpath
    MockServletConfig config = new MockServletConfig();
    MockHttpServletRequest request = new MockHttpServletRequest(config.getServletContext());
    request.setContentType("text/x-gwt-rpc");
    request.setCharacterEncoding("UTF-8");
    request.setContent(("6|0|10|http://apps.geomajas.org/explorer/be.geosparc.Explorer/"
            + "|54044FB0C988344F1715C8B91330B0A2|org.geomajas.gwt.client.GeomajasService|"
            + "execute|org.geomajas.gwt.client.command.GwtCommand/4093389776|command.configuration.GetMap|"
            + "org.geomajas.command.dto.GetMapConfigurationRequest/104733661|explorer|mainMap|"
            + "ss.TqRPfHFh24NVxB|1|2|3|4|1|5|5|6|7|8|9|0|10|").getBytes("UTF-8"));
    request.addHeader("X-GWT-Permutation", "54044FB0C988344F1715C8B91330B0A2");
    request.addHeader("X-GWT-Module-Base", "http://test/module/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    defaultController.setServletConfig(config);
    defaultController.doPost(request, response);
    // expect the message of the out-dated 1.3 policy of GWT
    Assert.assertTrue(response.getContentAsString()
            .contains("Type 'org.geomajas.gwt.client.command.GwtCommand' was not assignable"
                    + " to 'com.google.gwt.user.client.rpc.IsSerializable'"));
}

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

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

    handler.handle(context, mock(Authentication.class));

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

From source file:com.github.woonsan.katharsis.servlet.KatharsisFilterTest.java

@Test
public void onSimpleCollectionGetShouldReturnCollectionOfResources() throws Exception {
    MockFilterChain filterChain = new MockFilterChain();

    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath(null);//from   w w w  .  j  av a2  s  . com
    request.setPathInfo(null);
    request.setRequestURI("/api/tasks/");
    request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
    request.addHeader("Accept", "*/*");

    MockHttpServletResponse response = new MockHttpServletResponse();

    katharsisFilter.doFilter(request, response, filterChain);

    String responseContent = response.getContentAsString();

    log.debug("responseContent: {}", responseContent);
    assertNotNull(responseContent);

    assertJsonPartEquals("tasks", responseContent, "data[0].type");
    assertJsonPartEquals("\"1\"", responseContent, "data[0].id");
    assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data[0].attributes");
    assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data[0].links");
    assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data[0].relationships.project.links");
    assertJsonPartEquals("[]", responseContent, "included");
}

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

public void testResponse() throws Exception {
    final Map<String, Object> model = new HashMap<String, Object>();

    final Map<String, Object> attributes = new HashMap<String, Object>();
    attributes.put("testAttribute", "testValue");
    attributes.put("testEmptyCollection", Collections.emptyList());
    attributes.put("testAttributeCollection", Arrays.asList(new String[] { "tac1", "tac2" }));
    final SimplePrincipal principal = new SimplePrincipal("testPrincipal", attributes);

    final MutableAuthentication authentication = new MutableAuthentication(principal);
    authentication.getAttributes().put(SamlAuthenticationMetaDataPopulator.ATTRIBUTE_AUTHENTICATION_METHOD,
            SamlAuthenticationMetaDataPopulator.AUTHN_METHOD_SSL_TLS_CLIENT);
    authentication.getAttributes().put("testSamlAttribute", "value");

    final List<Authentication> authentications = new ArrayList<Authentication>();
    authentications.add(authentication);

    final Assertion assertion = new ImmutableAssertionImpl(authentications, TestUtils.getService(), true);

    model.put("assertion", assertion);

    final MockHttpServletResponse servletResponse = new MockHttpServletResponse();

    this.response.renderMergedOutputModel(model, new MockHttpServletRequest(), servletResponse);
    final String written = servletResponse.getContentAsString();

    assertTrue(written.contains("testPrincipal"));
    assertTrue(written.contains("testAttribute"));
    assertTrue(written.contains("testValue"));
    assertFalse(written.contains("testEmptyCollection"));
    assertTrue(written.contains("testAttributeCollection"));
    assertTrue(written.contains("tac1"));
    assertTrue(written.contains("tac2"));
    assertTrue(written.contains(SamlAuthenticationMetaDataPopulator.AUTHN_METHOD_SSL_TLS_CLIENT));
    assertTrue(written.contains("AuthenticationMethod"));
    assertTrue(written.contains("AssertionID"));
}

From source file:com.github.woonsan.katharsis.servlet.KatharsisFilterTest.java

@Test
public void onSimpleResourceGetShouldReturnOneResource() throws Exception {
    MockFilterChain filterChain = new MockFilterChain();

    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath(null);//from  ww w.  j  av a 2  s  .  c  o m
    request.setPathInfo(null);
    request.setRequestURI("/api/tasks/1");
    request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
    request.addHeader("Accept", "*/*");
    request.addParameter("filter", "");

    MockHttpServletResponse response = new MockHttpServletResponse();

    katharsisFilter.doFilter(request, response, filterChain);

    String responseContent = response.getContentAsString();

    log.debug("responseContent: {}", responseContent);
    assertNotNull(responseContent);

    assertJsonPartEquals("tasks", responseContent, "data.type");
    assertJsonPartEquals("\"1\"", responseContent, "data.id");
    assertJsonPartEquals(SOME_TASK_ATTRIBUTES, responseContent, "data.attributes");
    assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data.links");
    assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data.relationships.project.links");
    assertJsonPartEquals("[]", responseContent, "included");
}

From source file:com.github.woonsan.katharsis.servlet.KatharsisFilterTest.java

@Test
public void onCollectionRequestWithParamsGetShouldReturnCollection() throws Exception {
    MockFilterChain filterChain = new MockFilterChain();

    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath(null);/*from  w  w w  . j a v  a 2 s  .c  o  m*/
    request.setPathInfo(null);
    request.setRequestURI("/api/tasks");
    request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
    request.addHeader("Accept", "*/*");
    request.addParameter("filter", "{\"name\":\"John\"}");

    MockHttpServletResponse response = new MockHttpServletResponse();

    katharsisFilter.doFilter(request, response, filterChain);

    String responseContent = response.getContentAsString();

    log.debug("responseContent: {}", responseContent);
    assertNotNull(responseContent);

    assertJsonPartEquals("tasks", responseContent, "data[0].type");
    assertJsonPartEquals("\"1\"", responseContent, "data[0].id");
    assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data[0].attributes");
    assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data[0].links");
    assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data[0].relationships.project.links");
    assertJsonPartEquals("[]", responseContent, "included");
}

From source file:org.geomajas.gwt.server.mvc.GeomajasControllerTest.java

@Test
public void testSerializationPolicy() throws UnsupportedEncodingException, ServletException {
    // create mock context that loads from the classpath
    MockServletConfig config = new MockServletConfig();
    MockHttpServletRequest request = new MockHttpServletRequest(config.getServletContext());
    request.setContentType("text/x-gwt-rpc");
    request.setCharacterEncoding("UTF-8");
    request.setContent(("6|0|10|http://apps.geomajas.org/explorer/be.geosparc.Explorer/"
            + "|54044FB0C988344F1715C8B91330B0A2|org.geomajas.gwt.client.GeomajasService|"
            + "execute|org.geomajas.gwt.client.command.GwtCommand/4093389776|command.configuration.GetMap|"
            + "org.geomajas.command.dto.GetMapConfigurationRequest/104733661|explorer|mainMap|"
            + "ss.TqRPfHFh24NVxB|1|2|3|4|1|5|5|6|7|8|9|0|10|").getBytes("UTF-8"));
    request.addHeader("X-GWT-Permutation", "54044FB0C988344F1715C8B91330B0A2");
    request.addHeader("X-GWT-Module-Base", "http://test/module/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    customController.setServletConfig(config);
    customController.doPost(request, response);
    // expect the message that the type is missing from our policy file
    Assert.assertTrue(response.getContentAsString().contains(
            "Type 'org.geomajas.gwt.client.command.GwtCommand' was not included in the set of types"));
}