浏览代码

work on unit test, still some functionality to cover later

Colin Sampaleanu 21 年之前
父节点
当前提交
20025da7c7

+ 25 - 20
core/src/main/java/org/acegisecurity/ui/webapp/AuthenticationProcessingFilterEntryPoint.java

@@ -49,16 +49,16 @@ public class AuthenticationProcessingFilterEntryPoint
      */
     private String loginFormUrl;
     
-    private boolean forceSsl = false;
+    private boolean forceHttps = false;
     
-    private HashMap sslPortMapping;
+    private HashMap httpsPortMapping;
 
     //~ Methods ================================================================
     
     public AuthenticationProcessingFilterEntryPoint() {
-        sslPortMapping = new HashMap();
-        sslPortMapping.put(new Integer(80), new Integer(443));
-        sslPortMapping.put(new Integer(8080), new Integer(8443));
+        httpsPortMapping = new HashMap();
+        httpsPortMapping.put(new Integer(80), new Integer(443));
+        httpsPortMapping.put(new Integer(8080), new Integer(8443));
     }
 
     public void setLoginFormUrl(String loginFormUrl) {
@@ -83,9 +83,9 @@ public class AuthenticationProcessingFilterEntryPoint
         
         String redirectUrl =  contextPath + loginFormUrl;
         
-        if (forceSsl && req.getScheme().equals("http")) {
+        if (forceHttps && req.getScheme().equals("http")) {
             Integer httpPort = new Integer(req.getServerPort());
-            Integer httpsPort = (Integer) sslPortMapping.get(httpPort);
+            Integer httpsPort = (Integer) httpsPortMapping.get(httpPort);
             if (httpsPort != null ) {
                 String serverName = req.getServerName();
                 redirectUrl = "https://" + serverName + ":" + httpsPort + contextPath
@@ -96,35 +96,40 @@ public class AuthenticationProcessingFilterEntryPoint
         ((HttpServletResponse) response).sendRedirect(redirectUrl);
     }
     
-    public void setForceSsl(boolean forceSsl) {
-        this.forceSsl = forceSsl;
+    public void setForceHttps(boolean forceSsl) {
+        this.forceHttps = forceSsl;
     }
-    public boolean isForceSsl() {
-        return forceSsl;
+    public boolean getForceHttps() {
+        return forceHttps;
     }
 
     /**
      * @throws IllegalArgumentException if input map does not consist of String keys
      * and values, each representing an integer port number for one mapping.
      */
-    public void setSslPortMapping(HashMap sslPortMapping) {
-        this.sslPortMapping.clear();
-        Iterator it = sslPortMapping.entrySet().iterator();
+    public void setHttpsPortMapping(HashMap newMappings) {
+        httpsPortMapping.clear();
+        Iterator it = newMappings.entrySet().iterator();
         while (it.hasNext()) {
             Map.Entry entry = (Map.Entry) it.next();
             Integer httpPort = new Integer((String)entry.getKey());
-            Integer httpsPort = new Integer((String)entry.getKey());
+            Integer httpsPort = new Integer((String)entry.getValue());
             if (httpPort.intValue() < 1 || httpPort.intValue() > 65535 ||
                     httpsPort.intValue() < 1 || httpsPort.intValue() > 65535)
                 throw new IllegalArgumentException("one or both ports out of legal range: "
                         + httpPort + ", " + httpsPort);
-            sslPortMapping.put(httpPort, httpsPort);
-            if (sslPortMapping.size() < 1)
-                throw new IllegalArgumentException("Must map at least one port");
+            httpsPortMapping.put(httpPort, httpsPort);
+            if (httpsPortMapping.size() < 1)
+                throw new IllegalArgumentException("must map at least one port");
         }
         
     }
-    public HashMap getSslPortMapping() {
-        return sslPortMapping;
+    
+    /**
+     * Returns the translated (Integer -> Integer) version of the original port
+     * mapping specified via setHttpsPortMapping() 
+     */
+    protected HashMap getTranslatedHttpsPortMapping() {
+        return httpsPortMapping;
     }
 }

+ 41 - 9
core/src/test/java/org/acegisecurity/ui/webapp/AuthenticationProcessingFilterEntryPointTests.java

@@ -15,6 +15,8 @@
 
 package net.sf.acegisecurity.ui.webapp;
 
+import java.util.HashMap;
+
 import junit.framework.TestCase;
 
 import net.sf.acegisecurity.MockHttpServletRequest;
@@ -28,15 +30,6 @@ import net.sf.acegisecurity.MockHttpServletResponse;
  * @version $Id$
  */
 public class AuthenticationProcessingFilterEntryPointTests extends TestCase {
-    //~ Constructors ===========================================================
-
-    public AuthenticationProcessingFilterEntryPointTests() {
-        super();
-    }
-
-    public AuthenticationProcessingFilterEntryPointTests(String arg0) {
-        super(arg0);
-    }
 
     //~ Methods ================================================================
 
@@ -64,6 +57,38 @@ public class AuthenticationProcessingFilterEntryPointTests extends TestCase {
         ep.setLoginFormUrl("/hello");
         assertEquals("/hello", ep.getLoginFormUrl());
     }
+    
+    public void testSetSslPortMapping() {
+        AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
+        HashMap map = new HashMap();
+        try {
+            ep.setHttpsPortMapping(map);
+        } catch (IllegalArgumentException expected) {
+            assertEquals("must map at least one port", expected.getMessage());
+        }
+        
+        map.put(new Integer(0).toString(), new Integer(443).toString());
+        try {
+            ep.setHttpsPortMapping(map);
+        } catch (IllegalArgumentException expected) {
+            assertTrue(expected.getMessage().startsWith("one or both ports out of legal range"));
+        }
+        
+        map.clear();
+        map.put(new Integer(80).toString(), new Integer(100000).toString());
+        try {
+            ep.setHttpsPortMapping(map);
+        } catch (IllegalArgumentException expected) {
+            assertTrue(expected.getMessage().startsWith("one or both ports out of legal range"));
+        }
+        
+        map.clear();
+        map.put(new Integer(80).toString(), new Integer(443).toString());
+        ep.setHttpsPortMapping(map);
+        map = ep.getTranslatedHttpsPortMapping();
+        assertTrue(map.size() == 1);
+        assertTrue(((Integer)map.get(new Integer(80))).equals(new Integer(443)));
+    }
 
     public void testNormalOperation() throws Exception {
         AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
@@ -79,4 +104,11 @@ public class AuthenticationProcessingFilterEntryPointTests extends TestCase {
         ep.commence(request, response);
         assertEquals("/bigWebApp/hello", response.getRedirect());
     }
+    
+    public void testHttpsOperation() throws Exception {
+        AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
+        
+        //TODO: finish later today
+    }
+    
 }