1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2012 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 .mock .web ;
18
18
19
+ import static org .junit .Assert .*;
20
+
19
21
import java .io .IOException ;
20
22
import java .util .Arrays ;
21
23
import java .util .Set ;
22
24
23
- import junit .framework .TestCase ;
25
+ import javax .servlet .http .HttpServletResponse ;
26
+
27
+ import org .junit .Test ;
24
28
25
29
import org .springframework .web .util .WebUtils ;
26
30
27
31
/**
32
+ * Unit tests for {@link MockHttpServletResponse}.
33
+ *
28
34
* @author Juergen Hoeller
29
35
* @author Rick Evans
30
36
* @author Rossen Stoyanchev
37
+ * @author Rob Winch
38
+ * @author Sam Brannen
31
39
* @since 19.02.2006
32
40
*/
33
- public class MockHttpServletResponseTests extends TestCase {
41
+ public class MockHttpServletResponseTests {
42
+
43
+ private MockHttpServletResponse response = new MockHttpServletResponse ();
34
44
35
- public void testSetContentType () {
45
+
46
+ @ Test
47
+ public void setContentType () {
36
48
String contentType = "test/plain" ;
37
- MockHttpServletResponse response = new MockHttpServletResponse ();
38
49
response .setContentType (contentType );
39
50
assertEquals (contentType , response .getContentType ());
40
51
assertEquals (contentType , response .getHeader ("Content-Type" ));
41
52
assertEquals (WebUtils .DEFAULT_CHARACTER_ENCODING , response .getCharacterEncoding ());
42
53
}
43
54
44
- public void testSetContentTypeUTF8 () {
55
+ @ Test
56
+ public void setContentTypeUTF8 () {
45
57
String contentType = "test/plain;charset=UTF-8" ;
46
- MockHttpServletResponse response = new MockHttpServletResponse ();
47
58
response .setContentType (contentType );
48
59
assertEquals ("UTF-8" , response .getCharacterEncoding ());
49
60
assertEquals (contentType , response .getContentType ());
50
61
assertEquals (contentType , response .getHeader ("Content-Type" ));
51
62
}
52
-
53
- public void testContentTypeHeader () {
63
+
64
+ @ Test
65
+ public void contentTypeHeader () {
54
66
String contentType = "test/plain" ;
55
- MockHttpServletResponse response = new MockHttpServletResponse ();
56
67
response .addHeader ("Content-Type" , contentType );
57
68
assertEquals (contentType , response .getContentType ());
58
69
assertEquals (contentType , response .getHeader ("Content-Type" ));
@@ -65,9 +76,9 @@ public void testContentTypeHeader() {
65
76
assertEquals (WebUtils .DEFAULT_CHARACTER_ENCODING , response .getCharacterEncoding ());
66
77
}
67
78
68
- public void testContentTypeHeaderUTF8 () {
79
+ @ Test
80
+ public void contentTypeHeaderUTF8 () {
69
81
String contentType = "test/plain;charset=UTF-8" ;
70
- MockHttpServletResponse response = new MockHttpServletResponse ();
71
82
response .setHeader ("Content-Type" , contentType );
72
83
assertEquals (contentType , response .getContentType ());
73
84
assertEquals (contentType , response .getHeader ("Content-Type" ));
@@ -80,51 +91,50 @@ public void testContentTypeHeaderUTF8() {
80
91
assertEquals ("UTF-8" , response .getCharacterEncoding ());
81
92
}
82
93
83
- public void testSetContentTypeThenCharacterEncoding () {
84
- MockHttpServletResponse response = new MockHttpServletResponse ();
94
+ @ Test
95
+ public void setContentTypeThenCharacterEncoding () {
85
96
response .setContentType ("test/plain" );
86
97
response .setCharacterEncoding ("UTF-8" );
87
98
assertEquals ("test/plain" , response .getContentType ());
88
99
assertEquals ("test/plain;charset=UTF-8" , response .getHeader ("Content-Type" ));
89
100
assertEquals ("UTF-8" , response .getCharacterEncoding ());
90
101
}
91
102
92
- public void testSetCharacterEncodingThenContentType () {
93
- MockHttpServletResponse response = new MockHttpServletResponse ();
103
+ @ Test
104
+ public void setCharacterEncodingThenContentType () {
94
105
response .setCharacterEncoding ("UTF-8" );
95
106
response .setContentType ("test/plain" );
96
107
assertEquals ("test/plain" , response .getContentType ());
97
108
assertEquals ("test/plain;charset=UTF-8" , response .getHeader ("Content-Type" ));
98
109
assertEquals ("UTF-8" , response .getCharacterEncoding ());
99
110
}
100
111
101
- public void testContentLength () {
102
- MockHttpServletResponse response = new MockHttpServletResponse ();
112
+ @ Test
113
+ public void contentLength () {
103
114
response .setContentLength (66 );
104
115
assertEquals (66 , response .getContentLength ());
105
116
assertEquals ("66" , response .getHeader ("Content-Length" ));
106
117
}
107
-
108
- public void testContentLengthHeader () {
109
- MockHttpServletResponse response = new MockHttpServletResponse ();
118
+
119
+ @ Test
120
+ public void contentLengthHeader () {
110
121
response .addHeader ("Content-Length" , "66" );
111
- assertEquals (66 ,response .getContentLength ());
122
+ assertEquals (66 , response .getContentLength ());
112
123
assertEquals ("66" , response .getHeader ("Content-Length" ));
113
124
}
114
-
115
- public void testHttpHeaderNameCasingIsPreserved () throws Exception {
116
- final String headerName = "Header1" ;
117
125
118
- MockHttpServletResponse response = new MockHttpServletResponse ();
126
+ @ Test
127
+ public void httpHeaderNameCasingIsPreserved () throws Exception {
128
+ final String headerName = "Header1" ;
119
129
response .addHeader (headerName , "value1" );
120
130
Set <String > responseHeaders = response .getHeaderNames ();
121
131
assertNotNull (responseHeaders );
122
132
assertEquals (1 , responseHeaders .size ());
123
133
assertEquals ("HTTP header casing not being preserved" , headerName , responseHeaders .iterator ().next ());
124
134
}
125
135
126
- public void testServletOutputStreamCommittedWhenBufferSizeExceeded () throws IOException {
127
- MockHttpServletResponse response = new MockHttpServletResponse ();
136
+ @ Test
137
+ public void servletOutputStreamCommittedWhenBufferSizeExceeded () throws IOException {
128
138
assertFalse (response .isCommitted ());
129
139
response .getOutputStream ().write ('X' );
130
140
assertFalse (response .isCommitted ());
@@ -134,8 +144,8 @@ public void testServletOutputStreamCommittedWhenBufferSizeExceeded() throws IOEx
134
144
assertEquals (size + 1 , response .getContentAsByteArray ().length );
135
145
}
136
146
137
- public void testServletOutputStreamCommittedOnFlushBuffer () throws IOException {
138
- MockHttpServletResponse response = new MockHttpServletResponse ();
147
+ @ Test
148
+ public void servletOutputStreamCommittedOnFlushBuffer () throws IOException {
139
149
assertFalse (response .isCommitted ());
140
150
response .getOutputStream ().write ('X' );
141
151
assertFalse (response .isCommitted ());
@@ -144,8 +154,8 @@ public void testServletOutputStreamCommittedOnFlushBuffer() throws IOException {
144
154
assertEquals (1 , response .getContentAsByteArray ().length );
145
155
}
146
156
147
- public void testServletWriterCommittedWhenBufferSizeExceeded () throws IOException {
148
- MockHttpServletResponse response = new MockHttpServletResponse ();
157
+ @ Test
158
+ public void servletWriterCommittedWhenBufferSizeExceeded () throws IOException {
149
159
assertFalse (response .isCommitted ());
150
160
response .getWriter ().write ("X" );
151
161
assertFalse (response .isCommitted ());
@@ -157,8 +167,8 @@ public void testServletWriterCommittedWhenBufferSizeExceeded() throws IOExceptio
157
167
assertEquals (size + 1 , response .getContentAsByteArray ().length );
158
168
}
159
169
160
- public void testServletOutputStreamCommittedOnOutputStreamFlush () throws IOException {
161
- MockHttpServletResponse response = new MockHttpServletResponse ();
170
+ @ Test
171
+ public void servletOutputStreamCommittedOnOutputStreamFlush () throws IOException {
162
172
assertFalse (response .isCommitted ());
163
173
response .getOutputStream ().write ('X' );
164
174
assertFalse (response .isCommitted ());
@@ -167,8 +177,8 @@ public void testServletOutputStreamCommittedOnOutputStreamFlush() throws IOExcep
167
177
assertEquals (1 , response .getContentAsByteArray ().length );
168
178
}
169
179
170
- public void testServletWriterCommittedOnWriterFlush () throws IOException {
171
- MockHttpServletResponse response = new MockHttpServletResponse ();
180
+ @ Test
181
+ public void servletWriterCommittedOnWriterFlush () throws IOException {
172
182
assertFalse (response .isCommitted ());
173
183
response .getWriter ().write ("X" );
174
184
assertFalse (response .isCommitted ());
@@ -177,22 +187,39 @@ public void testServletWriterCommittedOnWriterFlush() throws IOException {
177
187
assertEquals (1 , response .getContentAsByteArray ().length );
178
188
}
179
189
180
- public void testServletWriterAutoFlushedForString () throws IOException {
181
- MockHttpServletResponse response = new MockHttpServletResponse ();
190
+ @ Test
191
+ public void servletWriterAutoFlushedForString () throws IOException {
182
192
response .getWriter ().write ("X" );
183
193
assertEquals ("X" , response .getContentAsString ());
184
194
}
185
195
186
- public void testServletWriterAutoFlushedForChar () throws IOException {
187
- MockHttpServletResponse response = new MockHttpServletResponse ();
196
+ @ Test
197
+ public void servletWriterAutoFlushedForChar () throws IOException {
188
198
response .getWriter ().write ('X' );
189
199
assertEquals ("X" , response .getContentAsString ());
190
200
}
191
201
192
- public void testServletWriterAutoFlushedForCharArray () throws IOException {
193
- MockHttpServletResponse response = new MockHttpServletResponse ();
202
+ @ Test
203
+ public void servletWriterAutoFlushedForCharArray () throws IOException {
194
204
response .getWriter ().write ("XY" .toCharArray ());
195
205
assertEquals ("XY" , response .getContentAsString ());
196
206
}
197
207
208
+ @ Test
209
+ public void sendRedirect () throws IOException {
210
+ String redirectUrl = "/redirect" ;
211
+ response .sendRedirect (redirectUrl );
212
+ assertEquals (HttpServletResponse .SC_MOVED_TEMPORARILY , response .getStatus ());
213
+ assertEquals (redirectUrl , response .getHeader ("Location" ));
214
+ assertEquals (redirectUrl , response .getRedirectedUrl ());
215
+ assertTrue (response .isCommitted ());
216
+ }
217
+
218
+ @ Test
219
+ public void locationHeaderUpdatesGetRedirectedUrl () {
220
+ String redirectUrl = "/redirect" ;
221
+ response .setHeader ("Location" , redirectUrl );
222
+ assertEquals (redirectUrl , response .getRedirectedUrl ());
223
+ }
224
+
198
225
}
0 commit comments