org.springframework.mock.web.MockMultipartHttpServletRequest Java Examples

The following examples show how to use org.springframework.mock.web.MockMultipartHttpServletRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MockMultipartHttpServletRequestBuilder.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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);
	this.files.stream().forEach(request::addFile);
	this.parts.values().stream().flatMap(Collection::stream).forEach(request::addPart);

	if (!this.parts.isEmpty()) {
		new StandardMultipartHttpServletRequest(request)
				.getMultiFileMap().values().stream().flatMap(Collection::stream)
				.forEach(request::addFile);
	}

	return request;
}
 
Example #2
Source File: MockMultipartHttpServletRequestBuilder.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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);
	this.files.stream().forEach(request::addFile);
	this.parts.values().stream().flatMap(Collection::stream).forEach(request::addPart);

	if (!this.parts.isEmpty()) {
		new StandardMultipartHttpServletRequest(request)
				.getMultiFileMap().values().stream().flatMap(Collection::stream)
				.forEach(request::addFile);
	}

	return request;
}
 
Example #3
Source File: ArtifactsControllerTest.java    From gocd with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnHttpErrorCodeWhenChecksumFileSaveFails() throws Exception {
    File artifactFile = new File("junk");
    JobIdentifier jobIdentifier = new JobIdentifier("pipeline-1", 1, "1", "stage-1", "2", "job-1", 122l);
    when(restfulService.findJob("pipeline-1", "1", "stage-1", "2", "job-1", 122l)).thenReturn(jobIdentifier);
    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'"));
}
 
Example #4
Source File: StreamPipeTest.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Test
public void doPipeHttpRequestAntiVirusTest() throws Exception {
	MockMultipartHttpServletRequest request = createMultipartHttpRequest(pipe, true);
	pipe.addParameter(createHttpRequestParameter(request, session));
	pipe.configure();
	pipe.start();
	PipeRunResult pipeRunResult = doPipe(pipe, "", 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().asString());
}
 
Example #5
Source File: StreamPipeTest.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Test
public void doPipeHttpRequestCheckAntiVirusPassedTest() throws Exception {
	pipe.setCheckAntiVirus(true);
	MockMultipartHttpServletRequest request = createMultipartHttpRequest(pipe, true);
	pipe.addParameter(createHttpRequestParameter(request, session));
	pipe.configure();
	pipe.start();
	PipeRunResult pipeRunResult = doPipe(pipe, "", 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().asString());
}
 
Example #6
Source File: MockMultipartHttpServletRequestBuilder.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #7
Source File: SpringMockRamlRequest.java    From raml-tester with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #8
Source File: ArtifactsControllerTest.java    From gocd with Apache License 2.0 5 votes vote down vote up
@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'"));

}
 
Example #9
Source File: StreamPipeTest.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Test
public void doPipeHttpRequestTest() throws Exception {
	MockMultipartHttpServletRequest request = createMultipartHttpRequest();
	pipe.addParameter(createHttpRequestParameter(request, session));
	pipe.configure();
	pipe.start();
	PipeRunResult pipeRunResult = doPipe(pipe, "", 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().asString());
}
 
Example #10
Source File: StreamPipeTest.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Test
public void doPipeHttpRequestCheckAntiVirusFailedTest() throws Exception {
	pipe.setCheckAntiVirus(true);
	PipeForward pipeAntiVirusFailedForward = new PipeForward();
	pipeAntiVirusFailedForward.setName(pipe.ANTIVIRUS_FAILED_FORWARD);
	pipe.registerForward(pipeAntiVirusFailedForward);
	MockMultipartHttpServletRequest request = createMultipartHttpRequest(pipe, true, true);
	pipe.addParameter(createHttpRequestParameter(request, session));
	pipe.configure();
	pipe.start();
	PipeRunResult pipeRunResult = doPipe(pipe, "", session);
	assertEquals("antiVirusFailed", pipeRunResult.getPipeForward().getName());
	String expectedResult = "multipart contains file [doc002.pdf] with antivirus status [Fail] and message []";
	assertEquals(expectedResult, pipeRunResult.getResult().asString());
}
 
Example #11
Source File: StreamPipeTest.java    From iaf with Apache License 2.0 5 votes vote down vote up
private Parameter createHttpRequestParameter(MockMultipartHttpServletRequest request, IPipeLineSession session) {
	session.put("httpRequest", request);
	Parameter parameter = new Parameter();
	parameter.setName("httpRequest");
	parameter.setSessionKey("httpRequest");
	return parameter;
}
 
Example #12
Source File: StreamPipeTest.java    From iaf with Apache License 2.0 4 votes vote down vote up
private MockMultipartHttpServletRequest createMultipartHttpRequest() throws Exception {
	return createMultipartHttpRequest(null, false);
}
 
Example #13
Source File: StreamPipeTest.java    From iaf with Apache License 2.0 4 votes vote down vote up
private MockMultipartHttpServletRequest createMultipartHttpRequest(StreamPipe pipe, boolean addAntiVirusParts) throws Exception {
	return createMultipartHttpRequest(pipe, addAntiVirusParts, false);
}
 
Example #14
Source File: StreamPipeTest.java    From iaf with Apache License 2.0 4 votes vote down vote up
private MockMultipartHttpServletRequest createMultipartHttpRequest(StreamPipe pipe, 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(
				pipe.getAntiVirusPartName(),
				pipe.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(
					pipe.getAntiVirusPassedMessage())) {
				throw new Exception("fail message ["
						+ antiVirusLastPartMessage
						+ "] must differ from pass message ["
						+ pipe.getAntiVirusPassedMessage() + "]");
			}
		} else {
			antiVirusLastPartMessage = pipe
					.getAntiVirusPassedMessage();
		}
		StringPart antiVirusPassedPart2 = new StringPart(
				pipe.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;
}