Example usage for org.springframework.mock.web MockHttpServletRequest getContentLength

List of usage examples for org.springframework.mock.web MockHttpServletRequest getContentLength

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest getContentLength.

Prototype

@Override
    public int getContentLength() 

Source Link

Usage

From source file:org.springframework.test.web.servlet.htmlunit.HtmlUnitRequestBuilderTest.java

@Test
public void buildRequestContentLength() {
    String content = "some content that has length";
    webRequest.setHttpMethod(HttpMethod.POST);
    webRequest.setRequestBody(content);/*w ww  . j  av  a 2  s  .  c o m*/

    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);

    assertThat(actualRequest.getContentLength()).isEqualTo(content.length());
}

From source file:org.springframework.test.web.servlet.htmlunit.HtmlUnitRequestBuilderTests.java

@Test
public void buildRequestContentLength() {
    String content = "some content that has length";
    webRequest.setHttpMethod(HttpMethod.POST);
    webRequest.setRequestBody(content);//from www  .  j a  v a 2s  .  c o m

    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);

    assertThat(actualRequest.getContentLength(), equalTo(content.length()));
}

From source file:com.redblackit.web.server.EchoServletTest.java

/**
 * Do test/*from   w ww.j a va 2 s  .co m*/
 *
 * @param method
 * @param hasBody if content should be expected
 */
private void doTest(String method, boolean hasBody) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod(method);
    request.setRequestURI(this.requestURI);

    final String msg = "doTest:" + method + ":hasBody=" + hasBody;
    logger.debug(msg + ":this=" + this);

    for (String headerName : headersMap.keySet()) {
        List<String> values = headersMap.get(headerName);
        if (values.size() == 1) {
            request.addHeader(headerName, values.get(0));
        } else {
            request.addHeader(headerName, values);
        }
        Enumeration<String> headerValues = request.getHeaders(headerName);
        int hi = 0;
        while (headerValues.hasMoreElements()) {
            logger.debug(msg + "request:header[" + headerName + "," + hi + "]=" + headerValues.nextElement());
            ++hi;
        }

        Assert.assertTrue(msg + "TEST ERROR:request:header[" + headerName + "]=" + values
                + ":shouldn't be empty (" + values.getClass() + ")", hi > 0);

    }

    int expectedContentLength = 0;
    if (hasBody && body != null && body.length() > 0) {
        request.setContent(body.getBytes());
        expectedContentLength = request.getContentLength();
    }

    MockHttpServletResponse response = new MockHttpServletResponse();
    echoServlet.service(request, response);

    String responseBody = response.getContentAsString();

    Assert.assertEquals("response code:" + response, HttpServletResponse.SC_OK, response.getStatus());
    Assert.assertEquals("requestURI and Location", requestURI, response.getHeader("Location"));

    Map<String, List<String>> responseHeadersMap = new TreeMap<String, List<String>>();
    for (String headerName : response.getHeaderNames()) {
        List<String> values = response.getHeaders(headerName);
        int hi = 0;
        for (String value : values) {
            logger.debug(msg + ":response:header[" + headerName + "," + hi + "]=" + value);
            ++hi;
        }

        if (hi == 0) {
            logger.debug(msg + ":response:header[" + headerName + "]=" + values + ":is empty ("
                    + values.getClass() + ")");
            values = Arrays.asList(new String[] { "" });
        }

        if (!(headerName.equals("Location") || headerName.equals("Content-Length"))) {
            responseHeadersMap.put(headerName, values);
        }
    }

    Assert.assertEquals("headers (excluding Location and Content-Length)", headersMap, responseHeadersMap);
    if (hasBody) {
        Assert.assertEquals("body", (body == null ? "" : body), responseBody);
    } else {
        Assert.assertEquals("body", "", responseBody);
    }

    Assert.assertEquals("contentLength", expectedContentLength, response.getContentLength());

}