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

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

Introduction

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

Prototype

@Override
    public void setCharacterEncoding(@Nullable String characterEncoding) 

Source Link

Usage

From source file:org.gwtwidgets.server.spring.test.TestRPCExporter.java

private HttpServletRequest getServiceRequest() throws Exception {
    MockHttpServletRequest serviceRequest = new MockHttpServletRequest("PUT", "/service");
    serviceRequest.setContentType("text/x-gwt-rpc");
    serviceRequest.setCharacterEncoding("UTF-8");
    serviceRequest.setContent(requestPayload.getBytes("UTF-8"));
    return serviceRequest;
}

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

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:com.cognitivabrasil.repositorio.web.FileControllerTest.java

@Test
public void testUploadFileErroNotMultipart() throws IOException, ServletException, FileUploadException {
    HttpServletResponse response = new MockHttpServletResponse();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCharacterEncoding("text/plain");
    request.setContent("algo".getBytes());
    request.setMethod("POST");

    FileController fileController = mockFiles();

    String result = fileController.upload(request, response);

    assertThat(result, equalTo(/*from   w  w  w . j av a2  s .c om*/
            "{\"jsonrpc\" : \"2.0\", \"error\" : {\"code\": 101, \"message\": \"Falha ao abrir o input stream.\"}, \"id\" : \"id\"}"));

}

From source file:org.cateproject.test.functional.mockmvc.HtmlUnitRequestBuilder.java

public MockHttpServletRequest buildRequest(ServletContext servletContext) {
    String charset = getCharset();
    String httpMethod = webRequest.getHttpMethod().name();
    UriComponents uriComponents = uriComponents();

    MockHttpServletRequest result = new HtmlUnitMockHttpServletRequest(servletContext, httpMethod,
            uriComponents.getPath());/*w  w w  .  j  ava  2  s. c  o m*/
    parent(result, parentBuilder);
    result.setServerName(uriComponents.getHost()); // needs to be first for additional headers
    authType(result);
    result.setCharacterEncoding(charset);
    content(result, charset);
    contextPath(result, uriComponents);
    contentType(result);
    cookies(result);
    headers(result);
    locales(result);
    servletPath(uriComponents, result);
    params(result, uriComponents);
    ports(uriComponents, result);
    result.setProtocol("HTTP/1.1");
    result.setQueryString(uriComponents.getQuery());
    result.setScheme(uriComponents.getScheme());
    pathInfo(uriComponents, result);

    return parentPostProcessor == null ? result : parentPostProcessor.postProcessRequest(result);
}

From source file:org.apache.struts2.interceptor.FileUploadInterceptorTest.java

public void testInvalidContentTypeMultipartRequest() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();

    req.setCharacterEncoding("text/html");
    req.setContentType("text/xml"); // not a multipart contentype
    req.addHeader("Content-type", "multipart/form-data");

    MyFileupAction action = new MyFileupAction();
    MockActionInvocation mai = new MockActionInvocation();
    mai.setAction(action);//from   w  ww .j a v a 2 s .  co m
    mai.setResultCode("success");
    mai.setInvocationContext(ActionContext.getContext());

    Map param = new HashMap();
    ActionContext.getContext().setParameters(param);
    ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, createMultipartRequest(req, 2000));

    interceptor.intercept(mai);

    assertTrue(action.hasErrors());
}

From source file:org.apache.struts2.interceptor.FileUploadInterceptorTest.java

public void testNoContentMultipartRequest() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();

    req.setCharacterEncoding("text/html");
    req.setContentType("multipart/form-data; boundary=---1234");
    req.setContent(null); // there is no content

    MyFileupAction action = new MyFileupAction();
    MockActionInvocation mai = new MockActionInvocation();
    mai.setAction(action);//from  w  w w. java 2s  .  com
    mai.setResultCode("success");
    mai.setInvocationContext(ActionContext.getContext());

    Map param = new HashMap();
    ActionContext.getContext().setParameters(param);
    ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST,
            createMultipartRequest((HttpServletRequest) req, 2000));

    interceptor.intercept(mai);

    assertTrue(action.hasErrors());
}

From source file:org.apache.struts2.interceptor.FileUploadInterceptorTest.java

