11/*
2- * Copyright 2002-2014 the original author or authors.
2+ * Copyright 2002-2015 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1919import java .net .URI ;
2020import java .util .HashMap ;
2121import java .util .Map ;
22+
2223import javax .servlet .http .Cookie ;
2324
24- import org .junit .Before ;
2525import org .junit .Test ;
2626
2727import org .springframework .http .HttpHeaders ;
4545 * Tests for {@link PrintingResultHandler}.
4646 *
4747 * @author Rossen Stoyanchev
48+ * @author Sam Brannen
4849 */
4950public class PrintingResultHandlerTests {
5051
51- private TestPrintingResultHandler handler ;
52-
53- private MockHttpServletRequest request ;
52+ private final TestPrintingResultHandler handler = new TestPrintingResultHandler ();
5453
55- private MockHttpServletResponse response ;
54+ private final MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/" ) {
55+ @ Override
56+ public boolean isAsyncStarted () {
57+ return false ;
58+ }
59+ };
5660
57- private StubMvcResult mvcResult ;
61+ private final MockHttpServletResponse response = new MockHttpServletResponse () ;
5862
63+ private final StubMvcResult mvcResult = new StubMvcResult (this .request , null , null ,
64+ null , null , null , this .response );
5965
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- }
7266
7367 @ Test
74- public void testPrintRequest () throws Exception {
68+ public void printRequest () throws Exception {
7569 this .request .addParameter ("param" , "paramValue" );
7670 this .request .addHeader ("header" , "headerValue" );
7771
@@ -91,14 +85,23 @@ public void testPrintRequest() throws Exception {
9185
9286 @ Test
9387 @ 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+
9597 this .response .setStatus (400 , "error" );
9698 this .response .addHeader ("header" , "headerValue" );
9799 this .response .setContentType ("text/plain" );
98100 this .response .getWriter ().print ("content" );
99101 this .response .setForwardedUrl ("redirectFoo" );
100102 this .response .sendRedirect ("/redirectFoo" );
101103 this .response .addCookie (new Cookie ("cookie" , "cookieValue" ));
104+ this .response .addCookie (enigmaCookie );
102105
103106 this .handler .handle (this .mvcResult );
104107
@@ -107,33 +110,46 @@ public void testPrintResponse() throws Exception {
107110 headers .setContentType (MediaType .TEXT_PLAIN );
108111 headers .setLocation (new URI ("/redirectFoo" ));
109112
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 ("]" ));
117133 }
118134
119135 @ Test
120- public void testPrintHandlerNull () throws Exception {
136+ public void printHandlerNull () throws Exception {
121137 StubMvcResult mvcResult = new StubMvcResult (this .request , null , null , null , null , null , this .response );
122138 this .handler .handle (mvcResult );
123139
124140 assertValue ("Handler" , "Type" , null );
125141 }
126142
127143 @ Test
128- public void testPrintHandler () throws Exception {
144+ public void printHandler () throws Exception {
129145 this .mvcResult .setHandler (new Object ());
130146 this .handler .handle (this .mvcResult );
131147
132148 assertValue ("Handler" , "Type" , Object .class .getName ());
133149 }
134150
135151 @ Test
136- public void testPrintHandlerMethod () throws Exception {
152+ public void printHandlerMethod () throws Exception {
137153 HandlerMethod handlerMethod = new HandlerMethod (this , "handle" );
138154 this .mvcResult .setHandler (handlerMethod );
139155 this .handler .handle (mvcResult );
@@ -143,22 +159,22 @@ public void testPrintHandlerMethod() throws Exception {
143159 }
144160
145161 @ Test
146- public void testResolvedExceptionNull () throws Exception {
162+ public void resolvedExceptionNull () throws Exception {
147163 this .handler .handle (this .mvcResult );
148164
149165 assertValue ("Resolved Exception" , "Type" , null );
150166 }
151167
152168 @ Test
153- public void testResolvedException () throws Exception {
169+ public void resolvedException () throws Exception {
154170 this .mvcResult .setResolvedException (new Exception ());
155171 this .handler .handle (this .mvcResult );
156172
157173 assertValue ("Resolved Exception" , "Type" , Exception .class .getName ());
158174 }
159175
160176 @ Test
161- public void testModelAndViewNull () throws Exception {
177+ public void modelAndViewNull () throws Exception {
162178 this .handler .handle (this .mvcResult );
163179
164180 assertValue ("ModelAndView" , "View name" , null );
@@ -167,7 +183,7 @@ public void testModelAndViewNull() throws Exception {
167183 }
168184
169185 @ Test
170- public void testModelAndView () throws Exception {
186+ public void modelAndView () throws Exception {
171187 BindException bindException = new BindException (new Object (), "target" );
172188 bindException .reject ("errorCode" );
173189
@@ -186,14 +202,14 @@ public void testModelAndView() throws Exception {
186202 }
187203
188204 @ Test
189- public void testFlashMapNull () throws Exception {
205+ public void flashMapNull () throws Exception {
190206 this .handler .handle (mvcResult );
191207
192208 assertValue ("FlashMap" , "Type" , null );
193209 }
194210
195211 @ Test
196- public void testFlashMap () throws Exception {
212+ public void flashMap () throws Exception {
197213 FlashMap flashMap = new FlashMap ();
198214 flashMap .put ("attrName" , "attrValue" );
199215 this .request .setAttribute (DispatcherServlet .class .getName () + ".OUTPUT_FLASH_MAP" , flashMap );
0 commit comments