1 5 package com.opensymphony.webwork.views.util; 6 7 import com.mockobjects.dynamic.Mock; 8 import com.opensymphony.webwork.config.Configuration; 9 import com.opensymphony.webwork.WebWorkTestCase; 10 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 13 import java.util.TreeMap ; 14 15 16 20 public class UrlHelperTest extends WebWorkTestCase { 21 24 public void testBuildUrlCorrectlyAddsAmp() { 25 String expectedString = "my.actionName?foo=bar&hello=world"; 26 Mock mockHttpServletRequest = new Mock(HttpServletRequest .class); 27 Mock mockHttpServletResponse = new Mock(HttpServletResponse .class); 28 mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString); 29 30 String actionName = "my.actionName"; 31 TreeMap params = new TreeMap (); 32 params.put("hello", "world"); 33 params.put("foo", "bar"); 34 35 String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest ) mockHttpServletRequest.proxy(), (HttpServletResponse ) mockHttpServletResponse.proxy(), params); 36 assertEquals(expectedString, urlString); 37 } 38 39 public void testBuildUrlWithStringArray() { 40 String expectedString = "my.actionName?foo=bar&hello=earth&hello=mars"; 41 Mock mockHttpServletRequest = new Mock(HttpServletRequest .class); 42 Mock mockHttpServletResponse = new Mock(HttpServletResponse .class); 43 mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString); 44 45 String actionName = "my.actionName"; 46 TreeMap params = new TreeMap (); 47 params.put("hello", new String []{"earth", "mars"}); 48 params.put("foo", "bar"); 49 50 String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest ) mockHttpServletRequest.proxy(), (HttpServletResponse ) mockHttpServletResponse.proxy(), params); 51 assertEquals(expectedString, urlString); 52 } 53 54 58 public void testSwitchToHttpsScheme() { 59 String expectedString = "https://www.mydomain.com/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars"; 60 61 Mock mockHttpServletRequest = new Mock(HttpServletRequest .class); 62 mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com"); 63 mockHttpServletRequest.expectAndReturn("getScheme", "http"); 64 mockHttpServletRequest.expectAndReturn("getServerPort", 80); 65 mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp"); 66 67 Mock mockHttpServletResponse = new Mock(HttpServletResponse .class); 68 mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString); 69 70 String actionName = "/MyAction.action"; 71 TreeMap params = new TreeMap (); 72 params.put("hello", new String []{"earth", "mars"}); 73 params.put("foo", "bar"); 74 75 String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest ) mockHttpServletRequest.proxy(), (HttpServletResponse ) mockHttpServletResponse.proxy(), params, "https", true, true); 76 assertEquals(expectedString, urlString); 77 } 78 79 83 public void testSwitchToHttpScheme() { 84 String expectedString = "http://www.mydomain.com/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars"; 85 86 Mock mockHttpServletRequest = new Mock(HttpServletRequest .class); 87 mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com"); 88 mockHttpServletRequest.expectAndReturn("getScheme", "https"); 89 mockHttpServletRequest.expectAndReturn("getServerPort", 443); 90 mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp"); 91 92 Mock mockHttpServletResponse = new Mock(HttpServletResponse .class); 93 mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString); 94 95 String actionName = "/MyAction.action"; 96 TreeMap params = new TreeMap (); 97 params.put("hello", new String []{"earth", "mars"}); 98 params.put("foo", "bar"); 99 100 String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest ) mockHttpServletRequest.proxy(), (HttpServletResponse ) mockHttpServletResponse.proxy(), params, "http", true, true); 101 assertEquals(expectedString, urlString); 102 } 103 104 108 public void testSwitchToHttpsNonDefaultPort() { 109 110 String expectedString = "https://www.mydomain.com:7002/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars"; 111 112 Configuration.set("webwork.url.http.port", "7001"); 113 Configuration.set("webwork.url.https.port", "7002"); 114 115 Mock mockHttpServletRequest = new Mock(HttpServletRequest .class); 116 mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com"); 117 mockHttpServletRequest.expectAndReturn("getScheme", "http"); 118 mockHttpServletRequest.expectAndReturn("getServerPort", 7001); 119 mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp"); 120 121 Mock mockHttpServletResponse = new Mock(HttpServletResponse .class); 122 mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString); 123 124 String actionName = "/MyAction.action"; 125 TreeMap params = new TreeMap (); 126 params.put("hello", new String []{"earth", "mars"}); 127 params.put("foo", "bar"); 128 129 String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest ) mockHttpServletRequest.proxy(), (HttpServletResponse ) mockHttpServletResponse.proxy(), params, "https", true, true); 130 assertEquals(expectedString, urlString); 131 } 132 133 137 public void testSwitchToHttpNonDefaultPort() { 138 139 String expectedString = "http://www.mydomain.com:7001/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars"; 140 141 Configuration.set("webwork.url.http.port", "7001"); 142 Configuration.set("webwork.url.https.port", "7002"); 143 144 Mock mockHttpServletRequest = new Mock(HttpServletRequest .class); 145 mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com"); 146 mockHttpServletRequest.expectAndReturn("getScheme", "https"); 147 mockHttpServletRequest.expectAndReturn("getServerPort", 7002); 148 mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp"); 149 150 Mock mockHttpServletResponse = new Mock(HttpServletResponse .class); 151 mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString); 152 153 String actionName = "/MyAction.action"; 154 TreeMap params = new TreeMap (); 155 params.put("hello", new String []{"earth", "mars"}); 156 params.put("foo", "bar"); 157 158 String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest ) mockHttpServletRequest.proxy(), (HttpServletResponse ) mockHttpServletResponse.proxy(), params, "http", true, true); 159 assertEquals(expectedString, urlString); 160 } 161 162 166 public void testBuildWithSameScheme() { 167 String expectedString = "/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars"; 168 169 Mock mockHttpServletRequest = new Mock(HttpServletRequest .class); 170 mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com"); 171 mockHttpServletRequest.expectAndReturn("getScheme", "https"); 172 mockHttpServletRequest.expectAndReturn("getServerPort", 443); 173 mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp"); 174 175 Mock mockHttpServletResponse = new Mock(HttpServletResponse .class); 176 mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString); 177 178 String actionName = "/MyAction.action"; 179 TreeMap params = new TreeMap (); 180 params.put("hello", new String []{"earth", "mars"}); 181 params.put("foo", "bar"); 182 183 String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest ) mockHttpServletRequest.proxy(), (HttpServletResponse ) mockHttpServletResponse.proxy(), params, "https", true, true); 184 assertEquals(expectedString, urlString); 185 } 186 } | Popular Tags |