View Javadoc

    1   /*
    2    * $Id: UrlHelperTest.java 508880 2007-02-18 06:33:26Z tschneider $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.views.util;
    22
    23  import java.util.HashMap;
    24  import java.util.LinkedHashMap;
    25  import java.util.Map;
    26  import java.util.TreeMap;
    27
    28  import javax.servlet.http.HttpServletRequest;
    29  import javax.servlet.http.HttpServletResponse;
    30
    31  import org.apache.struts2.StrutsConstants;
    32  import org.apache.struts2.StrutsTestCase;
    33
    34  import com.mockobjects.dynamic.Mock;
    35
    36
    37  /***
    38   * Test case for UrlHelper.
    39   *
    40   */
    41  public class UrlHelperTest extends StrutsTestCase {
    42
    43
    44
    45      public void testForceAddSchemeHostAndPort() throws Exception {
    46          String expectedUrl = "http://localhost/contextPath/path1/path2/myAction.action";
    47
    48          Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    49          mockHttpServletRequest.expectAndReturn("getScheme", "http");
    50          mockHttpServletRequest.expectAndReturn("getServerName", "localhost");
    51          mockHttpServletRequest.expectAndReturn("getContextPath", "/contextPath");
    52
    53          Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
    54          mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl, expectedUrl);
    55
    56          String result = UrlHelper.buildUrl("/path1/path2/myAction.action", (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse)mockHttpServletResponse.proxy(), null, "http", true, true, true);
    57          assertEquals(expectedUrl, result);
    58          mockHttpServletRequest.verify();
    59      }
    60
    61      public void testDoNotForceAddSchemeHostAndPort() throws Exception {
    62          String expectedUrl = "/contextPath/path1/path2/myAction.action";
    63
    64          Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    65          mockHttpServletRequest.expectAndReturn("getScheme", "http");
    66          mockHttpServletRequest.expectAndReturn("getServerName", "localhost");
    67          mockHttpServletRequest.expectAndReturn("getContextPath", "/contextPath");
    68
    69          Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
    70          mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl, expectedUrl);
    71
    72          String result = UrlHelper.buildUrl("/path1/path2/myAction.action", (HttpServletRequest)mockHttpServletRequest.proxy(), (HttpServletResponse)mockHttpServletResponse.proxy(), null, "http", true, true, false);
    73
    74          assertEquals(expectedUrl, result);
    75      }
    76
    77      public void testForceAddNullSchemeHostAndPort() throws Exception {
    78          String expectedUrl = "http://localhost/contextPath/path1/path2/myAction.action";
    79
    80          Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    81          mockHttpServletRequest.expectAndReturn("getScheme", "http");
    82          mockHttpServletRequest.expectAndReturn("getServerName", "localhost");
    83          mockHttpServletRequest.expectAndReturn("getContextPath",
    84              "/contextPath");
    85
    86          Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
    87          mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl,
    88              expectedUrl);
    89
    90          String result = UrlHelper.buildUrl("/path1/path2/myAction.action",
    91              (HttpServletRequest) mockHttpServletRequest.proxy(),
    92              (HttpServletResponse) mockHttpServletResponse.proxy(), null,
    93              null, true, true, true);
    94          assertEquals(expectedUrl, result);
    95          mockHttpServletRequest.verify();
    96      }
    97
    98      public void testBuildParametersStringWithUrlHavingSomeExistingParameters() throws Exception {
    99          String expectedUrl = "http://localhost:8080/myContext/myPage.jsp?initParam=initValue&amp;param1=value1&amp;param2=value2";
    100
    101         Map parameters = new LinkedHashMap();
    102         parameters.put("param1", "value1");
    103         parameters.put("param2", "value2");
    104
    105         StringBuffer url = new StringBuffer("http://localhost:8080/myContext/myPage.jsp?initParam=initValue");
    106
    107         UrlHelper.buildParametersString(parameters, url);
    108
    109         assertEquals(
    110            expectedUrl, url.toString());
    111     }
    112
    113
    114
    115     public void testBuildWithRootContext() {
    116         String expectedUrl = "/MyAction.action";
    117
    118         Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    119         mockHttpServletRequest.expectAndReturn("getContextPath", "/");
    120         mockHttpServletRequest.expectAndReturn("getScheme", "http");
    121
    122         Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
    123         mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl, expectedUrl);
    124
    125         String actualUrl = UrlHelper.buildUrl(expectedUrl, (HttpServletRequest) mockHttpServletRequest.proxy(),
    126                 (HttpServletResponse) mockHttpServletResponse.proxy(), new HashMap());
    127         assertEquals(expectedUrl, actualUrl);
    128     }
    129
    130     /***
    131      * just one &, not &amp;
    132      */
    133     public void testBuildUrlCorrectlyAddsAmp() {
    134         String expectedString = "my.actionName?foo=bar&amp;hello=world";
    135         Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    136         mockHttpServletRequest.expectAndReturn("getScheme", "http");
    137         Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
    138         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
    139
    140         String actionName = "my.actionName";
    141         TreeMap params = new TreeMap();
    142         params.put("hello", "world");
    143         params.put("foo", "bar");
    144
    145         String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params);
    146         assertEquals(expectedString, urlString);
    147     }
    148
    149     public void testBuildUrlWithStringArray() {
    150         String expectedString = "my.actionName?foo=bar&amp;hello=earth&amp;hello=mars";
    151         Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    152         mockHttpServletRequest.expectAndReturn("getScheme", "http");
    153         Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
    154         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
    155
    156         String actionName = "my.actionName";
    157         TreeMap params = new TreeMap();
    158         params.put("hello", new String[]{"earth", "mars"});
    159         params.put("foo", "bar");
    160
    161         String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params);
    162         assertEquals(expectedString, urlString);
    163     }
    164
    165     /***
    166      * The UrlHelper should build a URL that starts with "https" followed by the server name when the scheme of the
    167      * current request is "http" and the port for the "https" scheme is 443.
    168      */
    169     public void testSwitchToHttpsScheme() {
    170         String expectedString = "https://www.mydomain.com/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
    171
    172         Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    173         mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
    174         mockHttpServletRequest.expectAndReturn("getScheme", "http");
    175         mockHttpServletRequest.expectAndReturn("getServerPort", 80);
    176         mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
    177
    178         Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
    179         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
    180
    181         String actionName = "/MyAction.action";
    182         TreeMap params = new TreeMap();
    183         params.put("hello", new String[]{"earth", "mars"});
    184         params.put("foo", "bar");
    185
    186         String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "https", true, true);
    187         assertEquals(expectedString, urlString);
    188     }
    189
    190     /***
    191      * The UrlHelper should build a URL that starts with "http" followed by the server name when the scheme of the
    192      * current request is "https" and the port for the "http" scheme is 80.
    193      */
    194     public void testSwitchToHttpScheme() {
    195         String expectedString = "http://www.mydomain.com/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
    196
    197         Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    198         mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
    199         mockHttpServletRequest.expectAndReturn("getScheme", "https");
    200         mockHttpServletRequest.expectAndReturn("getServerPort", 443);
    201         mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
    202
    203         Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
    204         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
    205
    206         String actionName = "/MyAction.action";
    207         TreeMap params = new TreeMap();
    208         params.put("hello", new String[]{"earth", "mars"});
    209         params.put("foo", "bar");
    210
    211         String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "http", true, true);
    212         assertEquals(expectedString, urlString);
    213     }
    214
    215     /***
    216      * This test is similar to {@link #testSwitchToHttpsScheme()} with the HTTP port equal to 7001 and the HTTPS port
    217      * equal to 7002.
    218      */
    219     public void testSwitchToHttpsNonDefaultPort() {
    220
    221         String expectedString = "https://www.mydomain.com:7002/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
    222
    223         UrlHelper.setHttpPort("7001");
    224         UrlHelper.setHttpsPort("7002");
    225
    226         Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    227         mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
    228         mockHttpServletRequest.expectAndReturn("getScheme", "http");
    229         mockHttpServletRequest.expectAndReturn("getServerPort", 7001);
    230         mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
    231
    232         Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
    233         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
    234
    235         String actionName = "/MyAction.action";
    236         TreeMap params = new TreeMap();
    237         params.put("hello", new String[]{"earth", "mars"});
    238         params.put("foo", "bar");
    239
    240         String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "https", true, true);
    241         assertEquals(expectedString, urlString);
    242     }
    243
    244     /***
    245      * This test is similar to {@link #testSwitchToHttpScheme()} with the HTTP port equal to 7001 and the HTTPS port
    246      * equal to port 7002.
    247      */
    248     public void testSwitchToHttpNonDefaultPort() {
    249
    250         String expectedString = "http://www.mydomain.com:7001/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
    251
    252         UrlHelper.setHttpPort("7001");
    253         UrlHelper.setHttpsPort("7002");
    254
    255         Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    256         mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
    257         mockHttpServletRequest.expectAndReturn("getScheme", "https");
    258         mockHttpServletRequest.expectAndReturn("getServerPort", 7002);
    259         mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
    260
    261         Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
    262         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
    263
    264         String actionName = "/MyAction.action";
    265         TreeMap params = new TreeMap();
    266         params.put("hello", new String[]{"earth", "mars"});
    267         params.put("foo", "bar");
    268
    269         String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "http", true, true);
    270         assertEquals(expectedString, urlString);
    271     }
    272
    273     /***
    274      * The UrlHelper should build a URL that starts with "https" followed by the server name when the scheme of the
    275      * current request is "http" and the port for the "https" scheme is 443. When the request has been forwarded
    276      * in a Servlet 2.4 container, the UrlHelper should use the javax.servlet.forward.request_uri request attribute
    277      * instead of a call to HttpServletRequest#getRequestURI().
    278      */
    279     public void testForwardedRequest() {
    280         String expectedString = "https://www.example.com/mywebapp/product/widget/promo.html";
    281
    282         Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    283         mockHttpServletRequest.expectAndReturn("getServerName", "www.example.com");
    284         mockHttpServletRequest.expectAndReturn("getScheme", "http");
    285         mockHttpServletRequest.expectAndReturn("getServerPort", 80);
    286         mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
    287         mockHttpServletRequest.expectAndReturn("getAttribute", "javax.servlet.forward.request_uri", "/mywebapp/product/widget/");
    288         mockHttpServletRequest.expectAndReturn("getRequestURI", "/mywebapp/");
    289
    290         Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
    291         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
    292
    293         String actionName = "promo.html";
    294         Map params = new TreeMap();
    295
    296         String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "https", true, true);
    297         assertEquals(expectedString, urlString);
    298     }
    299
    300
    301     public void testParseQuery() throws Exception {
    302         Map result = UrlHelper.parseQueryString("aaa=aaaval&bbb=bbbval&ccc=");
    303
    304         assertEquals(result.get("aaa"), "aaaval");
    305         assertEquals(result.get("bbb"), "bbbval");
    306         assertEquals(result.get("ccc"), "");
    307     }
    308
    309     public void testParseEmptyQuery() throws Exception {
    310         Map result = UrlHelper.parseQueryString("");
    311
    312         assertNotNull(result);
    313         assertEquals(result.size(), 0);
    314     }
    315
    316     public void testParseNullQuery() throws Exception {
    317         Map result = UrlHelper.parseQueryString(null);
    318
    319         assertNotNull(result);
    320         assertEquals(result.size(), 0);
    321     }
    322
    323
    324     public void testTranslateAndEncode() throws Exception {
    325         UrlHelper.setCustomEncoding("UTF-8");
    326         String result = UrlHelper.translateAndEncode("\u65b0\u805e");
    327         String expectedResult = "%E6%96%B0%E8%81%9E";
    328
    329         assertEquals(result, expectedResult);
    330     }
    331
    332     public void testTranslateAndDecode() throws Exception {
    333         UrlHelper.setCustomEncoding("UTF-8");
    334         String result = UrlHelper.translateAndDecode("%E6%96%B0%E8%81%9E");
    335         String expectedResult = "\u65b0\u805e";
    336
    337         assertEquals(result, expectedResult);
    338     }
    339 }