FileDocCategorySizeDatePackage
HttpUnitTestBase.javaAPI DocApache Axis 1.45636Sat Apr 22 18:57:26 BST 2006test.httpunit

HttpUnitTestBase

public class HttpUnitTestBase extends TestCase
class to make it that much easier to validate httpunit requests

Fields Summary
protected String
url
our url
private static String
URL_PROPERTY
Constructors Summary
public HttpUnitTestBase(String s)

        super(s);
    
Methods Summary
public voidassertStringInBody(WebResponse response, java.lang.String searchfor, java.lang.String url)
assert that the response contains a string

param
response
param
searchfor
param
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);
        }
    
protected voidassertStringInBody(WebRequest request, java.lang.String searchfor)
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

        WebResponse response = makeRequest(request);
        assertStringInBody(response,searchfor,request.getURL().toString());
    
protected voidassertStringNotInBody(WebResponse response, java.lang.String searchfor, java.lang.String url)
assert that a string is not in a response

param
response
param
searchfor
param
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);
        }

    
protected voidassertStringNotInBody(WebRequest request, java.lang.String searchfor)
assert that a string is not in the response to a request

param
request
param
searchfor
throws
IOException
throws
org.xml.sax.SAXException

        WebConversation session = new WebConversation();
        WebResponse response=session.getResponse(request);
        assertStringNotInBody(response,searchfor,
                request.getURL().toString());
    
protected voidexpectErrorCode(WebRequest request, int errorCode, java.lang.String errorText)
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

        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);
            }
            */
        }
    
protected WebResponsemakeRequest(WebRequest request)
make a request in a new session

param
request request to make
return
the response
throws
IOException
throws
SAXException

        WebConversation session = new WebConversation();
        WebResponse response=session.getResponse(request);
        return response;
    
public voidsetUp()
The JUnit setup method

              
         
        url=System.getProperty(URL_PROPERTY);
        assertNotNull(URL_PROPERTY+" not set",url);
        HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
        HttpUnitOptions.setMatchesIgnoreCase(true);
        HttpUnitOptions.setParserWarningsEnabled(true);