public void testSuccessUploadOfATextFileMultipartRequest() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setCharacterEncoding("text/html");
    req.setContentType("multipart/form-data; boundary=---1234");
    req.addHeader("Content-type", "multipart/form-data");

    // inspired by the unit tests for jakarta commons fileupload
    String content = ("-----1234\r\n"
            + "Content-Disposition: form-data; name=\"file\"; filename=\"deleteme.txt\"\r\n"
            + "Content-Type: text/html\r\n" + "\r\n" + "Unit test of FileUploadInterceptor" + "\r\n"
            + "-----1234--\r\n");
    req.setContent(content.getBytes("US-ASCII"));

    MyFileupAction action = new MyFileupAction();

    MockActionInvocation mai = new MockActionInvocation();
    mai.setAction(action);/*from w  w  w.  j  av a2 s.c  o  m*/
    mai.setResultCode("success");
    mai.setInvocationContext(ActionContext.getContext());
    Map<String, Object> param = new HashMap<String, Object>();
    ActionContext.getContext().setParameters(param);
    ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, createMultipartRequest(req, 2000));

    interceptor.intercept(mai);

    assertTrue(!action.hasErrors());

    assertTrue(param.size() == 3);
    File[] files = (File[]) param.get("file");
    String[] fileContentTypes = (String[]) param.get("fileContentType");
    String[] fileRealFilenames = (String[]) param.get("fileFileName");

    assertNotNull(files);
    assertNotNull(fileContentTypes);
    assertNotNull(fileRealFilenames);
    assertTrue(files.length == 1);
    assertTrue(fileContentTypes.length == 1);
    assertTrue(fileRealFilenames.length == 1);
    assertEquals("text/html", fileContentTypes[0]);
    assertNotNull("deleteme.txt", fileRealFilenames[0]);
}

From source file:org.apache.struts2.interceptor.FileUploadInterceptorTest.java

/**
 * tests whether with multiple files sent with the same name, the ones with forbiddenTypes (see
 * FileUploadInterceptor.setAllowedTypes(...) ) are sorted out.
 *
 * @throws Exception//  w ww  .  j  ava2s. c om
 */
public void testMultipleAccept() throws Exception {
    final String htmlContent = "<html><head></head><body>html content</body></html>";
    final String plainContent = "plain content";
    final String bondary = "simple boundary";
    final String endline = "\r\n";

    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setCharacterEncoding("text/html");
    req.setMethod("POST");
    req.setContentType("multipart/form-data; boundary=" + bondary);
    req.addHeader("Content-type", "multipart/form-data");
    StringBuilder content = new StringBuilder(128);
    content.append(encodeTextFile(bondary, endline, "file", "test.html", "text/plain", plainContent));
    content.append(encodeTextFile(bondary, endline, "file", "test1.html", "text/html", htmlContent));
    content.append(encodeTextFile(bondary, endline, "file", "test2.html", "text/html", htmlContent));
    content.append(endline);
    content.append(endline);
    content.append(endline);
    content.append("--");
    content.append(bondary);
    content.append("--");
    content.append(endline);
    req.setContent(content.toString().getBytes());

    assertTrue(ServletFileUpload.isMultipartContent(req));

    MyFileupAction action = new MyFileupAction();
    MockActionInvocation mai = new MockActionInvocation();
    mai.setAction(action);
    mai.setResultCode("success");
    mai.setInvocationContext(ActionContext.getContext());
    Map<String, Object> param = new HashMap<String, Object>();
    ActionContext.getContext().setParameters(param);
    ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, createMultipartRequest(req, 2000));

    interceptor.setAllowedTypes("text/html");
    interceptor.intercept(mai);

    assertEquals(3, param.size());
    File[] files = (File[]) param.get("file");
    String[] fileContentTypes = (String[]) param.get("fileContentType");
    String[] fileRealFilenames = (String[]) param.get("fileFileName");

    assertNotNull(files);
    assertNotNull(fileContentTypes);
    assertNotNull(fileRealFilenames);
    assertEquals("files accepted ", 2, files.length);
    assertEquals(2, fileContentTypes.length);
    assertEquals(2, fileRealFilenames.length);
    assertEquals("text/html", fileContentTypes[0]);
    assertNotNull("test1.html", fileRealFilenames[0]);
}

From source file:org.osaf.cosmo.BaseMockServletTestCase.java

/**
 *///from   w  ww . j a  v  a 2s.com
protected void sendXmlRequest(MockHttpServletRequest request, Document doc) throws Exception {
    OutputFormat format = new OutputFormat("xml", "UTF-8", true);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLSerializer serializer = new XMLSerializer(out, format);
    serializer.setNamespaces(true);
    serializer.asDOMSerializer().serialize(doc);
    request.setContentType("text/xml");
    request.setCharacterEncoding("UTF-8");
    // log.debug("content: " + new String(out.toByteArray()));
    request.setContent(out.toByteArray());
    ;
}