shiro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lhazlew...@apache.org
Subject svn commit: r995613 - in /incubator/shiro/trunk/web/src: main/java/org/apache/shiro/web/filter/authz/PortFilter.java test/java/org/apache/shiro/web/filter/authz/PortFilterTest.java
Date Fri, 10 Sep 2010 00:05:28 GMT
Author: lhazlewood
Date: Fri Sep 10 00:05:28 2010
New Revision: 995613

URL: http://svn.apache.org/viewvc?rev=995613&view=rev
Log:
SHIRO-190: Added port fix w/ accompanying test case

Added:
    incubator/shiro/trunk/web/src/test/java/org/apache/shiro/web/filter/authz/PortFilterTest.java
Modified:
    incubator/shiro/trunk/web/src/main/java/org/apache/shiro/web/filter/authz/PortFilter.java

Modified: incubator/shiro/trunk/web/src/main/java/org/apache/shiro/web/filter/authz/PortFilter.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/web/src/main/java/org/apache/shiro/web/filter/authz/PortFilter.java?rev=995613&r1=995612&r2=995613&view=diff
==============================================================================
--- incubator/shiro/trunk/web/src/main/java/org/apache/shiro/web/filter/authz/PortFilter.java
(original)
+++ incubator/shiro/trunk/web/src/main/java/org/apache/shiro/web/filter/authz/PortFilter.java
Fri Sep 10 00:05:28 2010
@@ -102,12 +102,12 @@ public class PortFilter extends Authoriz
 
         String scheme = getScheme(request.getScheme(), port);
 
-        StringBuffer sb = new StringBuffer();
+        StringBuilder sb = new StringBuilder();
         sb.append(scheme).append("://");
         sb.append(request.getServerName());
-        if (port != 80 && port != 443) {
+        if (port != DEFAULT_HTTP_PORT && port != SslFilter.DEFAULT_HTTPS_PORT) {
             sb.append(":");
-            sb.append(request.getServerPort());
+            sb.append(port);
         }
         if (request instanceof HttpServletRequest) {
             sb.append(WebUtils.toHttp(request).getRequestURI());

Added: incubator/shiro/trunk/web/src/test/java/org/apache/shiro/web/filter/authz/PortFilterTest.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/web/src/test/java/org/apache/shiro/web/filter/authz/PortFilterTest.java?rev=995613&view=auto
==============================================================================
--- incubator/shiro/trunk/web/src/test/java/org/apache/shiro/web/filter/authz/PortFilterTest.java
(added)
+++ incubator/shiro/trunk/web/src/test/java/org/apache/shiro/web/filter/authz/PortFilterTest.java
Fri Sep 10 00:05:28 2010
@@ -0,0 +1,88 @@
+/*
+ * 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.filter.authz;
+
+import org.junit.Test;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
+
+/**
+ * Tests for the {@link PortFilter} class.
+ *
+ * @since 1.1
+ */
+public class PortFilterTest {
+
+    protected HttpServletRequest createBaseMockRequest() {
+        HttpServletRequest request = createNiceMock(HttpServletRequest.class);
+        expect(request.getScheme()).andReturn("http");
+        expect(request.getServerName()).andReturn("localhost");
+        expect(request.getRequestURI()).andReturn("/");
+        return request;
+    }
+
+    @Test
+    public void testDefault() throws Exception {
+        HttpServletResponse response = createNiceMock(HttpServletResponse.class);
+        HttpServletRequest request = createBaseMockRequest();
+
+        expect(response.encodeRedirectURL(eq("http://localhost/"))).andReturn("http://localhost/");
+        replay(request);
+        replay(response);
+
+        PortFilter filter = new PortFilter();
+        boolean result = filter.onAccessDenied(request, response, null);
+
+        verify(request);
+        verify(response);
+        assertFalse(result);
+    }
+
+    /**
+     * This tests the case where the client (e.g. browser) specifies a simple request to
http://localhost/
+     * (i.e. http scheme with the implied port of 80). The redirectURL should reflect the
configured port (8080) instead
+     * of the implied port 80.
+     *
+     * @throws Exception if there is a test failure
+     */
+    @Test
+    public void testConfiguredPort() throws Exception {
+        int port = 8080;
+        HttpServletResponse response = createNiceMock(HttpServletResponse.class);
+        HttpServletRequest request = createBaseMockRequest();
+
+        String expected = "http://localhost:" + port + "/";
+        expect(response.encodeRedirectURL(eq(expected))).andReturn(expected);
+        replay(request);
+        replay(response);
+
+        PortFilter filter = new PortFilter();
+        filter.setPort(port);
+        boolean result = filter.onAccessDenied(request, response, null);
+
+        verify(request);
+        verify(response);
+        assertFalse(result);
+    }
+
+}



Mime
View raw message