@@ -59,6 +59,7 @@ class HtmxRequestHandling {
5959 @ BeforeEach
6060 void setUp () throws IOException {
6161 when (request .getHeader ("HX-Request" )).thenReturn ("true" );
62+ when (request .getContextPath ()).thenReturn ("" );
6263 responseBody = new StringWriter ();
6364 when (response .getWriter ()).thenReturn (new PrintWriter (responseBody ));
6465 }
@@ -137,6 +138,7 @@ void shouldNotCallDelegateWhenHtmxRequestReceived() throws IOException, ServletE
137138 void shouldHandleHxRequestHeaderCaseInsensitively () throws IOException , ServletException {
138139 // Given
139140 when (request .getHeader ("HX-Request" )).thenReturn ("TRUE" );
141+ when (request .getContextPath ()).thenReturn ("" );
140142 AuthenticationException authException = new InsufficientAuthenticationException ("Session expired" );
141143
142144 // When
@@ -148,6 +150,63 @@ void shouldHandleHxRequestHeaderCaseInsensitively() throws IOException, ServletE
148150 }
149151 }
150152
153+ @ Nested
154+ @ DisplayName ("Servlet Context Path Handling" )
155+ class ServletContextPathHandling {
156+
157+ private StringWriter responseBody ;
158+
159+ @ BeforeEach
160+ void setUp () throws IOException {
161+ when (request .getHeader ("HX-Request" )).thenReturn ("true" );
162+ responseBody = new StringWriter ();
163+ when (response .getWriter ()).thenReturn (new PrintWriter (responseBody ));
164+ }
165+
166+ @ Test
167+ @ DisplayName ("Should prepend context path to HX-Redirect header when context path is non-empty" )
168+ void shouldPrependContextPathToHxRedirectWhenContextPathIsNonEmpty () throws IOException , ServletException {
169+ // Given
170+ when (request .getContextPath ()).thenReturn ("/app" );
171+ AuthenticationException authException = new InsufficientAuthenticationException ("Session expired" );
172+
173+ // When
174+ entryPoint .commence (request , response , authException );
175+
176+ // Then
177+ verify (response ).setHeader ("HX-Redirect" , "/app" + LOGIN_URL );
178+ }
179+
180+ @ Test
181+ @ DisplayName ("Should include context path in JSON loginUrl when context path is non-empty" )
182+ void shouldIncludeContextPathInJsonLoginUrlWhenContextPathIsNonEmpty () throws IOException , ServletException {
183+ // Given
184+ when (request .getContextPath ()).thenReturn ("/app" );
185+ AuthenticationException authException = new InsufficientAuthenticationException ("Session expired" );
186+
187+ // When
188+ entryPoint .commence (request , response , authException );
189+
190+ // Then
191+ assertThat (responseBody .toString ()).contains ("\" loginUrl\" :\" /app" + LOGIN_URL + "\" " );
192+ }
193+
194+ @ Test
195+ @ DisplayName ("Should use login URL as-is when context path is empty" )
196+ void shouldUseLoginUrlAsIsWhenContextPathIsEmpty () throws IOException , ServletException {
197+ // Given
198+ when (request .getContextPath ()).thenReturn ("" );
199+ AuthenticationException authException = new InsufficientAuthenticationException ("Session expired" );
200+
201+ // When
202+ entryPoint .commence (request , response , authException );
203+
204+ // Then
205+ verify (response ).setHeader ("HX-Redirect" , LOGIN_URL );
206+ assertThat (responseBody .toString ()).contains ("\" loginUrl\" :\" " + LOGIN_URL + "\" " );
207+ }
208+ }
209+
151210 @ Nested
152211 @ DisplayName ("Non-HTMX Request Handling" )
153212 class NonHtmxRequestHandling {
0 commit comments