Search in sources :

Example 1 with MockPart

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;
}
Also used : MockMvcRequestBuilders.asyncDispatch(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch) FilePart(org.springframework.http.codec.multipart.FilePart) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockMvc(org.springframework.test.web.servlet.MockMvc) HttpCookie(org.springframework.http.HttpCookie) MockPart(org.springframework.mock.web.MockPart) RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) Part(org.springframework.http.codec.multipart.Part) MockMvcResultHandlers(org.springframework.test.web.servlet.result.MockMvcResultHandlers) Duration(java.time.Duration) MvcResult(org.springframework.test.web.servlet.MvcResult) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) Nullable(org.springframework.lang.Nullable) FlashMap(org.springframework.web.servlet.FlashMap) URI(java.net.URI) ResolvableType(org.springframework.core.ResolvableType) ResponseCookie(org.springframework.http.ResponseCookie) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) DefaultPartHttpMessageReader(org.springframework.http.codec.multipart.DefaultPartHttpMessageReader) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) StringWriter(java.io.StringWriter) ObjectUtils(org.springframework.util.ObjectUtils) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockMvcRequestBuilders(org.springframework.test.web.servlet.request.MockMvcRequestBuilders) ModelAndView(org.springframework.web.servlet.ModelAndView) List(java.util.List) Cookie(jakarta.servlet.http.Cookie) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) MockServerClientHttpResponse(org.springframework.test.web.reactive.server.MockServerClientHttpResponse) MockMultipartHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder) Collections(java.util.Collections) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) MockMultipartHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder) FilePart(org.springframework.http.codec.multipart.FilePart) MockPart(org.springframework.mock.web.MockPart) Part(org.springframework.http.codec.multipart.Part) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockPart(org.springframework.mock.web.MockPart) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) FilePart(org.springframework.http.codec.multipart.FilePart)

Example 2 with MockPart

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");
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.MockMultipartHttpServletRequest) MockPart(org.springframework.mock.web.MockPart) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 3 with MockPart

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")));
}
Also used : MockPart(org.springframework.mock.web.MockPart) Test(org.junit.jupiter.api.Test)

Example 4 with MockPart

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");
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.MockMultipartHttpServletRequest) MockPart(org.springframework.mock.web.MockPart) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

MockPart (org.springframework.mock.web.MockPart)4 Test (org.junit.jupiter.api.Test)3 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)2 MockMultipartHttpServletRequest (org.springframework.mock.web.MockMultipartHttpServletRequest)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 Cookie (jakarta.servlet.http.Cookie)1 StringWriter (java.io.StringWriter)1 URI (java.net.URI)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Function (java.util.function.Function)1 ResolvableType (org.springframework.core.ResolvableType)1 DataBufferUtils (org.springframework.core.io.buffer.DataBufferUtils)1 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)1 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)1 HttpCookie (org.springframework.http.HttpCookie)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpMethod (org.springframework.http.HttpMethod)1