use of org.springframework.mock.web.MockMultipartHttpServletRequest in project spring-framework by spring-projects.
the class MockMultipartHttpServletRequestBuilderTests method addFileWithoutFilename.
// gh-26261, gh-26400
@Test
void addFileWithoutFilename() throws Exception {
MockPart jsonPart = new MockPart("data", "{\"node\":\"node\"}".getBytes(UTF_8));
jsonPart.getHeaders().setContentType(MediaType.APPLICATION_JSON);
MockMultipartHttpServletRequest mockRequest = (MockMultipartHttpServletRequest) new MockMultipartHttpServletRequestBuilder("/upload").file(new MockMultipartFile("file", "Test".getBytes(UTF_8))).part(jsonPart).buildRequest(new MockServletContext());
assertThat(mockRequest.getFileMap()).containsOnlyKeys("file");
assertThat(mockRequest.getParameterMap()).hasSize(1);
assertThat(mockRequest.getParameter("data")).isEqualTo("{\"node\":\"node\"}");
assertThat(mockRequest.getParts()).extracting(Part::getName).containsExactly("data");
}
use of org.springframework.mock.web.MockMultipartHttpServletRequest in project gocd by gocd.
the class ArtifactsControllerTest method shouldReturnBadRequestIfRequiredHeadersAreMissingOnACreateArtifactRequest.
@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'"));
}
use of org.springframework.mock.web.MockMultipartHttpServletRequest in project spring-framework by spring-projects.
the class MockMultipartHttpServletRequestBuilder method createServletRequest.
/**
* 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;
}
use of org.springframework.mock.web.MockMultipartHttpServletRequest in project iaf by ibissource.
the class StreamPipeTest method createMultipartHttpRequest.
private MockMultipartHttpServletRequest createMultipartHttpRequest(StreamPipe pipe, boolean addAntiVirusParts, boolean antiVirusLastPartFailed) throws Exception {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setBoundary("gc0p4Jq0M2Yt08jU534c0p");
builder.addTextBody("string1", "<hello>test</hello>");
URL url = ClassUtils.getResourceURL("/Documents/doc001.pdf");
File file = new File(url.toURI());
builder.addBinaryBody("file1", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());
if (addAntiVirusParts) {
builder.addTextBody(pipe.getAntiVirusPartName(), pipe.getAntiVirusPassedMessage());
}
URL url2 = ClassUtils.getResourceURL("/Documents/doc002.pdf");
File file2 = new File(url2.toURI());
builder.addBinaryBody("file2", file2, ContentType.APPLICATION_OCTET_STREAM, file2.getName());
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();
}
builder.addTextBody(pipe.getAntiVirusPartName(), antiVirusLastPartMessage);
}
ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
HttpEntity entity = builder.build();
entity.writeTo(requestContent);
request.setContent(requestContent.toByteArray());
request.setContentType(entity.getContentType().getValue());
return request;
}
use of org.springframework.mock.web.MockMultipartHttpServletRequest in project iaf by ibissource.
the class StreamPipeTest method doPipeHttpRequestTest.
@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\"/>" + "<part type=\"file\" name=\"doc002.pdf\" sessionKey=\"part_file2\" size=\"25879\" mimeType=\"application/octet-stream\"/>" + "</parts>";
assertEquals(expectedResult, pipeRunResult.getResult().asString());
}
Aggregations