use of org.springframework.mock.web.MockPart in project spring-framework by spring-projects.
the class MockMvcHttpConnector method initRequestBuilder.
private MockHttpServletRequestBuilder initRequestBuilder(HttpMethod httpMethod, URI uri, MockClientHttpRequest httpRequest, @Nullable byte[] bytes) {
String contentType = httpRequest.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
if (!StringUtils.startsWithIgnoreCase(contentType, "multipart/")) {
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.request(httpMethod, uri);
if (!ObjectUtils.isEmpty(bytes)) {
requestBuilder.content(bytes);
}
return requestBuilder;
}
// Parse the multipart request in order to adapt to Servlet Part's
MockMultipartHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.multipart(uri);
Assert.notNull(bytes, "No multipart content");
ReactiveHttpInputMessage inputMessage = MockServerHttpRequest.post(uri.toString()).headers(httpRequest.getHeaders()).body(Mono.just(DefaultDataBufferFactory.sharedInstance.wrap(bytes)));
MULTIPART_READER.read(ResolvableType.forClass(Part.class), inputMessage, Collections.emptyMap()).flatMap(part -> DataBufferUtils.join(part.content()).doOnNext(buffer -> {
byte[] partBytes = new byte[buffer.readableByteCount()];
buffer.read(partBytes);
DataBufferUtils.release(buffer);
// Adapt to jakarta.servlet.http.Part...
MockPart mockPart = (part instanceof FilePart ? new MockPart(part.name(), ((FilePart) part).filename(), partBytes) : new MockPart(part.name(), partBytes));
mockPart.getHeaders().putAll(part.headers());
requestBuilder.part(mockPart);
})).blockLast(TIMEOUT);
return requestBuilder;
}
use of org.springframework.mock.web.MockPart 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.MockPart in project spring-framework by spring-projects.
the class MultipartControllerTests method multipartRequestWithServletParts.
@Test
public void multipartRequestWithServletParts() throws Exception {
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
MockPart filePart = new MockPart("file", "orig", fileContent);
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockPart jsonPart = new MockPart("json", json);
jsonPart.getHeaders().setContentType(MediaType.APPLICATION_JSON);
standaloneSetup(new MultipartController()).build().perform(multipart("/multipartfile").part(filePart).part(jsonPart)).andExpect(status().isFound()).andExpect(model().attribute("fileContent", fileContent)).andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
use of org.springframework.mock.web.MockPart in project spring-framework by spring-projects.
the class MockMultipartHttpServletRequestBuilderTests method addFileAndParts.
// gh-26166
@Test
void addFileAndParts() throws Exception {
MockMultipartHttpServletRequest mockRequest = (MockMultipartHttpServletRequest) new MockMultipartHttpServletRequestBuilder("/upload").file(new MockMultipartFile("file", "test.txt", "text/plain", "Test".getBytes(UTF_8))).part(new MockPart("name", "value".getBytes(UTF_8))).buildRequest(new MockServletContext());
assertThat(mockRequest.getFileMap()).containsOnlyKeys("file");
assertThat(mockRequest.getParameterMap()).containsOnlyKeys("name");
assertThat(mockRequest.getParts()).extracting(Part::getName).containsExactly("name");
}
Aggregations