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