@@ -49,12 +49,8 @@ void setUp() throws IOException {
4949 when (response .getWriter ()).thenReturn (new PrintWriter (responseWriter ));
5050 }
5151
52- private void initFilter (String allowedOrigins , String serverApiKey , boolean allowNoOriginForSSR ) {
53- filter = new OriginVerificationFilter (serverApiKey , allowedOrigins , allowNoOriginForSSR );
54- }
55-
5652 private void initFilter (String allowedOrigins , String serverApiKey ) {
57- initFilter ( allowedOrigins , serverApiKey , true );
53+ filter = new OriginVerificationFilter ( serverApiKey , allowedOrigins );
5854 }
5955
6056 @ Nested
@@ -82,13 +78,13 @@ void testInitialize_WithEmptyOrigins_ShouldHandleGracefully() {
8278 class NonProtectedEndpointTests {
8379
8480 @ Test
85- @ DisplayName ("Should allow requests to non-protected endpoints" )
86- void testDoFilter_NonProtectedEndpoint_ShouldAllow () throws Exception {
81+ @ DisplayName ("Should block requests to non-excluded endpoints without authorization " )
82+ void testDoFilter_NonProtectedEndpoint_ShouldBlock () throws Exception {
8783 initFilter ("https://example.com" , "test-key" );
8884 when (request .getRequestURI ()).thenReturn ("/api/health" );
8985 filter .doFilter (request , response , filterChain );
90- verify (filterChain ).doFilter (request , response );
91- verify (response , never ()) .setStatus (anyInt ());
86+ verify (filterChain , never () ).doFilter (request , response );
87+ verify (response ) .setStatus (HttpStatus . FORBIDDEN . value ());
9288 }
9389
9490 @ Test
@@ -152,9 +148,9 @@ void testDoFilter_WithValidApiKey_ShouldAllow() throws Exception {
152148 }
153149
154150 @ Test
155- @ DisplayName ("Should block invalid API key when SSR is disabled " )
151+ @ DisplayName ("Should block invalid API key" )
156152 void testDoFilter_WithInvalidApiKey_SSRDisabled_ShouldBlock () throws Exception {
157- initFilter ("https://example.com" , "test-secret-key" , false );
153+ initFilter ("https://example.com" , "test-secret-key" );
158154 when (request .getRequestURI ()).thenReturn ("/blogs/published" );
159155 when (request .getMethod ()).thenReturn ("GET" );
160156 when (request .getHeader ("X-API-Key" )).thenReturn ("wrong-key" );
@@ -195,9 +191,9 @@ void testDoFilter_WithValidReferer_ShouldAllow() throws Exception {
195191 }
196192
197193 @ Test
198- @ DisplayName ("Should block invalid referer when SSR is disabled " )
194+ @ DisplayName ("Should block invalid referer" )
199195 void testDoFilter_WithInvalidReferer_SSRDisabled_ShouldBlock () throws Exception {
200- initFilter ("https://example.com" , "test-key" , false );
196+ initFilter ("https://example.com" , "test-key" );
201197 when (request .getRequestURI ()).thenReturn ("/blogs/published" );
202198 when (request .getMethod ()).thenReturn ("GET" );
203199 when (request .getHeader ("Referer" )).thenReturn ("https://malicious.com/hack" );
@@ -234,25 +230,13 @@ void testDoFilter_OriginPrecedence_ShouldUseOrigin() throws Exception {
234230 }
235231
236232 @ Nested
237- @ DisplayName ("SSR Detection Tests" )
238- class SSRDetectionTests {
239-
240- @ Test
241- @ DisplayName ("Should allow SSR requests (no Origin) when SSR is enabled" )
242- void testDoFilter_NoOrigin_SSREnabled_ShouldAllow () throws Exception {
243- initFilter ("https://example.com" , "test-key" , true );
244- when (request .getRequestURI ()).thenReturn ("/blogs/published" );
245- when (request .getMethod ()).thenReturn ("GET" );
246- when (request .getRemoteAddr ()).thenReturn ("127.0.0.1" );
247- filter .doFilter (request , response , filterChain );
248- verify (filterChain ).doFilter (request , response );
249- verify (response , never ()).setStatus (anyInt ());
250- }
233+ @ DisplayName ("Missing Origin Header Tests" )
234+ class MissingOriginHeaderTests {
251235
252236 @ Test
253- @ DisplayName ("Should block requests without Origin when SSR is disabled " )
254- void testDoFilter_NoOrigin_SSRDisabled_ShouldBlock () throws Exception {
255- initFilter ("https://example.com" , "test-key" , false );
237+ @ DisplayName ("Should block requests without Origin header " )
238+ void testDoFilter_NoOriginHeader_ShouldBlock () throws Exception {
239+ initFilter ("https://example.com" , "test-key" );
256240 when (request .getRequestURI ()).thenReturn ("/blogs/published" );
257241 when (request .getMethod ()).thenReturn ("GET" );
258242 filter .doFilter (request , response , filterChain );
@@ -261,9 +245,9 @@ void testDoFilter_NoOrigin_SSRDisabled_ShouldBlock() throws Exception {
261245 }
262246
263247 @ Test
264- @ DisplayName ("Browser request with invalid Origin should still be blocked even with SSR enabled " )
248+ @ DisplayName ("Should block requests with invalid Origin header " )
265249 void testDoFilter_BrowserWithInvalidOrigin_SSREnabled_ShouldBlock () throws Exception {
266- initFilter ("https://example.com" , "test-key" , true );
250+ initFilter ("https://example.com" , "test-key" );
267251 when (request .getRequestURI ()).thenReturn ("/blogs/published" );
268252 when (request .getMethod ()).thenReturn ("GET" );
269253 when (request .getHeader ("Origin" )).thenReturn ("https://malicious.com" );
0 commit comments