1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 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.
19
19
import java .net .URI ;
20
20
import java .util .HashMap ;
21
21
import java .util .Map ;
22
+
22
23
import javax .servlet .http .Cookie ;
23
24
24
- import org .junit .Before ;
25
25
import org .junit .Test ;
26
26
27
27
import org .springframework .http .HttpHeaders ;
45
45
* Tests for {@link PrintingResultHandler}.
46
46
*
47
47
* @author Rossen Stoyanchev
48
+ * @author Sam Brannen
48
49
*/
49
50
public class PrintingResultHandlerTests {
50
51
51
- private TestPrintingResultHandler handler ;
52
-
53
- private MockHttpServletRequest request ;
52
+ private final TestPrintingResultHandler handler = new TestPrintingResultHandler ();
54
53
55
- private MockHttpServletResponse response ;
54
+ private final MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/" ) {
55
+ @ Override
56
+ public boolean isAsyncStarted () {
57
+ return false ;
58
+ }
59
+ };
56
60
57
- private StubMvcResult mvcResult ;
61
+ private final MockHttpServletResponse response = new MockHttpServletResponse () ;
58
62
63
+ private final StubMvcResult mvcResult = new StubMvcResult (this .request , null , null ,
64
+ null , null , null , this .response );
59
65
60
- @ Before
61
- public void setup () {
62
- this .handler = new TestPrintingResultHandler ();
63
- this .request = new MockHttpServletRequest ("GET" , "/" ) {
64
- @ Override
65
- public boolean isAsyncStarted () {
66
- return false ;
67
- }
68
- };
69
- this .response = new MockHttpServletResponse ();
70
- this .mvcResult = new StubMvcResult (this .request , null , null , null , null , null , this .response );
71
- }
72
66
73
67
@ Test
74
- public void testPrintRequest () throws Exception {
68
+ public void printRequest () throws Exception {
75
69
this .request .addParameter ("param" , "paramValue" );
76
70
this .request .addHeader ("header" , "headerValue" );
77
71
@@ -91,14 +85,23 @@ public void testPrintRequest() throws Exception {
91
85
92
86
@ Test
93
87
@ SuppressWarnings ("deprecation" )
94
- public void testPrintResponse () throws Exception {
88
+ public void printResponse () throws Exception {
89
+ Cookie enigmaCookie = new Cookie ("enigma" , "42" );
90
+ enigmaCookie .setComment ("This is a comment" );
91
+ enigmaCookie .setHttpOnly (true );
92
+ enigmaCookie .setMaxAge (1234 );
93
+ enigmaCookie .setDomain (".example.com" );
94
+ enigmaCookie .setPath ("/crumbs" );
95
+ enigmaCookie .setSecure (true );
96
+
95
97
this .response .setStatus (400 , "error" );
96
98
this .response .addHeader ("header" , "headerValue" );
97
99
this .response .setContentType ("text/plain" );
98
100
this .response .getWriter ().print ("content" );
99
101
this .response .setForwardedUrl ("redirectFoo" );
100
102
this .response .sendRedirect ("/redirectFoo" );
101
103
this .response .addCookie (new Cookie ("cookie" , "cookieValue" ));
104
+ this .response .addCookie (enigmaCookie );
102
105
103
106
this .handler .handle (this .mvcResult );
104
107
@@ -107,33 +110,46 @@ public void testPrintResponse() throws Exception {
107
110
headers .setContentType (MediaType .TEXT_PLAIN );
108
111
headers .setLocation (new URI ("/redirectFoo" ));
109
112
110
- assertValue ("MockHttpServletResponse" , "Status" , this .response .getStatus ());
111
- assertValue ("MockHttpServletResponse" , "Error message" , response .getErrorMessage ());
112
- assertValue ("MockHttpServletResponse" , "Headers" , headers );
113
- assertValue ("MockHttpServletResponse" , "Content type" , this .response .getContentType ());
114
- assertValue ("MockHttpServletResponse" , "Body" , this .response .getContentAsString ());
115
- assertValue ("MockHttpServletResponse" , "Forwarded URL" , this .response .getForwardedUrl ());
116
- assertValue ("MockHttpServletResponse" , "Redirected URL" , this .response .getRedirectedUrl ());
113
+ String heading = "MockHttpServletResponse" ;
114
+ assertValue (heading , "Status" , this .response .getStatus ());
115
+ assertValue (heading , "Error message" , response .getErrorMessage ());
116
+ assertValue (heading , "Headers" , headers );
117
+ assertValue (heading , "Content type" , this .response .getContentType ());
118
+ assertValue (heading , "Body" , this .response .getContentAsString ());
119
+ assertValue (heading , "Forwarded URL" , this .response .getForwardedUrl ());
120
+ assertValue (heading , "Redirected URL" , this .response .getRedirectedUrl ());
121
+
122
+ Map <String , Map <String , Object >> printedValues = this .handler .getPrinter ().printedValues ;
123
+ String [] cookies = (String []) printedValues .get (heading ).get ("Cookies" );
124
+ assertEquals (2 , cookies .length );
125
+ String cookie1 = cookies [0 ];
126
+ String cookie2 = cookies [1 ];
127
+ assertTrue (cookie1 .startsWith ("[" + Cookie .class .getSimpleName ()));
128
+ assertTrue (cookie1 .contains ("name = 'cookie', value = 'cookieValue'" ));
129
+ assertTrue (cookie1 .endsWith ("]" ));
130
+ assertTrue (cookie2 .startsWith ("[" + Cookie .class .getSimpleName ()));
131
+ assertTrue (cookie2 .contains ("name = 'enigma', value = '42', comment = 'This is a comment', domain = '.example.com', maxAge = 1234, path = '/crumbs', secure = true, version = 0, httpOnly = true" ));
132
+ assertTrue (cookie2 .endsWith ("]" ));
117
133
}
118
134
119
135
@ Test
120
- public void testPrintHandlerNull () throws Exception {
136
+ public void printHandlerNull () throws Exception {
121
137
StubMvcResult mvcResult = new StubMvcResult (this .request , null , null , null , null , null , this .response );
122
138
this .handler .handle (mvcResult );
123
139
124
140
assertValue ("Handler" , "Type" , null );
125
141
}
126
142
127
143
@ Test
128
- public void testPrintHandler () throws Exception {
144
+ public void printHandler () throws Exception {
129
145
this .mvcResult .setHandler (new Object ());
130
146
this .handler .handle (this .mvcResult );
131
147
132
148
assertValue ("Handler" , "Type" , Object .class .getName ());
133
149
}
134
150
135
151
@ Test
136
- public void testPrintHandlerMethod () throws Exception {
152
+ public void printHandlerMethod () throws Exception {
137
153
HandlerMethod handlerMethod = new HandlerMethod (this , "handle" );
138
154
this .mvcResult .setHandler (handlerMethod );
139
155
this .handler .handle (mvcResult );
@@ -143,22 +159,22 @@ public void testPrintHandlerMethod() throws Exception {
143
159
}
144
160
145
161
@ Test
146
- public void testResolvedExceptionNull () throws Exception {
162
+ public void resolvedExceptionNull () throws Exception {
147
163
this .handler .handle (this .mvcResult );
148
164
149
165
assertValue ("Resolved Exception" , "Type" , null );
150
166
}
151
167
152
168
@ Test
153
- public void testResolvedException () throws Exception {
169
+ public void resolvedException () throws Exception {
154
170
this .mvcResult .setResolvedException (new Exception ());
155
171
this .handler .handle (this .mvcResult );
156
172
157
173
assertValue ("Resolved Exception" , "Type" , Exception .class .getName ());
158
174
}
159
175
160
176
@ Test
161
- public void testModelAndViewNull () throws Exception {
177
+ public void modelAndViewNull () throws Exception {
162
178
this .handler .handle (this .mvcResult );
163
179
164
180
assertValue ("ModelAndView" , "View name" , null );
@@ -167,7 +183,7 @@ public void testModelAndViewNull() throws Exception {
167
183
}
168
184
169
185
@ Test
170
- public void testModelAndView () throws Exception {
186
+ public void modelAndView () throws Exception {
171
187
BindException bindException = new BindException (new Object (), "target" );
172
188
bindException .reject ("errorCode" );
173
189
@@ -186,14 +202,14 @@ public void testModelAndView() throws Exception {
186
202
}
187
203
188
204
@ Test
189
- public void testFlashMapNull () throws Exception {
205
+ public void flashMapNull () throws Exception {
190
206
this .handler .handle (mvcResult );
191
207
192
208
assertValue ("FlashMap" , "Type" , null );
193
209
}
194
210
195
211
@ Test
196
- public void testFlashMap () throws Exception {
212
+ public void flashMap () throws Exception {
197
213
FlashMap flashMap = new FlashMap ();
198
214
flashMap .put ("attrName" , "attrValue" );
199
215
this .request .setAttribute (DispatcherServlet .class .getName () + ".OUTPUT_FLASH_MAP" , flashMap );
0 commit comments