com.meterware.httpunit
Class WebClient

java.lang.Object
  |
  +--com.meterware.httpunit.WebClient
All Implemented Interfaces:
FrameHolder
Direct Known Subclasses:
ServletUnitClient, WebConversation

public abstract class WebClient
extends Object
implements FrameHolder

The context for a series of web requests. This class manages cookies used to maintain session context, computes relative URLs, and generally emulates the browser behavior needed to build an automated test of a web site.

Author:
Russell Gold, Jan Ohrstrom, Seth Ladd, Oliver Imbusch

Inner Class Summary
static class WebClient.HeaderDictionary
           
 
Constructor Summary
protected WebClient()
           
 
Method Summary
 void addClientListener(WebClientListener listener)
          Adds a listener to watch for requests and responses.
 void addCookie(String name, String value)
          Defines a cookie to be sent to the server on every request.
 void clearContents()
          Resets the state of this client, removing all cookies, frames, and per-client headers.
protected  String getCookieHeaderField()
          Returns the value of the cookie header, or null if none is defined.
 String[] getCookieNames()
          Returns the name of all the active cookies which will be sent to the server.
 String getCookieValue(String name)
          Returns the value of the specified cookie.
 boolean getExceptionsThrownOnErrorStatus()
          Returns true if an exception will be thrown when an error status (4xx or 5xx) is detected on a response.
 WebResponse getFrameContents(String frameName)
          Returns the response associated with the specified frame name.
 String[] getFrameNames()
          Returns the name of the currently active frames.
 String getHeaderField(String fieldName)
          Returns the value for the header field with the specified name.
protected  Dictionary getHeaderFields()
          Returns the value of all current header fields.
 WebResponse getResponse(String urlString)
          Submits a GET method request and returns a response.
 WebResponse getResponse(WebRequest request)
          Submits a web request and returns a response, using all state developed so far as stored in cookies as requested by the server.
 String getUserAgent()
          Returns the current user agent setting.
protected abstract  WebResponse newResponse(WebRequest request)
          Creates a web response object which represents the response to the specified web request.
 void removeClientListener(WebClientListener listener)
          Removes a listener to watch for requests and responses.
 WebResponse sendRequest(WebRequest request)
          Submits a web request and returns a response.
 void setAuthorization(String userName, String password)
          Sets a username and password for a basic authentication scheme.
 void setExceptionsThrownOnErrorStatus(boolean throwExceptions)
          Specifies whether an exception will be thrown when an error status (4xx or 5xx) is detected on a response.
 void setHeaderField(String fieldName, String fieldValue)
          Sets the value for a header field to be sent with all requests.
 void setUserAgent(String userAgent)
          Specifies the user agent identification.
protected  void updateClient(WebResponse response)
          Updates this web client based on a received response.
protected  void writeMessageBody(WebRequest request, OutputStream stream)
          Writes the message body for the request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WebClient

protected WebClient()
Method Detail

getResponse

public WebResponse getResponse(String urlString)
                        throws MalformedURLException,
                               IOException,
                               SAXException
Submits a GET method request and returns a response.
Throws:
SAXException - thrown if there is an error parsing the retrieved page

sendRequest

public WebResponse sendRequest(WebRequest request)
                        throws MalformedURLException,
                               IOException,
                               SAXException
Submits a web request and returns a response. This is an alternate name for the getResponse method.

getResponse

public WebResponse getResponse(WebRequest request)
                        throws MalformedURLException,
                               IOException,
                               SAXException
Submits a web request and returns a response, using all state developed so far as stored in cookies as requested by the server.
Throws:
SAXException - thrown if there is an error parsing the retrieved page

clearContents

public void clearContents()
Resets the state of this client, removing all cookies, frames, and per-client headers. This does not affect any listeners or preferences which may have been set.

getFrameNames

public String[] getFrameNames()
Returns the name of the currently active frames.

getFrameContents

public WebResponse getFrameContents(String frameName)
Returns the response associated with the specified frame name. Throws a runtime exception if no matching frame is defined.
Specified by:
getFrameContents in interface FrameHolder

addCookie

public void addCookie(String name,
                      String value)
Defines a cookie to be sent to the server on every request.

getCookieNames

public String[] getCookieNames()
Returns the name of all the active cookies which will be sent to the server.

getCookieValue

public String getCookieValue(String name)
Returns the value of the specified cookie.

setUserAgent

public void setUserAgent(String userAgent)
Specifies the user agent identification. Used to trigger browser-specific server behavior.

getUserAgent

public String getUserAgent()
Returns the current user agent setting.

setAuthorization

public void setAuthorization(String userName,
                             String password)
Sets a username and password for a basic authentication scheme.

setHeaderField

public void setHeaderField(String fieldName,
                           String fieldValue)
Sets the value for a header field to be sent with all requests. If the value set is null, removes the header from those to be sent.

getHeaderField

public String getHeaderField(String fieldName)
Returns the value for the header field with the specified name. This method will ignore the case of the field name.

setExceptionsThrownOnErrorStatus

public void setExceptionsThrownOnErrorStatus(boolean throwExceptions)
Specifies whether an exception will be thrown when an error status (4xx or 5xx) is detected on a response. Defaults to the value returned by HttpUnitOptions.getExceptionsThrownOnErrorStatus.

getExceptionsThrownOnErrorStatus

public boolean getExceptionsThrownOnErrorStatus()
Returns true if an exception will be thrown when an error status (4xx or 5xx) is detected on a response.

addClientListener

public void addClientListener(WebClientListener listener)
Adds a listener to watch for requests and responses.

removeClientListener

public void removeClientListener(WebClientListener listener)
Removes a listener to watch for requests and responses.

newResponse

protected abstract WebResponse newResponse(WebRequest request)
                                    throws MalformedURLException,
                                           IOException
Creates a web response object which represents the response to the specified web request.

writeMessageBody

protected final void writeMessageBody(WebRequest request,
                                      OutputStream stream)
                               throws IOException
Writes the message body for the request.

getCookieHeaderField

protected String getCookieHeaderField()
Returns the value of the cookie header, or null if none is defined.

getHeaderFields

protected Dictionary getHeaderFields()
Returns the value of all current header fields.

updateClient

protected final void updateClient(WebResponse response)
                           throws MalformedURLException,
                                  IOException,
                                  SAXException
Updates this web client based on a received response. This includes updating cookies and frames.