# Class ServletUnitClient

java.lang.Object
com.meterware.httpunit.WebClient
com.meterware.servletunit.ServletUnitClient

public class ServletUnitClient extends WebClient
A client for use with the servlet runner class, allowing the testing of servlets without an actual servlet container. Testing can be done in one of two ways. End-to-end testing works much like the HttpUnit package, except that only servlets actually registered with the ServletRunner will be invoked. It is also possible to test servlets 'from the inside' by creating a ServletInvocationContext and then calling any servlet methods which may be desired. Even in this latter mode, end-to-end testing is supported, but requires a call to this class's getResponse method to update its cookies and frames.

Author:
  Russell Gold

## Nested Class Summary

### Nested classes/interfaces inherited from class WebClient

`WebClient.HeaderDictionary`

## Method Summary

Modifier and Type
Method
Description
`WebResponse`
`getResponse(InvocationContext invocation)`
Updates this client and returns the response which would be displayed by the user agent.
`javax.servlet.http.HttpSession`
`getSession(boolean create)`
Returns the session that would be used by the next request (if it asks for one).
`static ServletUnitClient`
`newClient(InvocationContextFactory factory)`
Creates and returns a new servlet unit client instance.
`InvocationContext`
`newInvocation(WebRequest request)`
Creates and returns a new invocation context to test calling of servlet methods.
`InvocationContext`
`newInvocation(String requestString)`
Creates and returns a new invocation context from a GET request.
`protected WebResponse`
`newResponse(WebRequest request, FrameSelector targetFrame)`
Creates a web response object which represents the response to the specified web request.
`void`
`setProxyServer(String proxyHost, int proxyPort)`
Specifies a proxy server to use for requests from this client.

### Methods inherited from class WebClient

`addClientListener, addCookie, addWindowListener, clearContents, clearProxyServer, getClientProperties, getCookieDetails, getCookieJar, getCookieNames, getCookieValue, getCurrentPage, getExceptionsThrownOnErrorStatus, getFrameContents, getFrameContents, getFrameNames, getHeaderField, getHeaderFields, getMainWindow, getNextAlert, getOpenWindow, getOpenWindows, getProxyHost, getProxyPort, getResource, getResponse, getResponse, getUserAgent, popNextAlert, putCookie, removeClientListener, removeWindowListener, sendRequest, setAuthentication, setAuthorization, setDialogResponder, setExceptionsThrownOnErrorStatus, setHeaderField, setMainWindow, setProxyServer, setUserAgent, updateMainWindow, writeMessageBody`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Method Details

### newClient

public static ServletUnitClient newClient(InvocationContextFactory factory)
Creates and returns a new servlet unit client instance.

### setProxyServer

public void setProxyServer(String proxyHost, int proxyPort)
Specifies a proxy server to use for requests from this client.

Specified by:
  `setProxyServer` in class `WebClient`

### newInvocation

public InvocationContext newInvocation(String requestString) throws IOException, MalformedURLException
Creates and returns a new invocation context from a GET request.

Throws:
  `IOException`
  `MalformedURLException`

### newInvocation

public InvocationContext newInvocation(WebRequest request) throws IOException, MalformedURLException
Creates and returns a new invocation context to test calling of servlet methods.

Throws:
  `IOException`
  `MalformedURLException`

### getResponse

public WebResponse getResponse(InvocationContext invocation) throws MalformedURLException, IOException, SAXException
Updates this client and returns the response which would be displayed by the user agent. Note that this will typically be the same as that returned by the servlet invocation unless that invocation results in a redirect request.

Throws:
  `MalformedURLException`
  `IOException`
  `SAXException`

### getSession

public javax.servlet.http.HttpSession getSession(boolean create)
Returns the session that would be used by the next request (if it asks for one).

Parameters:
  `create` - if true, will create a new session if no valid session is defined.
Since:
  1.6

### newResponse

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

Specified by:
  `newResponse` in class `WebClient`
Parameters:
  `request` - the request to which the response should be generated
  `targetFrame` - the frame in which the response should be stored
Throws:
  `MalformedURLException`
  `IOException`