@@ -49,61 +49,104 @@ public class SlingHttpServletResponseImplTest {
49
49
50
50
private static final String ACTIVE_SERVLET_NAME = "activeServlet" ;
51
51
String [] logMessages = {
52
- "0 TIMER_START{Request Processing}" ,
53
- "6 COMMENT timer_end format is {<elapsed microseconds>,<timer name>} <optional message>" ,
54
- "17 LOG Method=GET, PathInfo=null" ,
55
- "20 TIMER_START{handleSecurity}" ,
56
- "2104 TIMER_END{2081,handleSecurity} authenticator org.apache.sling.auth.core.impl.SlingAuthenticator@6367091e returns true" ,
57
- "2478 TIMER_START{ResourceResolution}" ,
58
- "2668 TIMER_END{189,ResourceResolution} URI=/content/slingshot.html resolves to Resource=JcrNodeResource, type=slingshot/Home, superType=null, path=/content/slingshot" ,
59
- "2678 LOG Resource Path Info: SlingRequestPathInfo: path='/content/slingshot', selectorString='null', extension='html', suffix='null'" ,
60
- "2678 TIMER_START{ServletResolution}" ,
61
- "2683 TIMER_START{resolveServlet(/content/slingshot)}" ,
62
- "3724 TIMER_END{1040,resolveServlet(/content/slingshot)} Using servlet /libs/slingshot/Home/html.jsp" ,
63
- "3727 TIMER_END{1047,ServletResolution} URI=/content/slingshot.html handled by Servlet=/libs/slingshot/Home/html.jsp" ,
64
- "3736 LOG Applying REQUESTfilters" ,
65
- "3751 LOG Calling filter: com.composum.sling.nodes.mount.remote.RemoteRequestFilter" ,
66
- "4722 TIMER_START{/libs/slingshot/Component/head.html.jsp#1}" ,
67
- "3757 LOG Calling filter: org.apache.sling.i18n.impl.I18NFilter" ,
68
- "4859 TIMER_END{135,/libs/slingshot/Component/head.html.jsp#1}" ,
69
- "3765 LOG Calling filter: org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter" ,
70
- "2678 TIMER_START{ServletResolution}" ,
71
- "2683 TIMER_START{resolveServlet(/content/slingshot)}" ,
72
- "2678 TIMER_START{ServletResolution}" ,
73
- "2683 TIMER_START{resolveServlet(/content/slingshot)}" ,
74
- "3724 TIMER_END{1040,resolveServlet(/content/slingshot)} Using servlet /libs/slingshot/Home/html.jsp" ,
75
- "3727 TIMER_END{1047,ServletResolution} URI=/content/slingshot.html handled by Servlet=/libs/slingshot/Home/html.jsp" ,
76
- "3724 TIMER_END{1040,resolveServlet(/content/slingshot)} Using servlet /libs/slingshot/Home/html.jsp" ,
77
- "3727 TIMER_END{1047,ServletResolution} URI=/content/slingshot.html handled by Servlet=/libs/slingshot/Home/html.jsp" ,
78
- "3774 LOG Applying Componentfilters" ,
79
- "3797 TIMER_START{/libs/slingshot/Home/html.jsp#0}" ,
80
- "3946 LOG Adding bindings took 18 microseconds" ,
81
- "4405 LOG Including resource JcrNodeResource, type=slingshot/Home, superType=null, path=/content/slingshot (SlingRequestPathInfo: path='/content/slingshot', selectorString='head', extension='html', suffix='null')" ,
82
- "4414 TIMER_START{resolveServlet(/content/slingshot)}" ,
83
- "4670 TIMER_END{253,resolveServlet(/content/slingshot)} Using servlet /libs/slingshot/Component/head.html.jsp" ,
84
- "4673 LOG Applying Includefilters" ,
85
- "4722 TIMER_START{/libs/slingshot/Component/head.html.jsp#1}" ,
86
- "4749 LOG Adding bindings took 4 microseconds"
52
+ "0 TIMER_START{Request Processing}" ,
53
+ "6 COMMENT timer_end format is {<elapsed microseconds>,<timer name>} <optional message>" ,
54
+ "17 LOG Method=GET, PathInfo=null" ,
55
+ "20 TIMER_START{handleSecurity}" ,
56
+ "2104 TIMER_END{2081,handleSecurity} authenticator org.apache.sling.auth.core.impl.SlingAuthenticator@6367091e returns true" ,
57
+ "2478 TIMER_START{ResourceResolution}" ,
58
+ "2668 TIMER_END{189,ResourceResolution} URI=/content/slingshot.html resolves to Resource=JcrNodeResource, type=slingshot/Home, superType=null, path=/content/slingshot" ,
59
+ "2678 LOG Resource Path Info: SlingRequestPathInfo: path='/content/slingshot', selectorString='null', extension='html', suffix='null'" ,
60
+ "2678 TIMER_START{ServletResolution}" ,
61
+ "2683 TIMER_START{resolveServlet(/content/slingshot)}" ,
62
+ "3724 TIMER_END{1040,resolveServlet(/content/slingshot)} Using servlet /libs/slingshot/Home/html.jsp" ,
63
+ "3727 TIMER_END{1047,ServletResolution} URI=/content/slingshot.html handled by Servlet=/libs/slingshot/Home/html.jsp" ,
64
+ "3736 LOG Applying REQUESTfilters" ,
65
+ "3751 LOG Calling filter: com.composum.sling.nodes.mount.remote.RemoteRequestFilter" ,
66
+ "4722 TIMER_START{/libs/slingshot/Component/head.html.jsp#1}" ,
67
+ "3757 LOG Calling filter: org.apache.sling.i18n.impl.I18NFilter" ,
68
+ "4859 TIMER_END{135,/libs/slingshot/Component/head.html.jsp#1}" ,
69
+ "3765 LOG Calling filter: org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter" ,
70
+ "2678 TIMER_START{ServletResolution}" ,
71
+ "2683 TIMER_START{resolveServlet(/content/slingshot)}" ,
72
+ "2678 TIMER_START{ServletResolution}" ,
73
+ "2683 TIMER_START{resolveServlet(/content/slingshot)}" ,
74
+ "3724 TIMER_END{1040,resolveServlet(/content/slingshot)} Using servlet /libs/slingshot/Home/html.jsp" ,
75
+ "3727 TIMER_END{1047,ServletResolution} URI=/content/slingshot.html handled by Servlet=/libs/slingshot/Home/html.jsp" ,
76
+ "3724 TIMER_END{1040,resolveServlet(/content/slingshot)} Using servlet /libs/slingshot/Home/html.jsp" ,
77
+ "3727 TIMER_END{1047,ServletResolution} URI=/content/slingshot.html handled by Servlet=/libs/slingshot/Home/html.jsp" ,
78
+ "3774 LOG Applying Componentfilters" ,
79
+ "3797 TIMER_START{/libs/slingshot/Home/html.jsp#0}" ,
80
+ "3946 LOG Adding bindings took 18 microseconds" ,
81
+ "4405 LOG Including resource JcrNodeResource, type=slingshot/Home, superType=null, path=/content/slingshot (SlingRequestPathInfo: path='/content/slingshot', selectorString='head', extension='html', suffix='null')" ,
82
+ "4414 TIMER_START{resolveServlet(/content/slingshot)}" ,
83
+ "4670 TIMER_END{253,resolveServlet(/content/slingshot)} Using servlet /libs/slingshot/Component/head.html.jsp" ,
84
+ "4673 LOG Applying Includefilters" ,
85
+ "4722 TIMER_START{/libs/slingshot/Component/head.html.jsp#1}" ,
86
+ "4749 LOG Adding bindings took 4 microseconds"
87
87
};
88
88
89
89
@ Test
90
- public void testNoViolationChecksOnCommitedResponse () {
90
+ public void testNoViolationChecksOnCommittedResponseWhenSendRedirect () throws IOException {
91
91
final SlingJakartaHttpServletResponse orig = Mockito .mock (SlingJakartaHttpServletResponse .class );
92
92
Mockito .when (orig .isCommitted ()).thenReturn (true );
93
93
94
94
final RequestData requestData = mock (RequestData .class );
95
95
final DispatchingInfo info = new DispatchingInfo (DispatcherType .INCLUDE );
96
96
when (requestData .getDispatchingInfo ()).thenReturn (info );
97
- info .setProtectHeadersOnInclude (true );
98
97
99
98
final SlingJakartaHttpServletResponseImpl include = new SlingJakartaHttpServletResponseImpl (requestData , orig );
100
99
SlingJakartaHttpServletResponseImpl spyInclude = Mockito .spy (include );
101
100
101
+ spyInclude .sendRedirect ("somewhere" );
102
+
103
+ spyInclude .setContentType ("someOtherType" );
104
+ Mockito .verify (orig , times (1 )).setContentType (Mockito .any ());
105
+ Mockito .verify (spyInclude , never ()).checkContentTypeOverride (Mockito .any ());
106
+ }
107
+
108
+ @ Test
109
+ public void testNoViolationChecksOnCommittedResponseWhenSendError () throws IOException {
110
+ final SlingJakartaHttpServletResponse orig = Mockito .mock (SlingJakartaHttpServletResponse .class );
111
+
112
+ final RequestData requestData = mock (RequestData .class );
113
+ final DispatchingInfo info = new DispatchingInfo (DispatcherType .INCLUDE );
114
+ when (requestData .getDispatchingInfo ()).thenReturn (info );
115
+ when (requestData .getSlingRequestProcessor ()).thenReturn (mock (SlingRequestProcessorImpl .class ));
116
+
117
+ final SlingJakartaHttpServletResponseImpl include = new SlingJakartaHttpServletResponseImpl (requestData , orig );
118
+ SlingJakartaHttpServletResponseImpl spyInclude = Mockito .spy (include );
119
+
120
+ spyInclude .sendError (501 );
121
+ // send error will eventually commit the response, let's mock this
122
+ Mockito .when (orig .isCommitted ()).thenReturn (true );
123
+
102
124
spyInclude .setContentType ("someOtherType" );
103
125
Mockito .verify (orig , times (1 )).setContentType (Mockito .any ());
104
126
Mockito .verify (spyInclude , never ()).checkContentTypeOverride (Mockito .any ());
105
127
}
106
128
129
+ @ Test
130
+ public void testViolationChecksOnCommittedResponses () throws IOException {
131
+ final SlingJakartaHttpServletResponse orig = Mockito .mock (SlingJakartaHttpServletResponse .class );
132
+ Mockito .when (orig .isCommitted ()).thenReturn (true );
133
+
134
+ final RequestData requestData = mock (RequestData .class );
135
+ final DispatchingInfo info = new DispatchingInfo (DispatcherType .INCLUDE );
136
+ when (requestData .getDispatchingInfo ()).thenReturn (info );
137
+ when (requestData .getSlingRequestProcessor ()).thenReturn (mock (SlingRequestProcessorImpl .class ));
138
+ final RequestProgressTracker rpt = mock (RequestProgressTracker .class );
139
+ when (rpt .getMessages ()).thenReturn (new ArrayList <String >().iterator ());
140
+ when (requestData .getRequestProgressTracker ()).thenReturn (rpt );
141
+
142
+ final SlingJakartaHttpServletResponseImpl include = new SlingJakartaHttpServletResponseImpl (requestData , orig );
143
+ SlingJakartaHttpServletResponseImpl spyInclude = Mockito .spy (include );
144
+
145
+ spyInclude .setContentType ("someOtherType" );
146
+ Mockito .verify (orig , times (1 )).setContentType (Mockito .any ());
147
+ Mockito .verify (spyInclude , Mockito .times (1 )).checkContentTypeOverride (Mockito .any ());
148
+ }
149
+
107
150
@ Test
108
151
public void testReset () {
109
152
final SlingJakartaHttpServletResponse originalResponse = mock (SlingJakartaHttpServletResponse .class );
@@ -112,8 +155,8 @@ public void testReset() {
112
155
when (requestData .getDispatchingInfo ()).thenReturn (dispatchingInfo );
113
156
dispatchingInfo .setProtectHeadersOnInclude (true );
114
157
115
- final HttpServletResponse includeResponse =
116
- new SlingJakartaHttpServletResponseImpl ( requestData , originalResponse );
158
+ final HttpServletResponse includeResponse = new SlingJakartaHttpServletResponseImpl ( requestData ,
159
+ originalResponse );
117
160
118
161
when (originalResponse .isCommitted ()).thenReturn (false );
119
162
includeResponse .reset ();
@@ -134,8 +177,8 @@ public void testResetOnError() {
134
177
135
178
// Simulate an error dispatching scenario on a uncommitted response
136
179
DispatchingInfo dispatchingInfo = new DispatchingInfo (DispatcherType .ERROR );
137
- final HttpServletResponse includeResponse =
138
- new SlingJakartaHttpServletResponseImpl ( requestData , originalResponse );
180
+ final HttpServletResponse includeResponse = new SlingJakartaHttpServletResponseImpl ( requestData ,
181
+ originalResponse );
139
182
dispatchingInfo .setProtectHeadersOnInclude (true );
140
183
when (requestData .getDispatchingInfo ()).thenReturn (dispatchingInfo );
141
184
when (originalResponse .isCommitted ()).thenReturn (false );
0 commit comments