Java 类org.springframework.mock.web.MockMultipartHttpServletRequest 实例源码

项目:gocd    文件:ArtifactsControllerTest.java   
@Test
public void shouldReturnHttpErrorCodeWhenChecksumFileSaveFails() throws Exception {
    File artifactFile = new File("junk");
    when(artifactService.findArtifact(any(JobIdentifier.class), eq("some-path"))).thenReturn(artifactFile);
    when(artifactService.saveFile(any(File.class), any(InputStream.class), eq(false), eq(1))).thenReturn(true);
    when(artifactService.saveOrAppendFile(any(File.class), any(InputStream.class))).thenReturn(false);

    MockMultipartHttpServletRequest mockMultipartHttpServletRequest = new MockMultipartHttpServletRequest();
    mockMultipartHttpServletRequest.addFile(new MockMultipartFile(REGULAR_MULTIPART_FILENAME, "content".getBytes()));
    mockMultipartHttpServletRequest.addFile(new MockMultipartFile(CHECKSUM_MULTIPART_FILENAME, "checksum-content".getBytes()));

    ModelAndView modelAndView = artifactsController.postArtifact("pipeline-1", "1", "stage-1", "2", "job-1", 122L, "some-path", 1, mockMultipartHttpServletRequest);


    ResponseCodeView view = (ResponseCodeView) modelAndView.getView();
    assertThat(view.getStatusCode(), is(SC_INTERNAL_SERVER_ERROR));
    assertThat(view.getContent(), is("Error saving checksum file for the artifact at path 'some-path'"));
}
项目:iaf    文件:StreamPipeTest.java   
@Test
public void doPipeHttpRequestTest() throws Exception {
    StreamPipe streamPipe = new StreamPipe();
    streamPipe.registerForward(createPipeSuccessForward());
    MockMultipartHttpServletRequest request = createMultipartHttpRequest();
    streamPipe.addParameter(createHttpRequestParameter(request, session));
    streamPipe.configure();
    PipeRunResult pipeRunResult = streamPipe.doPipe("", session);
    assertEquals("success", pipeRunResult.getPipeForward().getName());
    String expectedResult = "<parts>"
            + "<part type=\"string\" name=\"string1\" sessionKey=\"part_string\" size=\"19\"/>"
            + "<part type=\"file\" name=\"doc001.pdf\" sessionKey=\"part_file\" size=\"26358\" mimeType=\"application/octet-stream; charset=ISO-8859-1\"/>"
            + "<part type=\"file\" name=\"doc002.pdf\" sessionKey=\"part_file2\" size=\"25879\" mimeType=\"application/octet-stream; charset=ISO-8859-1\"/>"
            + "</parts>";
    assertEquals(expectedResult, pipeRunResult.getResult());
}
项目:iaf    文件:StreamPipeTest.java   
@Test
public void doPipeHttpRequestAntiVirusTest() throws Exception {
    StreamPipe streamPipe = new StreamPipe();
    streamPipe.registerForward(createPipeSuccessForward());
    MockMultipartHttpServletRequest request = createMultipartHttpRequest(
            streamPipe, true);
    streamPipe.addParameter(createHttpRequestParameter(request, session));
    streamPipe.configure();
    PipeRunResult pipeRunResult = streamPipe.doPipe("", session);
    assertEquals("success", pipeRunResult.getPipeForward().getName());
    String expectedResult = "<parts>"
            + "<part type=\"string\" name=\"string1\" sessionKey=\"part_string\" size=\"19\"/>"
            + "<part type=\"file\" name=\"doc001.pdf\" sessionKey=\"part_file\" size=\"26358\" mimeType=\"application/octet-stream; charset=ISO-8859-1\"/>"
            + "<part type=\"string\" name=\"antivirus_rc\" sessionKey=\"part_string2\" size=\"4\"/>"
            + "<part type=\"file\" name=\"doc002.pdf\" sessionKey=\"part_file2\" size=\"25879\" mimeType=\"application/octet-stream; charset=ISO-8859-1\"/>"
            + "<part type=\"string\" name=\"antivirus_rc\" sessionKey=\"part_string3\" size=\"4\"/>"
            + "</parts>";
    assertEquals(expectedResult, pipeRunResult.getResult());
}
项目:iaf    文件:StreamPipeTest.java   
@Test
public void doPipeHttpRequestCheckAntiVirusPassedTest() throws Exception {
    StreamPipe streamPipe = new StreamPipe();
    streamPipe.setCheckAntiVirus(true);
    streamPipe.registerForward(createPipeSuccessForward());
    MockMultipartHttpServletRequest request = createMultipartHttpRequest(
            streamPipe, true);
    streamPipe.addParameter(createHttpRequestParameter(request, session));
    streamPipe.configure();
    PipeRunResult pipeRunResult = streamPipe.doPipe("", session);
    assertEquals("success", pipeRunResult.getPipeForward().getName());
    String expectedResult = "<parts>"
            + "<part type=\"string\" name=\"string1\" sessionKey=\"part_string\" size=\"19\"/>"
            + "<part type=\"file\" name=\"doc001.pdf\" sessionKey=\"part_file\" size=\"26358\" mimeType=\"application/octet-stream; charset=ISO-8859-1\"/>"
            + "<part type=\"file\" name=\"doc002.pdf\" sessionKey=\"part_file2\" size=\"25879\" mimeType=\"application/octet-stream; charset=ISO-8859-1\"/>"
            + "</parts>";
    assertEquals(expectedResult, pipeRunResult.getResult());
}
项目:iaf    文件:StreamPipeTest.java   
@Test
public void doPipeHttpRequestCheckAntiVirusFailedTest() throws Exception {
    StreamPipe streamPipe = new StreamPipe();
    streamPipe.setCheckAntiVirus(true);
    PipeForward pipeAntiVirusFailedForward = new PipeForward();
    pipeAntiVirusFailedForward.setName(StreamPipe.ANTIVIRUS_FAILED_FORWARD);
    streamPipe.registerForward(pipeAntiVirusFailedForward);
    streamPipe.registerForward(createPipeSuccessForward());
    MockMultipartHttpServletRequest request = createMultipartHttpRequest(
            streamPipe, true, true);
    streamPipe.addParameter(createHttpRequestParameter(request, session));
    streamPipe.configure();
    PipeRunResult pipeRunResult = streamPipe.doPipe("", session);
    assertEquals("antiVirusFailed",
            pipeRunResult.getPipeForward().getName());
    String expectedResult = "multipart contains file [doc002.pdf] with antivirus status [Fail]";
    assertEquals(expectedResult, pipeRunResult.getResult());
}
项目:egovframework.rte.root    文件:FormValidationTest.java   
@Test
public void testValidationWithServletRequest(){

    Employee employee = new Employee();
    MockHttpServletRequest request = new MockMultipartHttpServletRequest();     
    ServletRequestDataBinder binder = new ServletRequestDataBinder(employee, "employee");

    request.addParameter("empId", "AA1001");
    request.addParameter("empName", "But You");
    request.addParameter("empAge", "12");

    binder.bind(request);
    Validator validator = new EmployeeValidator();
    Errors errors = binder.getBindingResult();
    validator.validate(employee, errors);

    assertFalse(errors.hasFieldErrors("empId"));
    assertFalse(errors.hasFieldErrors("empName"));
    assertTrue(errors.hasFieldErrors("empAge"));        
}
项目:egovframework.rte.root    文件:CommandMapArgumentResolverTest.java   
@Test
public void testCommandMapWithHttpMultiparRequest() throws ServletException, IOException{

    MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest();
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    mockRequest.setMethod("POST");
    mockRequest.setRequestURI("/test.do");
    mockRequest.addParameter("USA", "washington");
    mockRequest.addParameter("japan", "tokyo");
    mockRequest.addParameter("korea", "seoul");

    dispatcher.service(mockRequest, mockResponse);

    assertEquals("washington",mockRequest.getAttribute("USA"));
    assertEquals("tokyo",mockRequest.getAttribute("japan"));
    assertEquals("seoul",mockRequest.getAttribute("korea"));                
}
项目:spring4-understanding    文件:MockMultipartHttpServletRequestBuilder.java   
/**
 * Create a new {@link MockMultipartHttpServletRequest} based on the
 * supplied {@code ServletContext} and the {@code MockMultipartFiles}
 * added to this builder.
 */
