1 23 24 package org.dbforms.util; 25 26 import com.meterware.httpunit.GetMethodWebRequest; 27 import com.meterware.httpunit.PostMethodWebRequest; 28 import com.meterware.httpunit.WebConversation; 29 import com.meterware.httpunit.WebRequest; 30 import com.meterware.httpunit.WebResponse; 31 32 import junit.framework.TestCase; 34 35 import java.util.Iterator ; 36 import java.util.List ; 37 38 39 40 public abstract class HttpTestCase extends TestCase { 42 private String paramReplace; 43 private String paramSearch; 44 private String urlReplace; 45 private String urlSearch; 46 private WebConversation wc = new WebConversation(); 47 private WebResponse resp = null; 48 49 54 public HttpTestCase(String name) { 55 super(name); 56 57 String context = System.getProperty("cactus.contextURL"); 58 59 if (!Util.isNull(context)) { 60 println("change context to: " + context); 61 urlSearch = "http://localhost/bookstore"; 62 urlReplace = context; 63 paramSearch = "/bookstore/"; 64 paramReplace = "/dbforms-cactus/"; 65 } 66 } 67 68 75 public void get(String url) throws Exception { 76 get(url, null); 77 } 78 79 80 88 public void get(String url, 89 List args) throws Exception { 90 url = replaceURL(url); 91 println("========================="); 92 println("url" + " = " + url); 93 println("========================="); 94 95 WebRequest request = new GetMethodWebRequest(url); 96 doIt(request, args); 97 } 98 99 100 107 public void post(String url) throws Exception { 108 post(url, null); 109 } 110 111 112 120 public void post(String url, 121 List args) throws Exception { 122 url = replaceURL(url); 123 println("========================="); 124 println("url" + " = " + url); 125 println("========================="); 126 127 WebRequest request = new PostMethodWebRequest(url); 128 doIt(request, args); 129 } 130 131 132 137 protected WebResponse getResponse() { 138 return resp; 139 } 140 141 142 147 protected void printResponse() throws Exception { 148 println(resp.getText()); 149 } 150 151 152 157 protected void println(String s) { 158 System.out.println(s); 159 } 160 161 162 171 protected boolean responseContains(String text) throws Exception { 172 if ((resp == null) || (resp.getText() == null)) { 173 return false; 174 } 175 176 return resp.getText() 177 .indexOf(text) != -1; 178 } 179 180 181 private int getStatusCode() { 182 return resp.getResponseCode(); 183 } 184 185 186 private void doIt(WebRequest request, 187 List args) throws Exception { 188 resp = null; 189 190 if (args != null) { 191 println("parameters"); 192 println("========================="); 193 194 Iterator iter = args.iterator(); 195 196 while (iter.hasNext()) { 197 KeyValuePair pair = (KeyValuePair) iter.next(); 198 println(pair.getKey() + " = " + pair.getValue()); 199 request.setParameter(pair.getKey(), replaceParam(pair.getValue())); 200 } 201 202 println("========================="); 203 } 204 205 wc.setExceptionsThrownOnErrorStatus(false); 206 resp = wc.getResponse(request); 207 println("Response code: " + getStatusCode()); 208 println("========================="); 209 assertEquals("Page not found: ", 200, getStatusCode()); 210 } 211 212 213 private String replace(String from, 214 String search, 215 String replace) { 216 if ((search == null) || (replace == null)) { 217 return from; 218 } else { 219 int pos = from.indexOf(search); 220 221 if (pos == -1) { 222 return from; 223 } else { 224 String str = from.substring(0, pos) + replace 225 + from.substring(pos + search.length()); 226 227 return str; 228 } 229 } 230 } 231 232 233 private String replaceParam(String param) { 234 return replace(param, paramSearch, paramReplace); 235 } 236 237 238 private String replaceURL(String url) { 239 return replace(url, urlSearch, urlReplace); 240 } 241 } 242 | Popular Tags |