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:ch.rasc.extclassgenerator.GeneratorTestUtil.java

public static void testWriteModel(Class<?> clazz, String modelName) {
    try {/*from   ww w  . j  a v a  2  s.c  o  m*/
        MockHttpServletResponse response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS4, false);
        GeneratorTestUtil.compareExtJs4Code(modelName, response.getContentAsString(), false, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS5, false);
        GeneratorTestUtil.compareExtJs5Code(modelName, response.getContentAsString(), false, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.TOUCH2, false);
        GeneratorTestUtil.compareTouch2Code(modelName, response.getContentAsString(), false, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS4,
                IncludeValidation.NONE, false);
        GeneratorTestUtil.compareExtJs4Code(modelName, response.getContentAsString(), false, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS5,
                IncludeValidation.NONE, false);
        GeneratorTestUtil.compareExtJs5Code(modelName, response.getContentAsString(), false, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.TOUCH2,
                IncludeValidation.NONE, false);
        GeneratorTestUtil.compareTouch2Code(modelName, response.getContentAsString(), false, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS4);
        GeneratorTestUtil.compareExtJs4Code(modelName, response.getContentAsString(), false, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS5);
        GeneratorTestUtil.compareExtJs5Code(modelName, response.getContentAsString(), false, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.TOUCH2);
        GeneratorTestUtil.compareTouch2Code(modelName, response.getContentAsString(), false, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS4, true);
        GeneratorTestUtil.compareExtJs4Code(modelName, response.getContentAsString(), true, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS5, true);
        GeneratorTestUtil.compareExtJs5Code(modelName, response.getContentAsString(), true, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.TOUCH2, true);
        GeneratorTestUtil.compareTouch2Code(modelName, response.getContentAsString(), true, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS4,
                IncludeValidation.NONE, true);
        GeneratorTestUtil.compareExtJs4Code(modelName, response.getContentAsString(), true, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS5,
                IncludeValidation.NONE, true);
        GeneratorTestUtil.compareExtJs5Code(modelName, response.getContentAsString(), true, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.TOUCH2,
                IncludeValidation.NONE, true);
        GeneratorTestUtil.compareTouch2Code(modelName, response.getContentAsString(), true, false);

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:ch.rasc.extclassgenerator.GeneratorTestUtil.java

public static void testWriteModelBuiltinValidation(Class<?> clazz, String modelName) {
    try {/*from  ww w . j a v  a 2  s .c  om*/

        MockHttpServletResponse response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS4,
                IncludeValidation.BUILTIN, false);
        GeneratorTestUtil.compareExtJs4Code(modelName, response.getContentAsString(), false, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS5,
                IncludeValidation.BUILTIN, false);
        GeneratorTestUtil.compareExtJs5Code(modelName, response.getContentAsString(), false, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.TOUCH2,
                IncludeValidation.BUILTIN, false);
        GeneratorTestUtil.compareTouch2Code(modelName, response.getContentAsString(), false, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS4,
                IncludeValidation.BUILTIN, true);
        GeneratorTestUtil.compareExtJs4Code(modelName, response.getContentAsString(), true, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.EXTJS5,
                IncludeValidation.BUILTIN, true);
        GeneratorTestUtil.compareExtJs5Code(modelName, response.getContentAsString(), true, false);

        response = new MockHttpServletResponse();
        ModelGenerator.writeModel(new MockHttpServletRequest(), response, clazz, OutputFormat.TOUCH2,
                IncludeValidation.BUILTIN, true);
        GeneratorTestUtil.compareTouch2Code(modelName, response.getContentAsString(), true, false);

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.thoughtworks.go.server.functional.helpers.CSVResponse.java

public CSVResponse(MockHttpServletResponse response) throws UnsupportedEncodingException {
    this(response.getContentAsString(), response.getStatus(), response.getContentType());
}

From source file:io.github.robwin.swagger2markup.petstore.Swagger2MarkupTest.java

@Test
public void createSpringfoxSwaggerJson() throws Exception {
    //String designFirstSwaggerLocation = Swagger2MarkupTest.class.getResource("/swagger.yaml").getPath();

    String outputDir = System.getProperty("io.springfox.staticdocs.outputDir");
    MvcResult mvcResult = this.mockMvc.perform(get("/v2/api-docs").accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk()).andReturn();

    MockHttpServletResponse response = mvcResult.getResponse();
    String swaggerJson = response.getContentAsString();
    Files.createDirectories(Paths.get(outputDir));
    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputDir, "swagger.json"),
            StandardCharsets.UTF_8)) {
        writer.write(swaggerJson);// w  ww.j a  va 2s .  c om
    }
}

