// create a serversocket
Support_HttpServerSocket serversocket = new Support_HttpServerSocket();
// create a client connector
Support_URLConnector client = new Support_URLConnector();
// pass both to the HttpTest
Support_HttpTests test = new Support_HttpTests(serversocket, client);
// run various tests common to both HttpConnections and
// HttpURLConnections
test.runTests(this);
// Authentication test is separate from other tests because it is only
// in HttpURLConnection and not supported in HttpConnection
serversocket = new Support_HttpServerSocket();
Support_HttpServer server = new Support_HttpServer(serversocket, this);
int p = Support_PortManager.getNextPort();
server.startServer(p);
// it is the Support_HttpServer's responsibility to close this
// serversocket
serversocket = null;
final String authTestUrl = "http://localhost:" + server.getPort()
+ Support_HttpServer.AUTHTEST;
// Authentication test
try {
// set up a very simple authenticator
Authenticator.setDefault(new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("test", "password"
.toCharArray());
}
});
try {
client.open(authTestUrl);
is = client.getInputStream();
int c = is.read();
while (c > 0)
c = is.read();
c = is.read();
is.close();
} catch (FileNotFoundException e) {
fail("Error performing authentication test: " + e);
}
} catch (Exception e) {
fail("Exception during test3: " + e);
e.printStackTrace();
}
final String invalidLocation = "/missingFile.htm";
final String redirectTestUrl = "http://localhost:" + server.getPort()
+ Support_HttpServer.REDIRECTTEST;
// test redirecting to a non-existent URL on the same host
try {
// append the response code for the server to return
client.open(redirectTestUrl + "/" + Support_HttpServer.MOVED_PERM
+ "-" + invalidLocation);
is = client.getInputStream();
int c = is.read();
while (c > 0)
c = is.read();
c = is.read();