@Override
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(servletContext);
    for (MockMultipartFile file : this.files) {
        request.addFile(file);
    }
    return request;
}
项目:coderadar    文件:ExtendedMockHttpServletRequestBuilder.java   
/**
 * Create a new {@link MockMultipartHttpServletRequest} based on the supplied {@code
 * ServletContext} and the {@code MockMultipartFiles} added to this builder.
 */
@Override
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
  MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(servletContext);
  for (MockMultipartFile file : this.files) {
    request.addFile(file);
  }
  return request;
}
项目:eHMP    文件:ParameterMapTests.java   
@Test
public void testMultipartRequest() throws Exception {
    MockMultipartFile mockMultipartFile = new MockMultipartFile("schnobb.txt", "wobb".getBytes(Charset.forName("UTF-8")));

    MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest();
    mockRequest.addFile(mockMultipartFile);

    ParameterMap params = new ParameterMap(mockRequest);
    assertThat((MockMultipartFile) params.get("schnobb.txt"), sameInstance(mockMultipartFile));
}
项目:class-guard    文件:MockMultipartHttpServletRequestBuilder.java   
@Override
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
    MockMultipartHttpServletRequest request =
            (servlet3Present ? createServlet3Request() : new MockMultipartHttpServletRequest());
    for (MockMultipartFile file : this.files) {
        request.addFile(file);
    }
    return request;
}
项目:class-guard    文件:MockMultipartHttpServletRequestBuilder.java   
private MockMultipartHttpServletRequest createServlet3Request() {
    try {
        String className = "org.springframework.test.web.servlet.request.Servlet3MockMultipartHttpServletRequest";
        Class<?> clazz = ClassUtils.forName(className, MockMultipartHttpServletRequestBuilder.class.getClassLoader());
        Constructor<?> constructor = clazz.getDeclaredConstructor();
        return (MockMultipartHttpServletRequest) BeanUtils.instantiateClass(constructor);
    }
    catch (Throwable ex) {
        throw new IllegalStateException("Failed to instantiate MockMultipartHttpServletRequest", ex);
    }
}
项目:class-guard    文件:MockMultipartHttpServletRequestTests.java   
@Test
public void mockMultipartHttpServletRequestWithByteArray() throws IOException {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    assertFalse(request.getFileNames().hasNext());
    assertNull(request.getFile("file1"));
    assertNull(request.getFile("file2"));
    assertTrue(request.getFileMap().isEmpty());

    request.addFile(new MockMultipartFile("file1", "myContent1".getBytes()));
    request.addFile(new MockMultipartFile("file2", "myOrigFilename", "text/plain", "myContent2".getBytes()));
    doTestMultipartHttpServletRequest(request);
}
项目:class-guard    文件:MockMultipartHttpServletRequestTests.java   
@Test
public void mockMultipartHttpServletRequestWithInputStream() throws IOException {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.addFile(new MockMultipartFile("file1", new ByteArrayInputStream("myContent1".getBytes())));
    request.addFile(new MockMultipartFile("file2", "myOrigFilename", "text/plain", new ByteArrayInputStream(
        "myContent2".getBytes())));
    doTestMultipartHttpServletRequest(request);
}
项目:raml-tester    文件:SpringMockRamlRequest.java   
@Override
public Values getFormValues() {
    final Values values = new Values(delegate.getParameterMap());
    if (delegate instanceof MockMultipartHttpServletRequest) {
        for (final Map.Entry<String, List<MultipartFile>> entry : ((MockMultipartHttpServletRequest) delegate).getMultiFileMap().entrySet()) {
            for (int i = 0; i < entry.getValue().size(); i++) {
                values.addValue(entry.getKey(), new FileValue());
            }
        }
    }
    return values;
}
项目:gocd    文件:ArtifactsControllerTest.java   
@Test
public void shouldReturnBadRequestIfRequiredHeadersAreMissingOnACreateArtifactRequest() throws Exception {
    MultipartHttpServletRequest multipartHttpServletRequest = new MockMultipartHttpServletRequest();

    when(systemEnvironment.isApiSafeModeEnabled()).thenReturn(true);
    ModelAndView modelAndView = artifactsController.postArtifact("pipeline", "invalid-label", "stage", "stage-counter", "job-name", 3L, "file-path", 3, multipartHttpServletRequest);
    ResponseCodeView codeView = (ResponseCodeView) modelAndView.getView();

    assertThat(codeView.getStatusCode(), is(HttpServletResponse.SC_BAD_REQUEST));
    assertThat(codeView.getContent(), is("Missing required header 'Confirm'"));

}
项目:iaf    文件:StreamPipeTest.java   
private Parameter createHttpRequestParameter(
        MockMultipartHttpServletRequest request,
        IPipeLineSession session) {
    session.put("httpRequest", request);
    Parameter parameter = new Parameter();
    parameter.setName("httpRequest");
    parameter.setSessionKey("httpRequest");
    return parameter;
}
项目:openmrs-module-radiology    文件:RadiologyTestData.java   
/**
 * Convenience method constructing a multipart http servlet request for the tests
 */
