@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2002 - 2022 the original author or authors .
* Copyright 2002 - 2023 the original author or authors .
*
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -32,7 +32,6 @@ import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.multipart.FilePart ;
import org.springframework.http.codec.multipart.FilePart ;
import org.springframework.http.codec.multipart.FormFieldPart ;
import org.springframework.http.codec.multipart.FormFieldPart ;
import org.springframework.http.codec.multipart.Part ;
import org.springframework.http.codec.multipart.Part ;
import org.springframework.http.converter.FormHttpMessageConverter ;
import org.springframework.util.LinkedMultiValueMap ;
import org.springframework.util.LinkedMultiValueMap ;
import org.springframework.util.MultiValueMap ;
import org.springframework.util.MultiValueMap ;
import org.springframework.web.client.RestTemplate ;
import org.springframework.web.client.RestTemplate ;
@ -56,44 +55,23 @@ class MultipartHttpHandlerIntegrationTests extends AbstractHttpHandlerIntegratio
}
}
@ParameterizedHttpServerTest
@ParameterizedHttpServerTest
void getFormPartsFormd ata ( HttpServer httpServer ) throws Exception {
void getMultipartFormD ata ( HttpServer httpServer ) throws Exception {
performTes t( httpServer , MediaType . MULTIPART_FORM_DATA ) ;
testMultipar t( httpServer , MediaType . MULTIPART_FORM_DATA ) ;
}
}
@ParameterizedHttpServerTest
@ParameterizedHttpServerTest
void getFormParts Mixed ( HttpServer httpServer ) throws Exception {
void getMultipart Mixed ( HttpServer httpServer ) throws Exception {
performTes t( httpServer , MediaType . MULTIPART_MIXED ) ;
testMultipar t( httpServer , MediaType . MULTIPART_MIXED ) ;
}
}
@ParameterizedHttpServerTest
@ParameterizedHttpServerTest
void getFormPartsRelated ( HttpServer httpServer ) throws Exception {
void getMultipartRelated ( HttpServer httpServer ) throws Exception {
RestTemplate restTemplate = new RestTemplate ( ) ;
testMultipart ( httpServer , MediaType . MULTIPART_RELATED ) ;
restTemplate . getMessageConverters ( ) . stream ( )
. filter ( FormHttpMessageConverter . class : : isInstance )
. map ( FormHttpMessageConverter . class : : cast )
. findFirst ( )
. orElseThrow ( )
. addSupportedMediaTypes ( MediaType . MULTIPART_RELATED ) ;
performTest ( httpServer , MediaType . MULTIPART_RELATED , restTemplate ) ;
}
}
private void performTest ( HttpServer httpServer , MediaType mediaType ) throws Exception {
private void testMultipart ( HttpServer httpServer , MediaType mediaType ) throws Exception {
performTest ( httpServer , mediaType , new RestTemplate ( ) ) ;
}
private void performTest ( HttpServer httpServer , MediaType mediaType , RestTemplate restTemplate ) throws Exception {
startServer ( httpServer ) ;
startServer ( httpServer ) ;
@SuppressWarnings ( "resource" )
RequestEntity < MultiValueMap < String , Object > > request = RequestEntity
. post ( URI . create ( "http://localhost:" + port + "/form-parts" ) )
. contentType ( mediaType )
. body ( generateBody ( ) ) ;
ResponseEntity < Void > response = restTemplate . exchange ( request , Void . class ) ;
assertThat ( response . getStatusCode ( ) ) . isEqualTo ( HttpStatus . OK ) ;
}
private MultiValueMap < String , Object > generateBody ( ) {
HttpHeaders fooHeaders = new HttpHeaders ( ) ;
HttpHeaders fooHeaders = new HttpHeaders ( ) ;
fooHeaders . setContentType ( MediaType . TEXT_PLAIN ) ;
fooHeaders . setContentType ( MediaType . TEXT_PLAIN ) ;
ClassPathResource fooResource = new ClassPathResource ( "org/springframework/http/codec/multipart/foo.txt" ) ;
ClassPathResource fooResource = new ClassPathResource ( "org/springframework/http/codec/multipart/foo.txt" ) ;
@ -102,7 +80,12 @@ class MultipartHttpHandlerIntegrationTests extends AbstractHttpHandlerIntegratio
MultiValueMap < String , Object > parts = new LinkedMultiValueMap < > ( ) ;
MultiValueMap < String , Object > parts = new LinkedMultiValueMap < > ( ) ;
parts . add ( "fooPart" , fooPart ) ;
parts . add ( "fooPart" , fooPart ) ;
parts . add ( "barPart" , barPart ) ;
parts . add ( "barPart" , barPart ) ;
return parts ;
URI url = URI . create ( "http://localhost:" + port + "/form-parts" ) ;
ResponseEntity < Void > response = new RestTemplate ( ) . exchange (
RequestEntity . post ( url ) . contentType ( mediaType ) . body ( parts ) , Void . class ) ;
assertThat ( response . getStatusCode ( ) ) . isEqualTo ( HttpStatus . OK ) ;
}
}