Search in sources :

Example 16 with MultipartBodyBuilder

use of org.springframework.http.client.MultipartBodyBuilder in project spring-framework by spring-projects.

the class MultipartControllerTests method multipartRequestWithOptionalFileArray.

@Test
public void multipartRequestWithOptionalFileArray() throws Exception {
    byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
    Map<String, String> json = Collections.singletonMap("name", "yeeeah");
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.part("file", fileContent).filename("orig");
    bodyBuilder.part("file", fileContent).filename("orig");
    bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
    EntityExchangeResult<Void> exchangeResult = testClient.post().uri("/optionalfilearray").bodyValue(bodyBuilder.build()).exchange().expectStatus().isFound().expectBody().isEmpty();
    // Further assertions on the server response
    MockMvcWebTestClient.resultActionsFor(exchangeResult).andExpect(model().attribute("fileContent", fileContent)).andExpect(model().attribute("jsonContent", json));
}
Also used : MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) Test(org.junit.jupiter.api.Test)

Example 17 with MultipartBodyBuilder

use of org.springframework.http.client.MultipartBodyBuilder 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);
    Map<String, String> json = Collections.singletonMap("name", "yeeeah");
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.part("file", fileContent).filename("orig");
    bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
    EntityExchangeResult<Void> exchangeResult = testClient.post().uri("/multipartfile").bodyValue(bodyBuilder.build()).exchange().expectStatus().isFound().expectBody().isEmpty();
    // Further assertions on the server response
    MockMvcWebTestClient.resultActionsFor(exchangeResult).andExpect(model().attribute("fileContent", fileContent)).andExpect(model().attribute("jsonContent", json));
}
Also used : MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) Test(org.junit.jupiter.api.Test)

Example 18 with MultipartBodyBuilder

use of org.springframework.http.client.MultipartBodyBuilder in project spring-framework by spring-projects.

the class MultipartControllerTests method multipartRequestWithSingleFile.

@Test
public void multipartRequestWithSingleFile() throws Exception {
    byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
    Map<String, String> json = Collections.singletonMap("name", "yeeeah");
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.part("file", fileContent).filename("orig");
    bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
    EntityExchangeResult<Void> exchangeResult = testClient.post().uri("/multipartfile").bodyValue(bodyBuilder.build()).exchange().expectStatus().isFound().expectBody().isEmpty();
    // Further assertions on the server response
    MockMvcWebTestClient.resultActionsFor(exchangeResult).andExpect(model().attribute("fileContent", fileContent)).andExpect(model().attribute("jsonContent", json));
}
Also used : MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) Test(org.junit.jupiter.api.Test)

Example 19 with MultipartBodyBuilder

use of org.springframework.http.client.MultipartBodyBuilder in project spring-framework by spring-projects.

the class MultipartControllerTests method multipartRequestWithOptionalFileArrayNotPresent.

@Test
public void multipartRequestWithOptionalFileArrayNotPresent() throws Exception {
    Map<String, String> json = Collections.singletonMap("name", "yeeeah");
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
    EntityExchangeResult<Void> exchangeResult = testClient.post().uri("/optionalfilearray").bodyValue(bodyBuilder.build()).exchange().expectStatus().isFound().expectBody().isEmpty();
    // Further assertions on the server response
    MockMvcWebTestClient.resultActionsFor(exchangeResult).andExpect(model().attributeDoesNotExist("fileContent")).andExpect(model().attribute("jsonContent", json));
}
Also used : MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) Test(org.junit.jupiter.api.Test)

Example 20 with MultipartBodyBuilder

use of org.springframework.http.client.MultipartBodyBuilder in project spring-framework by spring-projects.

the class MultipartControllerTests method multipartRequestWrapped.

@Test
public void multipartRequestWrapped() throws Exception {
    Map<String, String> json = Collections.singletonMap("name", "yeeeah");
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
    WebTestClient client = MockMvcWebTestClient.bindToController(new MultipartController()).filter(new RequestWrappingFilter()).build();
    EntityExchangeResult<Void> exchangeResult = client.post().uri("/multipartfile").bodyValue(bodyBuilder.build()).exchange().expectStatus().isFound().expectBody().isEmpty();
    // Further assertions on the server response
    MockMvcWebTestClient.resultActionsFor(exchangeResult).andExpect(model().attribute("jsonContent", json));
}
Also used : MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) MockMvcWebTestClient(org.springframework.test.web.servlet.client.MockMvcWebTestClient) Test(org.junit.jupiter.api.Test)

Aggregations

MultipartBodyBuilder (org.springframework.http.client.MultipartBodyBuilder)36 Test (org.junit.jupiter.api.Test)34 MethodParameter (org.springframework.core.MethodParameter)18 Part (org.springframework.http.codec.multipart.Part)7 RequestPart (org.springframework.web.bind.annotation.RequestPart)7 MvcAnnotationPredicates.requestPart (org.springframework.web.testfixture.method.MvcAnnotationPredicates.requestPart)7 ClassPathResource (org.springframework.core.io.ClassPathResource)4 MultiValueMap (org.springframework.util.MultiValueMap)4 BindingContext (org.springframework.web.reactive.BindingContext)4 ServerWebExchange (org.springframework.web.server.ServerWebExchange)4 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)4 Resource (org.springframework.core.io.Resource)3 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3 ServerWebInputException (org.springframework.web.server.ServerWebInputException)2 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 MediaType (org.springframework.http.MediaType)1 MultipartHttpMessageReader (org.springframework.http.codec.multipart.MultipartHttpMessageReader)1 MultipartHttpMessageWriter (org.springframework.http.codec.multipart.MultipartHttpMessageWriter)1 WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)1