1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .web .multipart .support ;
18
18
19
+ import java .io .IOException ;
20
+ import java .nio .charset .StandardCharsets ;
21
+
19
22
import org .junit .Test ;
20
23
24
+ import org .springframework .http .MockHttpOutputMessage ;
25
+ import org .springframework .http .converter .FormHttpMessageConverter ;
21
26
import org .springframework .mock .web .test .MockHttpServletRequest ;
22
27
import org .springframework .mock .web .test .MockPart ;
28
+ import org .springframework .util .LinkedMultiValueMap ;
29
+ import org .springframework .util .MultiValueMap ;
23
30
import org .springframework .web .multipart .MultipartFile ;
24
31
32
+ import static org .hamcrest .CoreMatchers .*;
25
33
import static org .junit .Assert .*;
26
34
27
35
/**
@@ -33,8 +41,8 @@ public class StandardMultipartHttpServletRequestTests {
33
41
34
42
@ Test
35
43
public void filename () throws Exception {
36
- StandardMultipartHttpServletRequest request = getRequest (
37
- "file" , "form-data; name= \ " file\" ; filename= \" myFile.txt \ " " );
44
+ String disposition = "form-data; name= \" file \" ; filename= \" myFile.txt \" " ;
45
+ StandardMultipartHttpServletRequest request = requestWithPart ( "file" , disposition , "" );
38
46
39
47
MultipartFile multipartFile = request .getFile ("file" );
40
48
assertNotNull (multipartFile );
@@ -43,8 +51,8 @@ public void filename() throws Exception {
43
51
44
52
@ Test // SPR-13319
45
53
public void filenameRfc5987 () throws Exception {
46
- StandardMultipartHttpServletRequest request = getRequest (
47
- "file" , "form-data; name= \ " file\" ; filename*= \" UTF-8''foo-%c3%a4-%e2%82%ac.html \ " " );
54
+ String disposition = "form-data; name= \" file \" ; filename*= \" UTF-8''foo-%c3%a4-%e2%82%ac.html \" " ;
55
+ StandardMultipartHttpServletRequest request = requestWithPart ( "file" , disposition , "" );
48
56
49
57
MultipartFile multipartFile = request .getFile ("file" );
50
58
assertNotNull (multipartFile );
@@ -53,19 +61,42 @@ public void filenameRfc5987() throws Exception {
53
61
54
62
@ Test // SPR-15205
55
63
public void filenameRfc2047 () throws Exception {
56
- StandardMultipartHttpServletRequest request = getRequest (
57
- "file" , "form-data; name= \ " file\" ; filename= \" =?UTF-8?Q?Declara=C3=A7=C3=A3o.pdf?= \ " " );
64
+ String disposition = "form-data; name= \" file \" ; filename= \" =?UTF-8?Q?Declara=C3=A7=C3=A3o.pdf?= \" " ;
65
+ StandardMultipartHttpServletRequest request = requestWithPart ( "file" , disposition , "" );
58
66
59
67
MultipartFile multipartFile = request .getFile ("file" );
60
68
assertNotNull (multipartFile );
61
69
assertEquals ("Declaração.pdf" , multipartFile .getOriginalFilename ());
62
70
}
63
71
72
+ @ Test
73
+ public void multipartFileResource () throws IOException {
74
+ String name = "file" ;
75
+ String disposition = "form-data; name=\" " + name + "\" ; filename=\" myFile.txt\" " ;
76
+ StandardMultipartHttpServletRequest request = requestWithPart (name , disposition , "myBody" );
77
+ MultipartFile multipartFile = request .getFile (name );
78
+
79
+ assertNotNull (multipartFile );
80
+
81
+ MultiValueMap <String , Object > map = new LinkedMultiValueMap <>();
82
+ map .add (name , multipartFile .getResource ());
83
+
84
+ MockHttpOutputMessage output = new MockHttpOutputMessage ();
85
+ new FormHttpMessageConverter ().write (map , null , output );
86
+
87
+ assertThat (output .getBodyAsString (StandardCharsets .UTF_8 ), containsString (
88
+ "Content-Disposition: form-data; name=\" file\" ; filename=\" myFile.txt\" \r \n " +
89
+ "Content-Type: text/plain\r \n " +
90
+ "Content-Length: 6\r \n " +
91
+ "\r \n " +
92
+ "myBody\r \n " ));
93
+ }
94
+
64
95
65
- private StandardMultipartHttpServletRequest getRequest (String name , String dispositionValue ) {
96
+ private StandardMultipartHttpServletRequest requestWithPart (String name , String disposition , String content ) {
66
97
MockHttpServletRequest request = new MockHttpServletRequest ();
67
- MockPart part = new MockPart (name , null );
68
- part .getHeaders ().set ("Content-Disposition" , dispositionValue );
98
+ MockPart part = new MockPart (name , null , content . getBytes ( StandardCharsets . UTF_8 ) );
99
+ part .getHeaders ().set ("Content-Disposition" , disposition );
69
100
request .addPart (part );
70
101
return new StandardMultipartHttpServletRequest (request );
71
102
}
0 commit comments