FileDocCategorySizeDatePackage
TestWebServer.javaAPI DocAndroid 1.5 API29192Wed May 06 22:42:02 BST 2009android.core

TestWebServer

public class TestWebServer extends Object implements HttpConstants
TestWebServer is a simulated controllable test server that can respond to requests from HTTP clients. The server can be controlled to change how it reacts to any requests, and can be told to simulate various events (such as network failure) that would happen in a real environment.

Fields Summary
private static final String
LOGTAG
Vector
threads
Vector
activeThreads
int
timeout
int
workers
static final int
DEFAULT_PORT
static final int
DEFAULT_TIMEOUT
protected String
HTTP_VERSION_STRING
private boolean
http11
private AcceptThread
acceptT
int
mTimeout
int
mPort
boolean
mLog
boolean
keepAlive
boolean
chunked
String
redirectHost
int
redirectCode
int
acceptLimit
int
acceptedConnections
static final int
BUF_SIZE
static final byte[]
EOL
Constructors Summary
public TestWebServer()


      
    
Methods Summary
public voidclose()
Cause the thread accepting connections on the server socket to close

        /* Stop the Accept thread */
        if (acceptT != null) {
            log("Closing AcceptThread"+acceptT);
            acceptT.close();
            acceptT = null;
        }
    
public voidinitServer(boolean log)
Initialize a new server with default port and timeout.

param
log Set true if you want trace output

        initServer(DEFAULT_PORT, DEFAULT_TIMEOUT, log);
    
public voidinitServer(int port, boolean log)
Initialize a new server with default timeout.

param
port Sets the server to listen on this port
param
log Set true if you want trace output

        initServer(port, DEFAULT_TIMEOUT, log);
    
public voidinitServer(int port, int timeout, boolean log)
Initialize a new server with default port and timeout.

param
port Sets the server to listen on this port
param
timeout Indicates the period of time to wait until a socket is closed
param
log Set true if you want trace output

        mPort = port;
        mTimeout = timeout;
        mLog = log;
        keepAlive = true;

        if (acceptT == null) {
            acceptT = new AcceptThread();
            acceptT.init();
            acceptT.start();
        }
    
protected voidlog(java.lang.String s)
Print to the log file (if logging enabled)

param
s String to send to the log

        if (mLog) {
            Log.d(LOGTAG, s);
        }
    
public voidsetAcceptLimit(int limit)
Call this to specify the maximum number of sockets to accept

param
limit The number of sockets to accept

        acceptLimit = limit;
    
public voidsetChunked(boolean value)
Call this to indicate whether chunked data should be used

param
value Set true to make server respond with chunk encoded content data.

        chunked = value;
    
public voidsetHttpVersion11(boolean set)
Set the server to be an HTTP/1.0 or HTTP/1.1 server. This should be called prior to any requests being sent to the server.

param
set True for the server to be HTTP/1.1, false for HTTP/1.0

        http11 = set;
        if (set) {
            HTTP_VERSION_STRING = "HTTP/1.1";
        } else {
            HTTP_VERSION_STRING = "HTTP/1.0";
        }
    
public voidsetKeepAlive(boolean value)
Call this to determine whether server connection should remain open

param
value Set true to keep connections open after a request completes

        keepAlive = value;
    
public voidsetRedirect(java.lang.String redirect, int code)
Call this to indicate redirection port requirement. When this value is set, the server will respond to a request with a redirect code with the Location response header set to the value specified.

param
redirect The location to be redirected to
param
redirectCode The code to send when redirecting

        redirectHost = redirect;
        redirectCode = code;
        log("Server will redirect output to "+redirect+" code "+code);