From source file:springfox.documentation.staticdocs.SwaggerResultHandler.java

/**
 * Apply the action on the given result.
 *
 * @param result the result of the executed request
 * @throws Exception if a failure occurs
 *///from   w w w . java2s . c om
@Override
public void handle(MvcResult result) throws Exception {
    MockHttpServletResponse response = result.getResponse();
    String swaggerJson = response.getContentAsString();
    Files.createDirectories(Paths.get(outputDir));
    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputDir, fileName),
            StandardCharsets.UTF_8)) {
        writer.write(swaggerJson);
    }
}

From source file:org.esupportail.dining.web.controllers.WebWidgetController.java

private String renderView(HttpServletRequest request, String viewName, Model model) throws Exception {
    View resolvedView = viewResolver.resolveViewName(viewName, Locale.US);
    MockHttpServletResponse mockResp = new MockHttpServletResponse();
    resolvedView.render(model.asMap(), request, mockResp);
    return mockResp.getContentAsString();
}

From source file:mjg.HelloServletJavaTest.java

@Test
public void testDoGet() {
    HelloServlet servlet = new HelloServlet();
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse resp = new MockHttpServletResponse();
    try {/*w w w.jav  a 2s .c o m*/
        servlet.doGet(req, resp);
        assertEquals("Hello, Servlet!", resp.getContentAsString().trim());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ServletException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.cloudfoundry.identity.api.web.ApiControllerTests.java

@Test
public void testNoUser() throws Exception {
    controller.setInfo(new ClassPathResource("info.tmpl"));
    HashMap<String, Object> model = new HashMap<String, Object>();
    View view = controller.info(model, null);
    MockHttpServletResponse response = new MockHttpServletResponse();
    view.render(model, new MockHttpServletRequest(), response);
    String content = response.getContentAsString();
    assertFalse("Wrong content: " + content, content.contains("\"user\""));
}

From source file:org.cloudfoundry.identity.api.web.ApiControllerTests.java

@Test
public void testWithUser() throws Exception {
    controller.setInfo(new ClassPathResource("info.tmpl"));
    HashMap<String, Object> model = new HashMap<String, Object>();
    View view = controller.info(model, new UsernamePasswordAuthenticationToken("marissa", "<NONE>"));
    MockHttpServletResponse response = new MockHttpServletResponse();
    view.render(model, new MockHttpServletRequest(), response);
    String content = response.getContentAsString();
    assertTrue("Wrong content: " + content, content.contains("\n  \"user\": \"marissa\""));
}

From source file:com.gu.management.guice.ManagementUrlDiscoveryServletTest.java

@Test
public void shouldOnlyListManagementUrls() throws IOException, ServletException {
    when(context.getContextPath()).thenReturn("/mywebapp");
    when(urlPatternDiscoveryService.getServletsUrlPatterns())
            .thenReturn(Lists.<String>newArrayList("/hello", "/management/manifest"));

    ManagementUrlDiscoveryServlet discoveryServlet = new ManagementUrlDiscoveryServlet(context,
            urlPatternDiscoveryService);
    MockHttpServletResponse response = new MockHttpServletResponse();

    discoveryServlet.doGet(new MockHttpServletRequest(), response);
    assertThat(response.getContentAsString(),
            containsString("<li><a href=\"/mywebapp/management/manifest\">/manifest</a></li>"));
    assertThat(response.getContentAsString(), Matchers.not(containsString("/hello")));
}