public static MockMultipartHttpServletRequest getMockMultipartHttpServletRequestForMockObsWithComplexConcept() {
    MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest();
    mockRequest.addFile(getMockMultipartFileForMockObsWithComplexConcept());

    return mockRequest;
}
项目:iaf    文件:StreamPipeTest.java   
private MockMultipartHttpServletRequest createMultipartHttpRequest()
        throws Exception {
    return createMultipartHttpRequest(null, false);
}
项目:iaf    文件:StreamPipeTest.java   
private MockMultipartHttpServletRequest createMultipartHttpRequest(
        StreamPipe streamPipe, boolean addAntiVirusParts) throws Exception {
    return createMultipartHttpRequest(streamPipe, addAntiVirusParts, false);
}
项目:iaf    文件:StreamPipeTest.java   
private MockMultipartHttpServletRequest createMultipartHttpRequest(
        StreamPipe streamPipe, boolean addAntiVirusParts,
        boolean antiVirusLastPartFailed) throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.setContentType(
            "multipart/mixed;boundary=gc0p4Jq0M2Yt08jU534c0p");
    List<Part> parts = new ArrayList<Part>();
    String string = "<hello>test</hello>";
    StringPart stringPart = new StringPart("string1", string);
    parts.add(stringPart);
    URL url = ClassUtils.getResourceURL(this, "/Documents/doc001.pdf");
    File file = new File(url.toURI());
    FilePart filePart = new FilePart("file1", file.getName(), file);
    parts.add(filePart);
    if (addAntiVirusParts) {
        StringPart antiVirusPassedPart = new StringPart(
                streamPipe.getAntiVirusPartName(),
                streamPipe.getAntiVirusPassedMessage());
        parts.add(antiVirusPassedPart);
    }
    URL url2 = ClassUtils.getResourceURL(this, "/Documents/doc002.pdf");
    File file2 = new File(url2.toURI());
    FilePart filePart2 = new FilePart("file2", file2.getName(), file2);
    parts.add(filePart2);
    if (addAntiVirusParts) {
        String antiVirusLastPartMessage;
        if (antiVirusLastPartFailed) {
            antiVirusLastPartMessage = "Fail";
            if (antiVirusLastPartMessage.equalsIgnoreCase(
                    streamPipe.getAntiVirusPassedMessage())) {
                throw new Exception("fail message ["
                        + antiVirusLastPartMessage
                        + "] must differ from pass message ["
                        + streamPipe.getAntiVirusPassedMessage() + "]");
            }
        } else {
            antiVirusLastPartMessage = streamPipe
                    .getAntiVirusPassedMessage();
        }
        StringPart antiVirusPassedPart2 = new StringPart(
                streamPipe.getAntiVirusPartName(),
                antiVirusLastPartMessage);
        parts.add(antiVirusPassedPart2);
    }
    Part allParts[] = new Part[parts.size()];
    allParts = parts.toArray(allParts);
    MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(
            allParts, new PostMethod().getParams());
    ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
    multipartRequestEntity.writeRequest(requestContent);
    request.setContent(requestContent.toByteArray());
    request.setContentType(multipartRequestEntity.getContentType());
    return request;
}