avignon.handlers
Class TestHttpServer

java.lang.Object
  extended byjava.lang.Thread
      extended byavignon.handlers.TestHttpServer
All Implemented Interfaces:
java.lang.Runnable

public class TestHttpServer
extends java.lang.Thread

The TestHttpServer class is used to support browser unit tests. It runs on port 7001 and responds to requests of the form GET /PathToPage HTTP/1.1 and POST /PathToPage HTTP/1.1 for file uploads only.


Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
TestHttpServer()
          Constructs an instance of the server and starts listening on port 7001.
TestHttpServer(int port)
          Starts the server on the specified port.
 
Method Summary
 void expectFileUpload(java.lang.String page, java.lang.String response, java.io.ByteArrayOutputStream buffer)
          Prepares the test server to accept an uploaded file.
 java.lang.String getLastRequestText()
          Determines the text of the last HTTP request made.
 java.lang.Integer getListeningPort()
          Determine the port currently being used by the server.
 byte[] getPageForRequest(java.io.InputStream s)
          Determines the response text that will be used for a particular request.
 void run()
          HTTP server thread run method.
 void setDebuggingFlag(boolean debug)
          Deprecated. Duplicates setDebugServer.
 void setDebugServer(boolean debugServer)
          Determines whether debugging information should be sent to the standard output or not.
 void setResponseDelay(int milliseconds)
          Sets the amount of time to wait before responding to a request.
 void shuntBinaryResponse(java.lang.String request, java.lang.String contentType, byte[] responseData)
          Adds a response containing binary data to the server.
 void shuntServerResponse(java.lang.String getPage, java.io.File pageFile)
          Sets the text to return when a specific page is requested.
 void shuntServerResponse(java.lang.String getPage, java.lang.String pageText)
          Sets the text to return when a specific page is requested.
 void stopListening()
          Closes the server socket.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TestHttpServer

public TestHttpServer()
Constructs an instance of the server and starts listening on port 7001.


TestHttpServer

public TestHttpServer(int port)
Starts the server on the specified port.

Parameters:
port - Port on which to listen. Make sure that it is not being used by any other servers.
Method Detail

shuntBinaryResponse

public void shuntBinaryResponse(java.lang.String request,
                                java.lang.String contentType,
                                byte[] responseData)
Adds a response containing binary data to the server. Used for unit testing file downloads.

Parameters:
request - Name of the GET page to be shunted
contentType - Type of content to be returned.
responseData - Actual data to send to the client when the page is requested. HTTP headers will be added.

getListeningPort

public java.lang.Integer getListeningPort()
Determine the port currently being used by the server.

Returns:
The port on which the server is listening or null if it is not running.

setResponseDelay

public void setResponseDelay(int milliseconds)
Sets the amount of time to wait before responding to a request. This allows the simulation of processing time for a web application.

Parameters:
milliseconds - Time in milliseconds to wait before responding to a request.

shuntServerResponse

public void shuntServerResponse(java.lang.String getPage,
                                java.lang.String pageText)
Sets the text to return when a specific page is requested.

Parameters:
getPage - Name of the page to be shunted.
pageText - Text to be returned when a request for GET /getPage HTTP/1.1 is made.

shuntServerResponse

public void shuntServerResponse(java.lang.String getPage,
                                java.io.File pageFile)
                         throws java.io.IOException
Sets the text to return when a specific page is requested.

Parameters:
getPage - Name of the page to be shunted.
pageFile - File containing text to be return when shunted page is requested.
Throws:
java.io.IOException - If there is a problem reading the file.

setDebuggingFlag

public void setDebuggingFlag(boolean debug)
Deprecated. Duplicates setDebugServer.

Determines whether or not requests are output to the standard output.

Parameters:
debug - True to cause debugging output to be written to the standard output.

getLastRequestText

public java.lang.String getLastRequestText()
Determines the text of the last HTTP request made.

Returns:
The text of last HTTP request.

run

public void run()
HTTP server thread run method. There should be no reason to call this method directly since the constructor automatically starts the thread.


getPageForRequest

public byte[] getPageForRequest(java.io.InputStream s)
                         throws java.io.IOException
Determines the response text that will be used for a particular request. Public for unit testing purposes.

Parameters:
s - The InputStream for the HTTP request. Currently GET requests are supported and POST requests are supported for file upload only.
Returns:
The text of the response if it is stored in the server, or a fake 404 error if it cannot be found.
Throws:
java.io.IOException - Thrown if anything happens reading the stream.

stopListening

public void stopListening()
                   throws java.io.IOException
Closes the server socket.

Throws:
java.io.IOException - If any errors occur closing the socket.

setDebugServer

public void setDebugServer(boolean debugServer)
Determines whether debugging information should be sent to the standard output or not.

Parameters:
debugServer - True if debugging information should be sent to the standard output or not.

expectFileUpload

public void expectFileUpload(java.lang.String page,
                             java.lang.String response,
                             java.io.ByteArrayOutputStream buffer)
Prepares the test server to accept an uploaded file. This is currently fairly limited, but adequate for testing AvignonIE and AvignonFirefox.

Parameters:
page - Name of the page that will be POSTed to.
response - The text of the response to return to the client.
buffer - Place to store the uploaded file for later testing.

SourceForge.net Logo