1. opensymphony
  2. webwork

Commits

mbogaert  committed b2fa0ac

Tests from WW-713: The <ww:url> tag does not correctly construct the URL when the scheme attribute is set and the scheme changes.

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@673573baa09-0c28-0410-bef9-dab3c582ae83

  • Participants
  • Parent commits f6588fb
  • Branches master

Comments (0)

Files changed (1)

File src/test/com/opensymphony/webwork/views/util/UrlHelperTest.java

View file
 /*
- * Copyright (c) 2002-2003 by OpenSymphony
+ * Copyright (c) 2002-2005 by OpenSymphony
  * All rights reserved.
  */
 package com.opensymphony.webwork.views.util;
 
 import com.mockobjects.dynamic.Mock;
+import com.opensymphony.webwork.config.Configuration;
 import junit.framework.TestCase;
 
 import javax.servlet.http.HttpServletRequest;
         String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params);
         assertEquals(expectedString, urlString);
     }
-}
+
+    /**
+     * The UrlHelper should build a URL that starts with "https" followed by the server name
+     * when the scheme of the current request is "http" and the port for the "https" scheme is 443.
+     */
+    public void testSwitchToHttpsScheme() {
+        String expectedString = "https://www.mydomain.com/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars";
+
+        Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
+        mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
+        mockHttpServletRequest.expectAndReturn("getScheme", "http");
+        mockHttpServletRequest.expectAndReturn("getServerPort", 80);
+        mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
+
+        Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
+        mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
+
+        String actionName = "/MyAction.action";
+        TreeMap params = new TreeMap();
+        params.put("hello", new String[]{"earth", "mars"});
+        params.put("foo", "bar");
+
+        String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "https", true, true);
+        assertEquals(expectedString, urlString);
+    }
+
+    /**
+     * The UrlHelper should build a URL that starts with "http" followed by the server name when
+     * the scheme of the current request is "https" and the port for the "http" scheme is 80.
+     */
+    public void testSwitchToHttpScheme() {
+        String expectedString = "http://www.mydomain.com/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars";
+
+        Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
+        mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
+        mockHttpServletRequest.expectAndReturn("getScheme", "https");
+        mockHttpServletRequest.expectAndReturn("getServerPort", 443);
+        mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
+
+        Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
+        mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
+
+        String actionName = "/MyAction.action";
+        TreeMap params = new TreeMap();
+        params.put("hello", new String[]{"earth", "mars"});
+        params.put("foo", "bar");
+
+        String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "http", true, true);
+        assertEquals(expectedString, urlString);
+    }
+
+    /**
+     * This test is similar to {@link #testSwitchToHttpsScheme()} with the HTTP port equal to 7001
+     * and the HTTPS port equal to 7002.
+     */
+    public void testSwitchToHttpsNonDefaultPort() {
+
+        String expectedString = "https://www.mydomain.com:7002/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars";
+
+        Configuration.set("webwork.url.http.port", "7001");
+        Configuration.set("webwork.url.https.port", "7002");
+
+        Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
+        mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
+        mockHttpServletRequest.expectAndReturn("getScheme", "http");
+        mockHttpServletRequest.expectAndReturn("getServerPort", 7001);
+        mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
+
+        Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
+        mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
+
+        String actionName = "/MyAction.action";
+        TreeMap params = new TreeMap();
+        params.put("hello", new String[]{"earth", "mars"});
+        params.put("foo", "bar");
+
+        String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "https", true, true);
+        assertEquals(expectedString, urlString);
+    }
+
+    /**
+     * This test is similar to {@link #testSwitchToHttpScheme()} with the HTTP port equal to 7001
+     * and the HTTPS port equal to port 7002.
+     */
+    public void testSwitchToHttpNonDefaultPort() {
+
+        String expectedString = "http://www.mydomain.com:7001/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars";
+
+        Configuration.set("webwork.url.http.port", "7001");
+        Configuration.set("webwork.url.https.port", "7002");
+
+        Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
+        mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
+        mockHttpServletRequest.expectAndReturn("getScheme", "https");
+        mockHttpServletRequest.expectAndReturn("getServerPort", 7002);
+        mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
+
+        Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
+        mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
+
+        String actionName = "/MyAction.action";
+        TreeMap params = new TreeMap();
+        params.put("hello", new String[]{"earth", "mars"});
+        params.put("foo", "bar");
+
+        String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "http", true, true);
+        assertEquals(expectedString, urlString);
+    }
+
+    /**
+     * A check to verify that the scheme, server, and port number are omitted when the
+     * scheme of the current request matches the scheme supplied when building the URL.
+     */
+    public void testBuildWithSameScheme() {
+        String expectedString = "/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars";
+
+        Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
+        mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
+        mockHttpServletRequest.expectAndReturn("getScheme", "https");
+        mockHttpServletRequest.expectAndReturn("getServerPort", 443);
+        mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
+
+        Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
+        mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
+
+        String actionName = "/MyAction.action";
+        TreeMap params = new TreeMap();
+        params.put("hello", new String[]{"earth", "mars"});
+        params.put("foo", "bar");
+
+        String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "https", true, true);
+        assertEquals(expectedString, urlString);
+    }
+}