29
29
import org .springframework .core .io .ClassPathResource ;
30
30
import org .springframework .core .io .InputStreamResource ;
31
31
import org .springframework .core .io .Resource ;
32
+ import org .springframework .http .ContentDisposition ;
32
33
import org .springframework .http .MediaType ;
33
34
import org .springframework .http .MockHttpInputMessage ;
34
35
import org .springframework .http .MockHttpOutputMessage ;
@@ -69,18 +70,24 @@ public void shouldReadImageResource() throws IOException {
69
70
byte [] body = FileCopyUtils .copyToByteArray (getClass ().getResourceAsStream ("logo.jpg" ));
70
71
MockHttpInputMessage inputMessage = new MockHttpInputMessage (body );
71
72
inputMessage .getHeaders ().setContentType (MediaType .IMAGE_JPEG );
73
+ inputMessage .getHeaders ().setContentDisposition (
74
+ ContentDisposition .builder ("attachment" ).filename ("yourlogo.jpg" ).build ());
72
75
Resource actualResource = converter .read (Resource .class , inputMessage );
73
76
assertThat (FileCopyUtils .copyToByteArray (actualResource .getInputStream ()), is (body ));
77
+ assertEquals ("yourlogo.jpg" , actualResource .getFilename ());
74
78
}
75
79
76
80
@ Test // SPR-13443
77
81
public void shouldReadInputStreamResource () throws IOException {
78
82
try (InputStream body = getClass ().getResourceAsStream ("logo.jpg" ) ) {
79
83
MockHttpInputMessage inputMessage = new MockHttpInputMessage (body );
80
84
inputMessage .getHeaders ().setContentType (MediaType .IMAGE_JPEG );
85
+ inputMessage .getHeaders ().setContentDisposition (
86
+ ContentDisposition .builder ("attachment" ).filename ("yourlogo.jpg" ).build ());
81
87
Resource actualResource = converter .read (InputStreamResource .class , inputMessage );
82
88
assertThat (actualResource , instanceOf (InputStreamResource .class ));
83
89
assertThat (actualResource .getInputStream (), is (body ));
90
+ assertEquals ("yourlogo.jpg" , actualResource .getFilename ());
84
91
}
85
92
}
86
93
@@ -100,6 +107,7 @@ public void shouldWriteImageResource() throws IOException {
100
107
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
101
108
Resource body = new ClassPathResource ("logo.jpg" , getClass ());
102
109
converter .write (body , null , outputMessage );
110
+
103
111
assertEquals ("Invalid content-type" , MediaType .IMAGE_JPEG ,
104
112
outputMessage .getHeaders ().getContentType ());
105
113
assertEquals ("Invalid content-length" , body .getFile ().length (), outputMessage .getHeaders ().getContentLength ());
@@ -111,45 +119,42 @@ public void writeByteArrayNullMediaType() throws IOException {
111
119
byte [] byteArray = {1 , 2 , 3 };
112
120
Resource body = new ByteArrayResource (byteArray );
113
121
converter .write (body , null , outputMessage );
122
+
114
123
assertTrue (Arrays .equals (byteArray , outputMessage .getBodyAsBytes ()));
115
124
}
116
125
117
- // SPR-12999
118
- @ Test @ SuppressWarnings ("unchecked" )
126
+ @ Test // SPR-12999
127
+ @ SuppressWarnings ("unchecked" )
119
128
public void writeContentNotGettingInputStream () throws Exception {
120
129
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
121
130
Resource resource = mock (Resource .class );
122
131
given (resource .getInputStream ()).willThrow (FileNotFoundException .class );
123
-
124
132
converter .write (resource , MediaType .APPLICATION_OCTET_STREAM , outputMessage );
125
133
126
134
assertEquals (0 , outputMessage .getHeaders ().getContentLength ());
127
135
}
128
136
129
- // SPR-12999
130
- @ Test
137
+ @ Test // SPR-12999
131
138
public void writeContentNotClosingInputStream () throws Exception {
132
139
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
133
140
Resource resource = mock (Resource .class );
134
141
InputStream inputStream = mock (InputStream .class );
135
142
given (resource .getInputStream ()).willReturn (inputStream );
136
143
given (inputStream .read (any ())).willReturn (-1 );
137
144
doThrow (new NullPointerException ()).when (inputStream ).close ();
138
-
139
145
converter .write (resource , MediaType .APPLICATION_OCTET_STREAM , outputMessage );
140
146
141
147
assertEquals (0 , outputMessage .getHeaders ().getContentLength ());
142
148
}
143
149
144
- // SPR-13620
145
- @ Test @ SuppressWarnings ("unchecked" )
150
+ @ Test // SPR-13620
151
+ @ SuppressWarnings ("unchecked" )
146
152
public void writeContentInputStreamThrowingNullPointerException () throws Exception {
147
153
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
148
154
Resource resource = mock (Resource .class );
149
155
InputStream in = mock (InputStream .class );
150
156
given (resource .getInputStream ()).willReturn (in );
151
157
given (in .read (any ())).willThrow (NullPointerException .class );
152
-
153
158
converter .write (resource , MediaType .APPLICATION_OCTET_STREAM , outputMessage );
154
159
155
160
assertEquals (0 , outputMessage .getHeaders ().getContentLength ());
0 commit comments