001: /*
002: * $Id: UrlHelperTest.java 565492 2007-08-13 20:00:08Z jholmes $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.util;
022:
023: import java.util.Arrays;
024: import java.util.HashMap;
025: import java.util.LinkedHashMap;
026: import java.util.Map;
027: import java.util.TreeMap;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import org.apache.struts2.StrutsTestCase;
033:
034: import com.mockobjects.dynamic.Mock;
035:
036: /**
037: * Test case for UrlHelper.
038: *
039: */
040: public class UrlHelperTest extends StrutsTestCase {
041:
042: public void testForceAddSchemeHostAndPort() throws Exception {
043: String expectedUrl = "http://localhost/contextPath/path1/path2/myAction.action";
044:
045: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
046: mockHttpServletRequest.expectAndReturn("getScheme", "http");
047: mockHttpServletRequest.expectAndReturn("getServerName",
048: "localhost");
049: mockHttpServletRequest.expectAndReturn("getContextPath",
050: "/contextPath");
051: mockHttpServletRequest.expectAndReturn("getServerPort", 80);
052:
053: Mock mockHttpServletResponse = new Mock(
054: HttpServletResponse.class);
055: mockHttpServletResponse.expectAndReturn("encodeURL",
056: expectedUrl, expectedUrl);
057:
058: String result = UrlHelper.buildUrl(
059: "/path1/path2/myAction.action",
060: (HttpServletRequest) mockHttpServletRequest.proxy(),
061: (HttpServletResponse) mockHttpServletResponse.proxy(),
062: null, "http", true, true, true);
063: assertEquals(expectedUrl, result);
064: mockHttpServletRequest.verify();
065: }
066:
067: public void testDoNotForceAddSchemeHostAndPort() throws Exception {
068: String expectedUrl = "/contextPath/path1/path2/myAction.action";
069:
070: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
071: mockHttpServletRequest.expectAndReturn("getScheme", "http");
072: mockHttpServletRequest.expectAndReturn("getServerName",
073: "localhost");
074: mockHttpServletRequest.expectAndReturn("getContextPath",
075: "/contextPath");
076:
077: Mock mockHttpServletResponse = new Mock(
078: HttpServletResponse.class);
079: mockHttpServletResponse.expectAndReturn("encodeURL",
080: expectedUrl, expectedUrl);
081:
082: String result = UrlHelper.buildUrl(
083: "/path1/path2/myAction.action",
084: (HttpServletRequest) mockHttpServletRequest.proxy(),
085: (HttpServletResponse) mockHttpServletResponse.proxy(),
086: null, "http", true, true, false);
087:
088: assertEquals(expectedUrl, result);
089: }
090:
091: public void testForceAddSchemeHostAndPortWithNonStandardPort()
092: throws Exception {
093: String expectedUrl = "http://localhost:9090/contextPath/path1/path2/myAction.action";
094:
095: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
096: mockHttpServletRequest.expectAndReturn("getScheme", "http");
097: mockHttpServletRequest.expectAndReturn("getServerName",
098: "localhost");
099: mockHttpServletRequest.expectAndReturn("getContextPath",
100: "/contextPath");
101: mockHttpServletRequest.expectAndReturn("getServerPort", 9090);
102:
103: Mock mockHttpServletResponse = new Mock(
104: HttpServletResponse.class);
105: mockHttpServletResponse.expectAndReturn("encodeURL",
106: expectedUrl, expectedUrl);
107:
108: String result = UrlHelper.buildUrl(
109: "/path1/path2/myAction.action",
110: (HttpServletRequest) mockHttpServletRequest.proxy(),
111: (HttpServletResponse) mockHttpServletResponse.proxy(),
112: null, "http", true, true, true);
113: assertEquals(expectedUrl, result);
114: mockHttpServletRequest.verify();
115: }
116:
117: public void testForceAddNullSchemeHostAndPort() throws Exception {
118: String expectedUrl = "http://localhost/contextPath/path1/path2/myAction.action";
119:
120: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
121: mockHttpServletRequest.expectAndReturn("getScheme", "http");
122: mockHttpServletRequest.expectAndReturn("getServerName",
123: "localhost");
124: mockHttpServletRequest.expectAndReturn("getContextPath",
125: "/contextPath");
126:
127: Mock mockHttpServletResponse = new Mock(
128: HttpServletResponse.class);
129: mockHttpServletResponse.expectAndReturn("encodeURL",
130: expectedUrl, expectedUrl);
131:
132: String result = UrlHelper.buildUrl(
133: "/path1/path2/myAction.action",
134: (HttpServletRequest) mockHttpServletRequest.proxy(),
135: (HttpServletResponse) mockHttpServletResponse.proxy(),
136: null, null, true, true, true);
137: assertEquals(expectedUrl, result);
138: mockHttpServletRequest.verify();
139: }
140:
141: public void testBuildParametersStringWithUrlHavingSomeExistingParameters()
142: throws Exception {
143: String expectedUrl = "http://localhost:8080/myContext/myPage.jsp?initParam=initValue&param1=value1&param2=value2";
144:
145: Map parameters = new LinkedHashMap();
146: parameters.put("param1", "value1");
147: parameters.put("param2", "value2");
148:
149: StringBuffer url = new StringBuffer(
150: "http://localhost:8080/myContext/myPage.jsp?initParam=initValue");
151:
152: UrlHelper.buildParametersString(parameters, url);
153:
154: assertEquals(expectedUrl, url.toString());
155: }
156:
157: public void testBuildWithRootContext() {
158: String expectedUrl = "/MyAction.action";
159:
160: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
161: mockHttpServletRequest.expectAndReturn("getContextPath", "/");
162: mockHttpServletRequest.expectAndReturn("getScheme", "http");
163:
164: Mock mockHttpServletResponse = new Mock(
165: HttpServletResponse.class);
166: mockHttpServletResponse.expectAndReturn("encodeURL",
167: expectedUrl, expectedUrl);
168:
169: String actualUrl = UrlHelper.buildUrl(expectedUrl,
170: (HttpServletRequest) mockHttpServletRequest.proxy(),
171: (HttpServletResponse) mockHttpServletResponse.proxy(),
172: new HashMap());
173: assertEquals(expectedUrl, actualUrl);
174: }
175:
176: /**
177: * just one &, not &
178: */
179: public void testBuildUrlCorrectlyAddsAmp() {
180: String expectedString = "my.actionName?foo=bar&hello=world";
181: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
182: mockHttpServletRequest.expectAndReturn("getScheme", "http");
183: Mock mockHttpServletResponse = new Mock(
184: HttpServletResponse.class);
185: mockHttpServletResponse.expectAndReturn("encodeURL",
186: expectedString, expectedString);
187:
188: String actionName = "my.actionName";
189: TreeMap params = new TreeMap();
190: params.put("hello", "world");
191: params.put("foo", "bar");
192:
193: String urlString = UrlHelper.buildUrl(actionName,
194: (HttpServletRequest) mockHttpServletRequest.proxy(),
195: (HttpServletResponse) mockHttpServletResponse.proxy(),
196: params);
197: assertEquals(expectedString, urlString);
198: }
199:
200: public void testBuildUrlCorrectlyAddsDoNotEscapeAmp() {
201: String expectedString = "my.actionName?foo=bar&hello=world";
202: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
203: mockHttpServletRequest.expectAndReturn("getScheme", "http");
204: Mock mockHttpServletResponse = new Mock(
205: HttpServletResponse.class);
206: mockHttpServletResponse.expectAndReturn("encodeURL",
207: expectedString, expectedString);
208:
209: String actionName = "my.actionName";
210: TreeMap params = new TreeMap();
211: params.put("hello", "world");
212: params.put("foo", "bar");
213:
214: String urlString = UrlHelper.buildUrl(actionName,
215: (HttpServletRequest) mockHttpServletRequest.proxy(),
216: (HttpServletResponse) mockHttpServletResponse.proxy(),
217: params, null, true, true, false, false);
218: assertEquals(expectedString, urlString);
219: }
220:
221: public void testBuildUrlWithStringArray() {
222: String expectedString = "my.actionName?foo=bar&hello=earth&hello=mars";
223: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
224: mockHttpServletRequest.expectAndReturn("getScheme", "http");
225: Mock mockHttpServletResponse = new Mock(
226: HttpServletResponse.class);
227: mockHttpServletResponse.expectAndReturn("encodeURL",
228: expectedString, expectedString);
229:
230: String actionName = "my.actionName";
231: TreeMap params = new TreeMap();
232: params.put("hello", new String[] { "earth", "mars" });
233: params.put("foo", "bar");
234:
235: String urlString = UrlHelper.buildUrl(actionName,
236: (HttpServletRequest) mockHttpServletRequest.proxy(),
237: (HttpServletResponse) mockHttpServletResponse.proxy(),
238: params);
239: assertEquals(expectedString, urlString);
240: }
241:
242: /**
243: * The UrlHelper should build a URL that starts with "https" followed by the server name when the scheme of the
244: * current request is "http" and the port for the "https" scheme is 443.
245: */
246: public void testSwitchToHttpsScheme() {
247: String expectedString = "https://www.mydomain.com/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars";
248:
249: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
250: mockHttpServletRequest.expectAndReturn("getServerName",
251: "www.mydomain.com");
252: mockHttpServletRequest.expectAndReturn("getScheme", "http");
253: mockHttpServletRequest.expectAndReturn("getServerPort", 80);
254: mockHttpServletRequest.expectAndReturn("getContextPath",
255: "/mywebapp");
256:
257: Mock mockHttpServletResponse = new Mock(
258: HttpServletResponse.class);
259: mockHttpServletResponse.expectAndReturn("encodeURL",
260: expectedString, expectedString);
261:
262: String actionName = "/MyAction.action";
263: TreeMap params = new TreeMap();
264: params.put("hello", new String[] { "earth", "mars" });
265: params.put("foo", "bar");
266:
267: String urlString = UrlHelper.buildUrl(actionName,
268: (HttpServletRequest) mockHttpServletRequest.proxy(),
269: (HttpServletResponse) mockHttpServletResponse.proxy(),
270: params, "https", true, true);
271: assertEquals(expectedString, urlString);
272: }
273:
274: /**
275: * The UrlHelper should build a URL that starts with "http" followed by the server name when the scheme of the
276: * current request is "https" and the port for the "http" scheme is 80.
277: */
278: public void testSwitchToHttpScheme() {
279: String expectedString = "http://www.mydomain.com/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars";
280:
281: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
282: mockHttpServletRequest.expectAndReturn("getServerName",
283: "www.mydomain.com");
284: mockHttpServletRequest.expectAndReturn("getScheme", "https");
285: mockHttpServletRequest.expectAndReturn("getServerPort", 443);
286: mockHttpServletRequest.expectAndReturn("getContextPath",
287: "/mywebapp");
288:
289: Mock mockHttpServletResponse = new Mock(
290: HttpServletResponse.class);
291: mockHttpServletResponse.expectAndReturn("encodeURL",
292: expectedString, expectedString);
293:
294: String actionName = "/MyAction.action";
295: TreeMap params = new TreeMap();
296: params.put("hello", new String[] { "earth", "mars" });
297: params.put("foo", "bar");
298:
299: String urlString = UrlHelper.buildUrl(actionName,
300: (HttpServletRequest) mockHttpServletRequest.proxy(),
301: (HttpServletResponse) mockHttpServletResponse.proxy(),
302: params, "http", true, true);
303: assertEquals(expectedString, urlString);
304: }
305:
306: /**
307: * This test is similar to {@link #testSwitchToHttpsScheme()} with the HTTP port equal to 7001 and the HTTPS port
308: * equal to 7002.
309: */
310: public void testSwitchToHttpsNonDefaultPort() {
311:
312: String expectedString = "https://www.mydomain.com:7002/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars";
313:
314: UrlHelper.setHttpPort("7001");
315: UrlHelper.setHttpsPort("7002");
316:
317: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
318: mockHttpServletRequest.expectAndReturn("getServerName",
319: "www.mydomain.com");
320: mockHttpServletRequest.expectAndReturn("getScheme", "http");
321: mockHttpServletRequest.expectAndReturn("getServerPort", 7001);
322: mockHttpServletRequest.expectAndReturn("getContextPath",
323: "/mywebapp");
324:
325: Mock mockHttpServletResponse = new Mock(
326: HttpServletResponse.class);
327: mockHttpServletResponse.expectAndReturn("encodeURL",
328: expectedString, expectedString);
329:
330: String actionName = "/MyAction.action";
331: TreeMap params = new TreeMap();
332: params.put("hello", new String[] { "earth", "mars" });
333: params.put("foo", "bar");
334:
335: String urlString = UrlHelper.buildUrl(actionName,
336: (HttpServletRequest) mockHttpServletRequest.proxy(),
337: (HttpServletResponse) mockHttpServletResponse.proxy(),
338: params, "https", true, true);
339: assertEquals(expectedString, urlString);
340: }
341:
342: /**
343: * This test is similar to {@link #testSwitchToHttpScheme()} with the HTTP port equal to 7001 and the HTTPS port
344: * equal to port 7002.
345: */
346: public void testSwitchToHttpNonDefaultPort() {
347:
348: String expectedString = "http://www.mydomain.com:7001/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars";
349:
350: UrlHelper.setHttpPort("7001");
351: UrlHelper.setHttpsPort("7002");
352:
353: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
354: mockHttpServletRequest.expectAndReturn("getServerName",
355: "www.mydomain.com");
356: mockHttpServletRequest.expectAndReturn("getScheme", "https");
357: mockHttpServletRequest.expectAndReturn("getServerPort", 7002);
358: mockHttpServletRequest.expectAndReturn("getContextPath",
359: "/mywebapp");
360:
361: Mock mockHttpServletResponse = new Mock(
362: HttpServletResponse.class);
363: mockHttpServletResponse.expectAndReturn("encodeURL",
364: expectedString, expectedString);
365:
366: String actionName = "/MyAction.action";
367: TreeMap params = new TreeMap();
368: params.put("hello", new String[] { "earth", "mars" });
369: params.put("foo", "bar");
370:
371: String urlString = UrlHelper.buildUrl(actionName,
372: (HttpServletRequest) mockHttpServletRequest.proxy(),
373: (HttpServletResponse) mockHttpServletResponse.proxy(),
374: params, "http", true, true);
375: assertEquals(expectedString, urlString);
376: }
377:
378: /**
379: * The UrlHelper should build a URL that starts with "https" followed by the server name when the scheme of the
380: * current request is "http" and the port for the "https" scheme is 443. When the request has been forwarded
381: * in a Servlet 2.4 container, the UrlHelper should use the javax.servlet.forward.request_uri request attribute
382: * instead of a call to HttpServletRequest#getRequestURI().
383: */
384: public void testForwardedRequest() {
385: String expectedString = "https://www.example.com/mywebapp/product/widget/promo.html";
386:
387: Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
388: mockHttpServletRequest.expectAndReturn("getServerName",
389: "www.example.com");
390: mockHttpServletRequest.expectAndReturn("getScheme", "http");
391: mockHttpServletRequest.expectAndReturn("getServerPort", 80);
392: mockHttpServletRequest.expectAndReturn("getContextPath",
393: "/mywebapp");
394: mockHttpServletRequest.expectAndReturn("getAttribute",
395: "javax.servlet.forward.request_uri",
396: "/mywebapp/product/widget/");
397: mockHttpServletRequest.expectAndReturn("getRequestURI",
398: "/mywebapp/");
399:
400: Mock mockHttpServletResponse = new Mock(
401: HttpServletResponse.class);
402: mockHttpServletResponse.expectAndReturn("encodeURL",
403: expectedString, expectedString);
404:
405: String actionName = "promo.html";
406: Map params = new TreeMap();
407:
408: String urlString = UrlHelper.buildUrl(actionName,
409: (HttpServletRequest) mockHttpServletRequest.proxy(),
410: (HttpServletResponse) mockHttpServletResponse.proxy(),
411: params, "https", true, true);
412: assertEquals(expectedString, urlString);
413: }
414:
415: public void testParseQuery() throws Exception {
416: Map result = UrlHelper
417: .parseQueryString("aaa=aaaval&bbb=bbbval&ccc=");
418:
419: assertEquals(result.get("aaa"), "aaaval");
420: assertEquals(result.get("bbb"), "bbbval");
421: assertEquals(result.get("ccc"), "");
422: }
423:
424: public void testParseEmptyQuery() throws Exception {
425: Map result = UrlHelper.parseQueryString("");
426:
427: assertNotNull(result);
428: assertEquals(result.size(), 0);
429: }
430:
431: public void testParseNullQuery() throws Exception {
432: Map result = UrlHelper.parseQueryString(null);
433:
434: assertNotNull(result);
435: assertEquals(result.size(), 0);
436: }
437:
438: public void testParseMultiQuery() throws Exception {
439: Map result = UrlHelper
440: .parseQueryString("param=1¶m=1¶m=1");
441:
442: assertNotNull(result);
443: assertEquals(result.size(), 1);
444: String values[] = (String[]) result.get("param");
445: Arrays.sort(values);
446: assertEquals(values.length, 3);
447: assertEquals(values[0], "1");
448: assertEquals(values[1], "1");
449: assertEquals(values[2], "1");
450: }
451:
452: public void testParseDuplicateQuery() throws Exception {
453: Map result = UrlHelper
454: .parseQueryString("param=1¶m=2¶m=3");
455:
456: assertNotNull(result);
457: assertEquals(result.size(), 1);
458: String values[] = (String[]) result.get("param");
459: Arrays.sort(values);
460: assertEquals(values.length, 3);
461: assertEquals(values[0], "1");
462: assertEquals(values[1], "2");
463: assertEquals(values[2], "3");
464: }
465:
466: public void testTranslateAndEncode() throws Exception {
467: UrlHelper.setCustomEncoding("UTF-8");
468: String result = UrlHelper.translateAndEncode("\u65b0\u805e");
469: String expectedResult = "%E6%96%B0%E8%81%9E";
470:
471: assertEquals(result, expectedResult);
472: }
473:
474: public void testTranslateAndDecode() throws Exception {
475: UrlHelper.setCustomEncoding("UTF-8");
476: String result = UrlHelper
477: .translateAndDecode("%E6%96%B0%E8%81%9E");
478: String expectedResult = "\u65b0\u805e";
479:
480: assertEquals(result, expectedResult);
481: }
482: }
|