| 1 |
/* |
| 2 |
* Copyright 2002-2004 The Apache Software Foundation. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package test.httpunit; |
| 18 |
|
| 19 |
import junit.framework.TestCase; |
| 20 |
import com.meterware.httpunit.*; |
| 21 |
|
| 22 |
import java.io.*; |
| 23 |
import java.net.MalformedURLException; |
| 24 |
|
| 25 |
import org.xml.sax.SAXException; |
| 26 |
|
| 27 |
/** |
| 28 |
* class to make it that much easier to validate httpunit requests |
| 29 |
*/ |
| 30 |
public class HttpUnitTestBase extends TestCase { |
| 31 |
/** |
| 32 |
* our url |
| 33 |
* |
| 34 |
*/ |
| 35 |
protected String url; |
| 36 |
|
| 37 |
public HttpUnitTestBase(String s) { |
| 38 |
super(s); |
| 39 |
} |
| 40 |
|
| 41 |
private static String URL_PROPERTY="test.functional.webapp.url"; |
| 42 |
/** |
| 43 |
* The JUnit setup method |
| 44 |
* |
| 45 |
*/ |
| 46 |
public void setUp() throws Exception { |
| 47 |
url=System.getProperty(URL_PROPERTY); |
| 48 |
assertNotNull(URL_PROPERTY+" not set",url); |
| 49 |
HttpUnitOptions.setExceptionsThrownOnErrorStatus(true); |
| 50 |
HttpUnitOptions.setMatchesIgnoreCase(true); |
| 51 |
HttpUnitOptions.setParserWarningsEnabled(true); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* assert that the response contains a string |
| 56 |
* @param response |
| 57 |
* @param searchfor |
| 58 |
* @param url |
| 59 |
* @throws IOException |
| 60 |
*/ |
| 61 |
public void assertStringInBody(WebResponse response,String searchfor, String url) |
| 62 |
throws IOException { |
| 63 |
String body=response.getText(); |
| 64 |
boolean found=body.indexOf(searchfor)>=0; |
| 65 |
if(!found) { |
| 66 |
String message; |
| 67 |
message="failed to find ["+searchfor+"] at "+url; |
| 68 |
fail(message); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* assert that a named string is in the request body of the |
| 74 |
* response to a request |
| 75 |
* @param request what we ask |
| 76 |
* @param searchfor string to look for |
| 77 |
* @throws IOException when the fetch fails |
| 78 |
* @throws org.xml.sax.SAXException |
| 79 |
*/ |
| 80 |
protected void assertStringInBody( WebRequest request, |
| 81 |
String searchfor |
| 82 |
) |
| 83 |
throws IOException, org.xml.sax.SAXException { |
| 84 |
WebResponse response = makeRequest(request); |
| 85 |
assertStringInBody(response,searchfor,request.getURL().toString()); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* make a request in a new session |
| 90 |
* @param request request to make |
| 91 |
* @return the response |
| 92 |
* @throws IOException |
| 93 |
* @throws SAXException |
| 94 |
*/ |
| 95 |
protected WebResponse makeRequest(WebRequest request) throws IOException, SAXException { |
| 96 |
WebConversation session = new WebConversation(); |
| 97 |
WebResponse response=session.getResponse(request); |
| 98 |
return response; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* assert that a string is not in a response |
| 103 |
* @param response |
| 104 |
* @param searchfor |
| 105 |
* @param url |
| 106 |
* @throws IOException |
| 107 |
*/ |
| 108 |
protected void assertStringNotInBody(WebResponse response, |
| 109 |
String searchfor, |
| 110 |
String url) |
| 111 |
throws IOException { |
| 112 |
String body=response.getText(); |
| 113 |
boolean found=body.indexOf(searchfor)>=0; |
| 114 |
if(found) { |
| 115 |
String message; |
| 116 |
message="unexpectedly found "+searchfor+" at "+url; |
| 117 |
fail(message); |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* assert that a string is not in the response to a request |
| 124 |
* @param request |
| 125 |
* @param searchfor |
| 126 |
* @throws IOException |
| 127 |
* @throws org.xml.sax.SAXException |
| 128 |
*/ |
| 129 |
protected void assertStringNotInBody( WebRequest request, |
| 130 |
String searchfor) |
| 131 |
throws IOException, org.xml.sax.SAXException { |
| 132 |
WebConversation session = new WebConversation(); |
| 133 |
WebResponse response=session.getResponse(request); |
| 134 |
assertStringNotInBody(response,searchfor, |
| 135 |
request.getURL().toString()); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* here we expect an errorCode other than 200, and look for it |
| 140 |
* checking for text is omitted as it doesnt work. It would never work on |
| 141 |
* java1.3, but one may have expected java1.4+ to have access to the |
| 142 |
* error stream in responses. clearly not |
| 143 |
* @param request |
| 144 |
* @param errorCode |
| 145 |
* @param errorText optional text string to search for |
| 146 |
* @throws MalformedURLException |
| 147 |
* @throws IOException |
| 148 |
* @throws SAXException |
| 149 |
*/ |
| 150 |
protected void expectErrorCode(WebRequest request, |
| 151 |
int errorCode, String errorText) |
| 152 |
throws MalformedURLException, IOException, SAXException { |
| 153 |
WebConversation session = new WebConversation(); |
| 154 |
String failureText="Expected error "+errorCode+" from "+request.getURL(); |
| 155 |
try { |
| 156 |
session.getResponse(request); |
| 157 |
fail(errorText+" -got success instead"); |
| 158 |
} catch (HttpException e) { |
| 159 |
assertEquals(failureText,errorCode,e.getResponseCode()); |
| 160 |
/* checking for text omitted as it doesnt work. |
| 161 |
if(errorText!=null) { |
| 162 |
assertTrue( |
| 163 |
"Failed to find "+errorText+" in "+ e.getResponseMessage(), |
| 164 |
e.getMessage().indexOf(errorText)>=0); |
| 165 |
} |
| 166 |
*/ |
| 167 |
} |
| 168 |
} |
| 169 |
} |