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:com.nominanuda.springsoy.SoySourceTest.java

@Test
public void testJavaView() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/");
    req.addParameter("lang", "en");
    MockHttpServletResponse resp = new MockHttpServletResponse();

    SoyViewResolver viewResolver = new SoyViewResolver();
    viewResolver.setSoySource(soySource);

    LocaleResolver localeResolver = new QueryParamLocaleResolver();
    Locale loc = localeResolver.resolveLocale(req);
    View view = viewResolver.resolveViewName("examples.simple.helloWorld2", loc);
    Map<String, ?> m = Collections.emptyMap();
    view.render(m, req, resp);/*from  w w w  .ja v a2  s .c om*/
    Assert.assertEquals("Hello world!", resp.getContentAsString());
}

From source file:org.auraframework.integration.test.http.resource.InlineJsTest.java

@Test
public void testInlineScriptIsWritenIntoInlineJs() throws Exception {
    // Arrange/*from w ww. jav a 2s  .  c  om*/
    if (contextService.isEstablished()) {
        contextService.endContext();
    }

    String script = "var foo = null;";
    String templateMarkup = String.format(baseComponentTag, "isTemplate='true'",
            String.format("<script>%s</script>", script));
    DefDescriptor<ComponentDef> templateDesc = addSourceAutoCleanup(ComponentDef.class, templateMarkup);
    String appTagAttributes = String.format("template='%s'", templateDesc.getDescriptorName());
    String appMarkup = String.format(baseApplicationTag, appTagAttributes, "");
    DefDescriptor<ApplicationDef> appDesc = addSourceAutoCleanup(ApplicationDef.class, appMarkup);

    AuraContext context = contextService.startContext(AuraContext.Mode.DEV, AuraContext.Format.JS,
            AuraContext.Authentication.AUTHENTICATED, appDesc);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addParameter("jwt", configAdapter.generateJwtToken());
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    context.setFrameworkUID(configAdapter.getAuraFrameworkNonce());

    InlineJs inlineJs = getInlineJs();

    // Act
    inlineJs.write(mockRequest, mockResponse, context);
    String content = mockResponse.getContentAsString();

    // Assert
    assertEquals("Didn't find expected inline scripts in response content.", script, content.trim());
}

From source file:org.auraframework.integration.test.http.resource.InlineJsTest.java

@Test
public void testInlineScriptInTemplateWhichExtendsAuraTemplate() throws Exception {
    // Arrange//from   w  w  w  .  j a  v a2s.  c o m
    if (contextService.isEstablished()) {
        contextService.endContext();
    }

    String script = "var foo = null;";
    String cmpTagAttributes = String.format("isTemplate='true' extends='aura:template'");
    String templateMarkup = String.format(baseComponentTag, cmpTagAttributes,
            String.format("<script>%s</script>", script));
    DefDescriptor<ComponentDef> templateDesc = addSourceAutoCleanup(ComponentDef.class, templateMarkup);
    String appTagAttributes = String.format("template='%s'", templateDesc.getDescriptorName());
    String appMarkup = String.format(baseApplicationTag, appTagAttributes, "");
    DefDescriptor<ApplicationDef> appDesc = addSourceAutoCleanup(ApplicationDef.class, appMarkup);

    AuraContext context = contextService.startContext(AuraContext.Mode.DEV, AuraContext.Format.JS,
            AuraContext.Authentication.AUTHENTICATED, appDesc);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addParameter("jwt", configAdapter.generateJwtToken());
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    context.setFrameworkUID(configAdapter.getAuraFrameworkNonce());

    InlineJs inlineJs = getInlineJs();

    // Act
    inlineJs.write(mockRequest, mockResponse, context);
    String content = mockResponse.getContentAsString();

    // Assert
    assertThat("Didn't find expected inline scripts in response content.", content, containsString(script));
}

From source file:org.springsource.restbucks.PaymentProcessIntegrationTest.java

/**
 * Polls the order resource every 2 seconds and uses an {@code If-None-Match} header alongside the {@code ETag} of the
 * first response to avoid sending the representation over and over again.
 * //from  w w w .j  a v a 2  s.  c om
 * @param response
 * @return
 * @throws Exception
 */
