PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/struts-2.2.1/src/core/src/test/java/org/apache/struts2/views/util/UrlHelperTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 438 lines | 289 code | 97 blank | 52 comment | 3 complexity | 52a77603610a929b3a1c5430f083492f MD5 | raw file
  1. /*
  2. * $Id: UrlHelperTest.java 927363 2010-03-25 12:14:50Z lukaszlenart $
  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. import com.mockobjects.dynamic.C;
  23. import com.mockobjects.dynamic.Mock;
  24. import com.opensymphony.xwork2.ActionContext;
  25. import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
  26. import com.opensymphony.xwork2.inject.Container;
  27. import com.opensymphony.xwork2.inject.Scope.Strategy;
  28. import com.opensymphony.xwork2.util.ValueStack;
  29. import java.util.HashMap;
  30. import java.util.LinkedHashMap;
  31. import java.util.Map;
  32. import java.util.Set;
  33. import java.util.TreeMap;
  34. import javax.servlet.http.HttpServletRequest;
  35. import javax.servlet.http.HttpServletResponse;
  36. import org.apache.struts2.StrutsConstants;
  37. import org.apache.struts2.StrutsTestCase;
  38. /**
  39. * Test case for UrlHelper.
  40. *
  41. */
  42. public class UrlHelperTest extends StrutsTestCase {
  43. private StubContainer stubContainer;
  44. public void testForceAddSchemeHostAndPort() throws Exception {
  45. String expectedUrl = "http://localhost/contextPath/path1/path2/myAction.action";
  46. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  47. mockHttpServletRequest.expectAndReturn("getScheme", "http");
  48. mockHttpServletRequest.expectAndReturn("getServerName", "localhost");
  49. mockHttpServletRequest.expectAndReturn("getContextPath", "/contextPath");
  50. mockHttpServletRequest.expectAndReturn("getServerPort", 80);
  51. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  52. mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl, expectedUrl);
  53. String result = UrlHelper.buildUrl("/path1/path2/myAction.action", (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse)mockHttpServletResponse.proxy(), null, "http", true, true, true);
  54. assertEquals(expectedUrl, result);
  55. mockHttpServletRequest.verify();
  56. }
  57. public void testDoNotForceAddSchemeHostAndPort() throws Exception {
  58. String expectedUrl = "/contextPath/path1/path2/myAction.action";
  59. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  60. mockHttpServletRequest.expectAndReturn("getScheme", "http");
  61. mockHttpServletRequest.expectAndReturn("getServerName", "localhost");
  62. mockHttpServletRequest.expectAndReturn("getContextPath", "/contextPath");
  63. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  64. mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl, expectedUrl);
  65. String result = UrlHelper.buildUrl("/path1/path2/myAction.action", (HttpServletRequest)mockHttpServletRequest.proxy(), (HttpServletResponse)mockHttpServletResponse.proxy(), null, "http", true, true, false);
  66. assertEquals(expectedUrl, result);
  67. }
  68. public void testForceAddSchemeHostAndPortWithNonStandardPort() throws Exception {
  69. String expectedUrl = "http://localhost:9090/contextPath/path1/path2/myAction.action";
  70. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  71. mockHttpServletRequest.expectAndReturn("getScheme", "http");
  72. mockHttpServletRequest.expectAndReturn("getServerName", "localhost");
  73. mockHttpServletRequest.expectAndReturn("getContextPath", "/contextPath");
  74. mockHttpServletRequest.expectAndReturn("getServerPort", 9090);
  75. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  76. mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl, expectedUrl);
  77. String result = UrlHelper.buildUrl("/path1/path2/myAction.action", (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse)mockHttpServletResponse.proxy(), null, "http", true, true, true);
  78. assertEquals(expectedUrl, result);
  79. mockHttpServletRequest.verify();
  80. }
  81. public void testBuildParametersStringWithUrlHavingSomeExistingParameters() throws Exception {
  82. String expectedUrl = "http://localhost:8080/myContext/myPage.jsp?initParam=initValue&param1=value1&param2=value2&param3%22%3CsCrIpT%3Ealert%281%29%3B%3C%2FsCrIpT%3E=value3";
  83. Map parameters = new LinkedHashMap();
  84. parameters.put("param1", "value1");
  85. parameters.put("param2", "value2");
  86. parameters.put("param3\"<sCrIpT>alert(1);</sCrIpT>","value3");
  87. StringBuilder url = new StringBuilder("http://localhost:8080/myContext/myPage.jsp?initParam=initValue");
  88. UrlHelper.buildParametersString(parameters, url);
  89. assertEquals(
  90. expectedUrl, url.toString());
  91. }
  92. public void testForceAddNullSchemeHostAndPort() throws Exception {
  93. String expectedUrl = "http://localhost/contextPath/path1/path2/myAction.action";
  94. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  95. mockHttpServletRequest.expectAndReturn("getScheme", "http");
  96. mockHttpServletRequest.expectAndReturn("getServerName", "localhost");
  97. mockHttpServletRequest.expectAndReturn("getContextPath",
  98. "/contextPath");
  99. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  100. mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl,
  101. expectedUrl);
  102. String result = UrlHelper.buildUrl("/path1/path2/myAction.action",
  103. (HttpServletRequest) mockHttpServletRequest.proxy(),
  104. (HttpServletResponse) mockHttpServletResponse.proxy(), null,
  105. null, true, true, true);
  106. assertEquals(expectedUrl, result);
  107. mockHttpServletRequest.verify();
  108. }
  109. public void testBuildWithRootContext() {
  110. String expectedUrl = "/MyAction.action";
  111. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  112. mockHttpServletRequest.expectAndReturn("getContextPath", "/");
  113. mockHttpServletRequest.expectAndReturn("getScheme", "http");
  114. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  115. mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl, expectedUrl);
  116. String actualUrl = UrlHelper.buildUrl(expectedUrl, (HttpServletRequest) mockHttpServletRequest.proxy(),
  117. (HttpServletResponse) mockHttpServletResponse.proxy(), new HashMap());
  118. assertEquals(expectedUrl, actualUrl);
  119. }
  120. /**
  121. * just one &, not &amp;
  122. */
  123. public void testBuildUrlCorrectlyAddsAmp() {
  124. String expectedString = "my.actionName?foo=bar&amp;hello=world";
  125. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  126. mockHttpServletRequest.expectAndReturn("getScheme", "http");
  127. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  128. mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
  129. String actionName = "my.actionName";
  130. TreeMap params = new TreeMap();
  131. params.put("hello", "world");
  132. params.put("foo", "bar");
  133. String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params);
  134. assertEquals(expectedString, urlString);
  135. }
  136. /**
  137. * just one &, not &amp;
  138. */
  139. public void testBuildUrlCorrectlyAddsDoNotEscapeAmp() {
  140. String expectedString = "my.actionName?foo=bar&hello=world";
  141. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  142. mockHttpServletRequest.expectAndReturn("getScheme", "http");
  143. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  144. mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
  145. String actionName = "my.actionName";
  146. TreeMap params = new TreeMap();
  147. params.put("hello", "world");
  148. params.put("foo", "bar");
  149. String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, null, true, true, false, false);
  150. assertEquals(expectedString, urlString);
  151. }
  152. public void testBuildUrlWithStringArray() {
  153. String expectedString = "my.actionName?foo=bar&amp;hello=earth&amp;hello=mars";
  154. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  155. mockHttpServletRequest.expectAndReturn("getScheme", "http");
  156. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  157. mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
  158. String actionName = "my.actionName";
  159. TreeMap params = new TreeMap();
  160. params.put("hello", new String[]{"earth", "mars"});
  161. params.put("foo", "bar");
  162. String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params);
  163. assertEquals(expectedString, urlString);
  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. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  172. mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
  173. mockHttpServletRequest.expectAndReturn("getScheme", "http");
  174. mockHttpServletRequest.expectAndReturn("getServerPort", 80);
  175. mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
  176. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  177. mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
  178. String actionName = "/MyAction.action";
  179. TreeMap params = new TreeMap();
  180. params.put("hello", new String[]{"earth", "mars"});
  181. params.put("foo", "bar");
  182. String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "https", true, true);
  183. assertEquals(expectedString, urlString);
  184. }
  185. /**
  186. * The UrlHelper should build a URL that starts with "http" followed by the server name when the scheme of the
  187. * current request is "https" and the port for the "http" scheme is 80.
  188. */
  189. public void testSwitchToHttpScheme() {
  190. String expectedString = "http://www.mydomain.com/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
  191. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  192. mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
  193. mockHttpServletRequest.expectAndReturn("getScheme", "https");
  194. mockHttpServletRequest.expectAndReturn("getServerPort", 443);
  195. mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
  196. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  197. mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
  198. String actionName = "/MyAction.action";
  199. TreeMap params = new TreeMap();
  200. params.put("hello", new String[]{"earth", "mars"});
  201. params.put("foo", "bar");
  202. String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "http", true, true);
  203. assertEquals(expectedString, urlString);
  204. }
  205. /**
  206. * This test is similar to {@link #testSwitchToHttpsScheme()} with the HTTP port equal to 7001 and the HTTPS port
  207. * equal to 7002.
  208. */
  209. public void testSwitchToHttpsNonDefaultPort() {
  210. String expectedString = "https://www.mydomain.com:7002/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
  211. setProp(StrutsConstants.STRUTS_URL_HTTP_PORT, "7001");
  212. setProp(StrutsConstants.STRUTS_URL_HTTPS_PORT, "7002");
  213. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  214. mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
  215. mockHttpServletRequest.expectAndReturn("getScheme", "http");
  216. mockHttpServletRequest.expectAndReturn("getServerPort", 7001);
  217. mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
  218. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  219. mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
  220. String actionName = "/MyAction.action";
  221. TreeMap params = new TreeMap();
  222. params.put("hello", new String[]{"earth", "mars"});
  223. params.put("foo", "bar");
  224. String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "https", true, true);
  225. assertEquals(expectedString, urlString);
  226. }
  227. /**
  228. * This test is similar to {@link #testSwitchToHttpScheme()} with the HTTP port equal to 7001 and the HTTPS port
  229. * equal to port 7002.
  230. */
  231. public void testSwitchToHttpNonDefaultPort() {
  232. String expectedString = "http://www.mydomain.com:7001/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
  233. setProp(StrutsConstants.STRUTS_URL_HTTP_PORT, "7001");
  234. setProp(StrutsConstants.STRUTS_URL_HTTPS_PORT, "7002");
  235. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  236. mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
  237. mockHttpServletRequest.expectAndReturn("getScheme", "https");
  238. mockHttpServletRequest.expectAndReturn("getServerPort", 7002);
  239. mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
  240. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  241. mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
  242. String actionName = "/MyAction.action";
  243. TreeMap params = new TreeMap();
  244. params.put("hello", new String[]{"earth", "mars"});
  245. params.put("foo", "bar");
  246. String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "http", true, true);
  247. assertEquals(expectedString, urlString);
  248. }
  249. /**
  250. * The UrlHelper should build a URL that starts with "https" followed by the server name when the scheme of the
  251. * current request is "http" and the port for the "https" scheme is 443. When the request has been forwarded
  252. * in a Servlet 2.4 container, the UrlHelper should use the javax.servlet.forward.request_uri request attribute
  253. * instead of a call to HttpServletRequest#getRequestURI().
  254. */
  255. public void testForwardedRequest() {
  256. String expectedString = "https://www.example.com/mywebapp/product/widget/promo.html";
  257. Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
  258. mockHttpServletRequest.expectAndReturn("getServerName", "www.example.com");
  259. mockHttpServletRequest.expectAndReturn("getScheme", "http");
  260. mockHttpServletRequest.expectAndReturn("getServerPort", 80);
  261. mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
  262. mockHttpServletRequest.expectAndReturn("getAttribute", "javax.servlet.forward.request_uri", "/mywebapp/product/widget/");
  263. mockHttpServletRequest.expectAndReturn("getRequestURI", "/mywebapp/");
  264. Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
  265. mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
  266. String actionName = "promo.html";
  267. Map params = new TreeMap();
  268. String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "https", true, true);
  269. assertEquals(expectedString, urlString);
  270. }
  271. public void testParseQuery() throws Exception {
  272. Map result = UrlHelper.parseQueryString("aaa=aaaval&bbb=bbbval&ccc=&%3Ca%22%3E=%3Cval%3E");
  273. assertEquals(result.get("aaa"), "aaaval");
  274. assertEquals(result.get("bbb"), "bbbval");
  275. assertEquals(result.get("ccc"), "");
  276. assertEquals(result.get("<a\">"), "<val>");
  277. }
  278. public void testParseEmptyQuery() throws Exception {
  279. Map result = UrlHelper.parseQueryString("");
  280. assertNotNull(result);
  281. assertEquals(result.size(), 0);
  282. }
  283. public void testParseNullQuery() throws Exception {
  284. Map result = UrlHelper.parseQueryString(null);
  285. assertNotNull(result);
  286. assertEquals(result.size(), 0);
  287. }
  288. public void testTranslateAndEncode() throws Exception {
  289. setProp(StrutsConstants.STRUTS_I18N_ENCODING, "UTF-8");
  290. String result = UrlHelper.translateAndEncode("\u65b0\u805e");
  291. String expectedResult = "%E6%96%B0%E8%81%9E";
  292. assertEquals(result, expectedResult);
  293. }
  294. public void testTranslateAndDecode() throws Exception {
  295. setProp(StrutsConstants.STRUTS_I18N_ENCODING, "UTF-8");
  296. String result = UrlHelper.translateAndDecode("%E6%96%B0%E8%81%9E");
  297. String expectedResult = "\u65b0\u805e";
  298. assertEquals(result, expectedResult);
  299. }
  300. public void setUp() throws Exception {
  301. super.setUp();
  302. stubContainer = new StubContainer(container);
  303. ActionContext.getContext().put(ActionContext.CONTAINER, stubContainer);
  304. }
  305. private void setProp(String key, String val) {
  306. stubContainer.overrides.put(key, val);
  307. }
  308. class StubContainer implements Container {
  309. Container parent;
  310. public StubContainer(Container parent) {
  311. super();
  312. this.parent = parent;
  313. }
  314. public Map<String, Object> overrides = new HashMap<String,Object>();
  315. public <T> T getInstance(Class<T> type, String name) {
  316. if (String.class.isAssignableFrom(type) && overrides.containsKey(name)) {
  317. return (T) overrides.get(name);
  318. } else {
  319. return parent.getInstance(type, name);
  320. }
  321. }
  322. public <T> T getInstance(Class<T> type) {
  323. return parent.getInstance(type);
  324. }
  325. public Set<String> getInstanceNames(Class<?> type) {
  326. return parent.getInstanceNames(type);
  327. }
  328. public void inject(Object o) {
  329. parent.inject(o);
  330. }
  331. public <T> T inject(Class<T> implementation) {
  332. return parent.inject(implementation);
  333. }
  334. public void removeScopeStrategy() {
  335. parent.removeScopeStrategy();
  336. }
  337. public void setScopeStrategy(Strategy scopeStrategy) {
  338. parent.setScopeStrategy(scopeStrategy);
  339. }
  340. }
  341. }