bmarwell commented on code in PR #2372:
URL: https://github.com/apache/shiro/pull/2372#discussion_r2545481416


##########
web/src/test/groovy/org/apache/shiro/web/filter/authc/BearerHttpFilterAuthenticationTest.groovy:
##########
@@ -137,6 +136,43 @@ class BearerHttpFilterAuthenticationTest extends 
SecurityManagerTestSupport {
         })
     }
 
+    @Test
+    void allowedPreflightRequestsAndOptionsRequest() {
+        BearerHttpAuthenticationFilter testFilter = new 
BearerHttpAuthenticationFilter()
+        testFilter.setAllowPreFlightRequests(true)
+
+        HttpServletRequest request = createMock(HttpServletRequest)
+        expect(request.getMethod()).andReturn("OPTIONS")
+        expect(request.getHeader("Origin")).andReturn("localhost")
+        
expect(request.getHeader("Access-Control-Request-Method")).andReturn("GET,OPTIONS")
+
+        replay(request)
+
+        HttpServletResponse response = mockResponse()
+
+        runWithSubject({
+            String[] methods = ["OPTIONS"]
+            boolean accessAllowed = testFilter.isAccessAllowed(request, 
response, methods)
+            assertThat("Access allowed for OPTIONS", accessAllowed)
+        })
+    }
+
+    @Test
+    void notAllowedPreFlightRequests() {
+        BearerHttpAuthenticationFilter testFilter = new 
BearerHttpAuthenticationFilter()
+
+        HttpServletRequest request = mockRequest("valid-token", "localhost", {
+            expect(it.getMethod()).andReturn("OPTIONS")
+        })
+        HttpServletResponse response = mockResponse()
+
+        runWithSubject({
+            String[] methods = ["OPTIONS"]
+            boolean accessAllowed = testFilter.isAccessAllowed(request, 
response, methods)
+            assertThat("Access not allowed for OPTIONS", !accessAllowed)
+        })
+    }
+

Review Comment:
   I am pretty sure we agreed on using mockito + assertj for new tests?



##########
web/src/test/java/org/apache/shiro/web/util/CorsUtilsTest.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.shiro.web.util;
+
+import org.junit.jupiter.api.Test;
+
+import javax.servlet.http.HttpServletRequest;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class CorsUtilsTest {
+
+    @Test
+    void isPreFlightRequestTest() {
+        HttpServletRequest request = mock(HttpServletRequest.class);
+
+        when(request.getMethod()).thenReturn("OPTIONS");
+        when(request.getHeader("Origin")).thenReturn("https://kaboom.com";);
+        
when(request.getHeader("Access-Control-Request-Method")).thenReturn("OPTIONS");
+
+        assertThat(CorsUtils.isPreFlightRequest(request)).isTrue();
+    }
+
+    @Test
+    void isNotOptionsRequestTest() {
+        HttpServletRequest request = mock(HttpServletRequest.class);
+
+        when(request.getMethod()).thenReturn("GET");
+
+        assertThat(CorsUtils.isPreFlightRequest(request)).isFalse();

Review Comment:
   Here we use assertj 👍🏻



##########
web/src/test/java/org/apache/shiro/web/filter/authc/BasicHttpFilterAuthenticationTest.java:
##########
@@ -290,6 +290,40 @@ public void 
httpMethodRequiresAuthenticationWithPermissive() throws Exception {
         });
     }
 
+    @Test
+    public void allowedPreFlightRequestsAndOptionsRequest() {
+        testFilter = new BasicHttpAuthenticationFilter();
+        testFilter.setAllowPreFlightRequests(true);
+
+        HttpServletRequest request = mock(HttpServletRequest.class);
+        HttpServletResponse response = mock(HttpServletResponse.class);
+
+        when(request.getMethod()).thenReturn("OPTIONS");
+        when(request.getHeader("Origin")).thenReturn("https://kaboom.com";);
+        
when(request.getHeader("Access-Control-Request-Method")).thenReturn("GET,OPTIONS");

Review Comment:
   Here we use mockito! 👍🏻



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to