struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [1/2] struts git commit: WW-4843 DefaultUrlHelper().buildUrl() not outputting port when used as parameter
Date Wed, 06 Sep 2017 09:39:40 GMT
Repository: struts
Updated Branches:
  refs/heads/master 64fa814a5 -> 223e735e3


WW-4843 DefaultUrlHelper().buildUrl() not outputting port when used as parameter


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/0b9e68ed
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/0b9e68ed
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/0b9e68ed

Branch: refs/heads/master
Commit: 0b9e68ed16341c585d48d301ceacfb9e74438552
Parents: c9bdb24
Author: Aleksandr Mashchenko <amashchenko@apache.org>
Authored: Wed Aug 23 23:05:29 2017 +0300
Committer: Aleksandr Mashchenko <amashchenko@apache.org>
Committed: Wed Aug 23 23:05:29 2017 +0300

----------------------------------------------------------------------
 .../struts2/views/util/DefaultUrlHelper.java    | 27 +++++++++-----------
 .../views/util/DefaultUrlHelperTest.java        | 23 +++++++++++++++++
 2 files changed, 35 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/0b9e68ed/core/src/main/java/org/apache/struts2/views/util/DefaultUrlHelper.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/views/util/DefaultUrlHelper.java b/core/src/main/java/org/apache/struts2/views/util/DefaultUrlHelper.java
index 668d1a9..0fb86d9 100644
--- a/core/src/main/java/org/apache/struts2/views/util/DefaultUrlHelper.java
+++ b/core/src/main/java/org/apache/struts2/views/util/DefaultUrlHelper.java
@@ -110,19 +110,13 @@ public class DefaultUrlHelper implements UrlHelper {
             if (scheme != null) {
                 // If switching schemes, use the configured port for the particular scheme.
                 if (!scheme.equals(reqScheme)) {
-                    if ((HTTP_PROTOCOL.equals(scheme) && (httpPort != DEFAULT_HTTP_PORT))
|| (HTTPS_PROTOCOL.equals(scheme) && httpsPort != DEFAULT_HTTPS_PORT)) {
-                        link.append(":");
-                        link.append(HTTP_PROTOCOL.equals(scheme) ? httpPort : httpsPort);
-                    }
+                    appendPort(link, scheme, HTTP_PROTOCOL.equals(scheme) ? httpPort : httpsPort);
                 // Else use the port from the current request.
                 } else {
-                    int reqPort = request.getServerPort();
-
-                    if ((scheme.equals(HTTP_PROTOCOL) && (reqPort != DEFAULT_HTTP_PORT))
|| (scheme.equals(HTTPS_PROTOCOL) && reqPort != DEFAULT_HTTPS_PORT)) {
-                        link.append(":");
-                        link.append(reqPort);
-                    }
+                    appendPort(link, scheme, request.getServerPort());
                 }
+            } else {
+                appendPort(link, reqScheme, request.getServerPort());
             }
         } else if ((scheme != null) && !scheme.equals(request.getScheme())) {
             changedScheme = true;
@@ -130,11 +124,7 @@ public class DefaultUrlHelper implements UrlHelper {
             link.append("://");
             link.append(request.getServerName());
 
-            if ((scheme.equals(HTTP_PROTOCOL) && (httpPort != DEFAULT_HTTP_PORT))
|| (HTTPS_PROTOCOL.equals(scheme) && httpsPort != DEFAULT_HTTPS_PORT))
-            {
-                link.append(":");
-                link.append(HTTP_PROTOCOL.equals(scheme) ? httpPort : httpsPort);
-            }
+            appendPort(link, scheme, HTTP_PROTOCOL.equals(scheme) ? httpPort : httpsPort);
         }
 
         if (action != null) {
@@ -201,6 +191,13 @@ public class DefaultUrlHelper implements UrlHelper {
         return result;
     }
 
+    private void appendPort(StringBuilder link, String scheme, int port) {
+        if ((HTTP_PROTOCOL.equals(scheme) && port != DEFAULT_HTTP_PORT) || (HTTPS_PROTOCOL.equals(scheme)
&& port != DEFAULT_HTTPS_PORT)) {
+            link.append(":");
+            link.append(port);
+        }
+    }
+
     public void buildParametersString(Map<String, Object> params, StringBuilder link,
String paramSeparator) {
         buildParametersString(params, link, paramSeparator, true);
     }

http://git-wip-us.apache.org/repos/asf/struts/blob/0b9e68ed/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java b/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java
index b4a5b26..81c595f 100644
--- a/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java
+++ b/core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java
@@ -138,6 +138,29 @@ public class DefaultUrlHelperTest extends StrutsInternalTestCase {
         mockHttpServletRequest.expectAndReturn("getServerName", "localhost");
         mockHttpServletRequest.expectAndReturn("getContextPath",
             "/contextPath");
+        mockHttpServletRequest.expectAndReturn("getServerPort", 80);
+
+        Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
+        mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl,
+            expectedUrl);
+
+        String result = urlHelper.buildUrl("/path1/path2/myAction.action",
+                (HttpServletRequest) mockHttpServletRequest.proxy(),
+                (HttpServletResponse) mockHttpServletResponse.proxy(), null,
+                null, true, true, true);
+        assertEquals(expectedUrl, result);
+        mockHttpServletRequest.verify();
+    }
+
+    public void testForceAddNullSchemeHostAndPort2() throws Exception {
+        String expectedUrl = "http://localhost:8080/contextPath/path1/path2/myAction.action";
+
+        Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
+        mockHttpServletRequest.expectAndReturn("getScheme", "http");
+        mockHttpServletRequest.expectAndReturn("getServerName", "localhost");
+        mockHttpServletRequest.expectAndReturn("getContextPath",
+            "/contextPath");
+        mockHttpServletRequest.expectAndReturn("getServerPort", 8080);
 
         Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
         mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl,


Mime
View raw message