private MockHttpServletResponse pollUntilOrderHasReceiptLink(MockHttpServletResponse response)
        throws Exception {

    // Grab
    String content = response.getContentAsString();
    Link orderLink = links.findLinkWithRel(ORDER_REL, content);

    // Poll order until receipt link is set
    Link receiptLink = null;
    String etag = null;
    MockHttpServletResponse pollResponse;

    do {

        HttpHeaders headers = new HttpHeaders();
        if (etag != null) {
            headers.setIfNoneMatch(etag);
        }

        log.info("Poll state of order until receipt is ready");

        ResultActions action = mvc.perform(get(orderLink.getHref()).headers(headers));
        pollResponse = action.andReturn().getResponse();

        int status = pollResponse.getStatus();
        etag = pollResponse.getHeader("ETag");

        log.info(String.format("Received %s with ETag of %s", status, etag));

        if (status == HttpStatus.OK.value()) {

            action.andExpect(linkWithRelIsPresent(Link.REL_SELF)). //
                    andExpect(linkWithRelIsNotPresent(UPDATE_REL)). //
                    andExpect(linkWithRelIsNotPresent(CANCEL_REL));

            receiptLink = links.findLinkWithRel(RECEIPT_REL, pollResponse.getContentAsString());

        } else if (status == HttpStatus.NO_CONTENT.value()) {
            action.andExpect(content().string(isEmptyOrNullString()));
        }

        if (receiptLink == null) {
            Thread.sleep(2000);
        }

    } while (receiptLink == null);

    return pollResponse;
}

From source file:org.auraframework.integration.test.http.resource.InlineJsTest.java

@Test
public void testMultpleInlineScriptOnSameTemplate() throws Exception {
    // Arrange//  w  w w  . j  av a 2  s  . com
    if (contextService.isEstablished()) {
        contextService.endContext();
    }

    String script = "var foo = null;";
    String templateMarkup = String.format(baseComponentTag, "isTemplate='true'",
            String.format("<script>%s</script><script>%s</script>", script, script));
    DefDescriptor<ComponentDef> templateDesc = addSourceAutoCleanup(ComponentDef.class, templateMarkup);
    String appTagAttributes = String.format("template='%s'", templateDesc.getDescriptorName());
    String appMarkup = String.format(baseApplicationTag, appTagAttributes, "");
    DefDescriptor<ApplicationDef> appDesc = addSourceAutoCleanup(ApplicationDef.class, appMarkup);

    AuraContext context = contextService.startContext(AuraContext.Mode.DEV, AuraContext.Format.JS,
            AuraContext.Authentication.AUTHENTICATED, appDesc);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addParameter("jwt", configAdapter.generateJwtToken());
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    context.setFrameworkUID(configAdapter.getAuraFrameworkNonce());

    InlineJs inlineJs = getInlineJs();

    // Act
    inlineJs.write(mockRequest, mockResponse, context);
    String content = mockResponse.getContentAsString();

    // Assert
    String expected = script + script;
    String actual = content.trim();
    assertEquals("Didn't find expected inline scripts in response content.", expected, actual);
}

From source file:com.nebhale.cyclinglibrary.web.GzipFilterTest.java

@Test
public void gzipRequest() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Content-Encoding", "gzip");
    request.setContent(gzipContent("test-request-content"));

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

    this.filter.doFilterInternal(request, response, filterChain);
    writeContent("test-response-content", filterChain.getResponse().getOutputStream());

    assertEquals("test-request-content", readContent(filterChain.getRequest().getInputStream()));
    assertEquals("test-response-content", response.getContentAsString());
}

From source file:org.auraframework.integration.test.http.resource.InlineJsTest.java

@Test
public void testMultpleInlineScriptOnTemplateInheritance() throws Exception {
    // Arrange/*from w ww  .j  a  va2  s.c o m*/
    if (contextService.isEstablished()) {
        contextService.endContext();
    }

    String script = "var foo = null;";
    String baseCmpTagAttributes = "isTemplate='true' extensible='true'";
    String baseTemplateMarkup = String.format(baseComponentTag, baseCmpTagAttributes,
            String.format("<script>%s</script>{!v.body}", script));
    DefDescriptor<ComponentDef> baseTemplateDesc = addSourceAutoCleanup(ComponentDef.class, baseTemplateMarkup);

    String cmpTagAttributes = String.format("isTemplate='true' extends='%s'",
            baseTemplateDesc.getDescriptorName());
    String templateMarkup = String.format(baseComponentTag, cmpTagAttributes,
            String.format("<script>%s</script>", script));
    DefDescriptor<ComponentDef> templateDesc = addSourceAutoCleanup(ComponentDef.class, templateMarkup);

    String appTagAttributes = String.format("template='%s'", templateDesc.getDescriptorName());
    String appMarkup = String.format(baseApplicationTag, appTagAttributes, "");
    DefDescriptor<ApplicationDef> appDesc = addSourceAutoCleanup(ApplicationDef.class, appMarkup);

    AuraContext context = contextService.startContext(AuraContext.Mode.DEV, AuraContext.Format.JS,
            AuraContext.Authentication.AUTHENTICATED, appDesc);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addParameter("jwt", configAdapter.generateJwtToken());
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    context.setFrameworkUID(configAdapter.getAuraFrameworkNonce());

    InlineJs inlineJs = getInlineJs();

    // Act
    inlineJs.write(mockRequest, mockResponse, context);
    String content = mockResponse.getContentAsString();

    // Assert
    String expected = script + script;
    String actual = content.trim();
    assertEquals("Didn't find expected inline scripts in response content.", expected, actual);
}

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: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:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java

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

    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());
}