1 20 package org.apache.cactus; 21 22 import java.util.Enumeration ; 23 24 import junit.framework.TestCase; 25 26 import org.apache.cactus.internal.WebRequestImpl; 27 import org.apache.cactus.internal.configuration.DefaultServletConfiguration; 28 import org.apache.cactus.util.ChainedRuntimeException; 29 30 35 public class TestWebRequest extends TestCase 36 { 37 40 private WebRequest request; 41 42 45 protected void setUp() 46 { 47 request = new WebRequestImpl(new DefaultServletConfiguration()); 48 } 49 50 54 public void testAddParameterInvalidMethod() 55 { 56 try 57 { 58 request.addParameter("param1", "value1", "INVALIDMETHOD"); 59 fail("Should have thrown an exception"); 60 } 61 catch (ChainedRuntimeException e) 62 { 63 assertEquals("The method need to be either \"POST\" or \"GET\"", 64 e.getMessage()); 65 } 66 } 67 68 72 public void testGetParametersGetOk() 73 { 74 request.addParameter("param1", "value1", WebRequest.GET_METHOD); 75 request.addParameter("param1", "value2", WebRequest.GET_METHOD); 76 77 String result = request.getParameterGet("param1"); 78 79 assertEquals("value1", result); 80 } 81 82 86 public void testGetParameterGetNull() 87 { 88 request.addParameter("param1", "value1", WebRequest.POST_METHOD); 89 90 String result = request.getParameterGet("param1"); 91 92 assertNull(result); 93 } 94 95 99 public void testGetParametersPostOk() 100 { 101 request.addParameter("param1", "value1", WebRequest.POST_METHOD); 102 request.addParameter("param1", "value2", WebRequest.POST_METHOD); 103 104 String result = request.getParameterPost("param1"); 105 106 assertEquals("value1", result); 107 } 108 109 113 public void testGetParameterPostNull() 114 { 115 request.addParameter("param1", "value1", WebRequest.GET_METHOD); 116 117 String result = request.getParameterPost("param1"); 118 119 assertNull(result); 120 } 121 122 126 public void testGetHeaderOk() 127 { 128 request.addHeader("header1", "value1"); 129 request.addHeader("header2", "value2"); 130 131 String result = request.getHeader("header1"); 132 133 assertEquals("value1", result); 134 } 135 136 140 public void testGetHeaderNull() 141 { 142 String result = request.getHeader("header1"); 143 144 assertNull(result); 145 } 146 147 151 public void testToString() 152 { 153 request.addHeader("header1", "value1"); 154 request.addHeader("header1", "value2"); 155 request.addParameter("param1", "value1", WebRequest.GET_METHOD); 156 request.addParameter("param1", "value1", WebRequest.POST_METHOD); 157 request.addCookie("cookie1", "value1"); 158 request.setAutomaticSession(false); 159 request.setURL("jakarta.apache.org:80", "/catalog", "/garden", 160 "/implements/", "param1=value1¶m2=¶m3=value3"); 161 162 String result = request.toString(); 163 164 assertEquals("simulation URL = [protocol = [http], host name = " 165 + "[jakarta.apache.org], port = [80], context path = [/catalog], " 166 + "servlet path = [/garden], path info = [/implements/], query " 167 + "string = [param1=value1¶m2=¶m3=value3]], automatic " 168 + "session = [false], cookies = [[name = [cookie1], value = " 169 + "[value1], domain = [localhost], path = [null], isSecure = " 170 + "[false], comment = [null], expiryDate = [null]]], headers = " 171 + "[[[header1] = [[value1], [value2]]]], GET parameters = " 172 + "[[[param3] = [[value3]]][[param2] = [[]]][[param1] = [[value1], " 173 + "[value1]]]], POST parameters = [[[param1] = [[value1]]]]", 174 result); 175 } 176 177 181 public void testSetURLBadQueryString() 182 { 183 try 184 { 185 request.setURL("jakarta.apache.org:80", "/catalog", "/garden", 186 "/implements/", "badquerystring"); 187 fail("Failed to recognize invalid query string"); 188 } 189 catch (RuntimeException e) 190 { 191 assertEquals("Bad QueryString [badquerystring] NameValue pair: " 192 + "[badquerystring]", e.getMessage()); 193 } 194 } 195 196 199 public void testGetPostParametersSeveral() 200 { 201 request.addParameter("param1", "value1", WebRequest.POST_METHOD); 202 request.addParameter("param2", "value2", WebRequest.POST_METHOD); 203 request.addParameter("param3", "value3", WebRequest.POST_METHOD); 204 205 Enumeration keys = request.getParameterNamesPost(); 206 while (keys.hasMoreElements()) 207 { 208 String key = (String ) keys.nextElement(); 209 String [] values = request.getParameterValuesPost(key); 210 211 assertEquals(1, values.length); 212 213 if (!values[0].equals("value1") && !values[0].equals("value2") 214 && !values[0].equals("value3")) 215 { 216 fail("Return value was [" + values[0] + "] but should have " 217 + "been one of [value1], [value2] or [value3]"); 218 } 219 } 220 } 221 } 222 | Popular Tags |