< prev index next >
test/java/net/CookieHandler/CookieManagerTest.java
Print this page
*** 23,45 ****
--- 23,65 ----
/*
* @test
* @summary Unit test for java.net.CookieManager
* @bug 6244040 7150552 7051862
+ * @modules jdk.httpserver
* @run main/othervm -ea CookieManagerTest
* @author Edward Wang
*/
import com.sun.net.httpserver.*;
import java.io.IOException;
import java.net.*;
+ import static java.net.Proxy.NO_PROXY;
public class CookieManagerTest {
static CookieTransactionHandler httpTrans;
static HttpServer server;
+ static final String hostAddress = getAddr();
+
+ /** Returns an IP literal suitable for use by the test. */
+ static String getAddr() {
+ try {
+ InetAddress lh = InetAddress.getLocalHost();
+ System.out.println("Trying: " + lh);
+ if (lh.isReachable(5_000)) {
+ System.out.println("Using: " + lh);
+ return lh.getHostAddress();
+ }
+ } catch (IOException x) {
+ System.out.println("Debug: caught:" + x);
+ }
+ System.out.println("Using: \"127.0.0.1\"");
+ return "127.0.0.1";
+ }
+
public static void main(String[] args) throws Exception {
startHttpServer();
makeHttpCall();
if (httpTrans.badRequest) {
*** 76,87 ****
throw new RuntimeException("Return value is not false!");
}
public static void makeHttpCall() throws IOException {
try {
! System.out.println("http server listenining on: "
! + server.getAddress().getPort());
// install CookieManager to use
CookieHandler.setDefault(new CookieManager());
for (int i = 0; i < CookieTransactionHandler.testCount; i++) {
--- 96,107 ----
throw new RuntimeException("Return value is not false!");
}
public static void makeHttpCall() throws IOException {
try {
! int port = server.getAddress().getPort();
! System.out.println("http server listenining on: " + port);
// install CookieManager to use
CookieHandler.setDefault(new CookieManager());
for (int i = 0; i < CookieTransactionHandler.testCount; i++) {
*** 90,104 ****
((CookieManager)CookieHandler.getDefault())
.setCookiePolicy(CookieTransactionHandler.testPolicies[i]);
((CookieManager)CookieHandler.getDefault())
.getCookieStore().removeAll();
URL url = new URL("http" ,
! InetAddress.getLocalHost().getHostAddress(),
server.getAddress().getPort(),
CookieTransactionHandler.testCases[i][0]
.serverPath);
! HttpURLConnection uc = (HttpURLConnection)url.openConnection();
uc.getResponseCode();
uc.disconnect();
}
} finally {
server.stop(0);
--- 110,125 ----
((CookieManager)CookieHandler.getDefault())
.setCookiePolicy(CookieTransactionHandler.testPolicies[i]);
((CookieManager)CookieHandler.getDefault())
.getCookieStore().removeAll();
URL url = new URL("http" ,
! hostAddress,
server.getAddress().getPort(),
CookieTransactionHandler.testCases[i][0]
.serverPath);
! System.out.println("Requesting " + url);
! HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY);
uc.getResponseCode();
uc.disconnect();
}
} finally {
server.stop(0);
*** 114,125 ****
public static boolean badRequest = false;
// the main test control logic will also loop exactly this number
// to send http request
public static final int testCount = 6;
- private String localHostAddr = "127.0.0.1";
-
@Override
public void handle(HttpExchange exchange) throws IOException {
if (testDone < testCases[testcaseDone].length) {
// still have other tests to run,
// check the Cookie header and then redirect it
--- 135,144 ----
*** 186,199 ****
CookieTransactionHandler() {
testCases = new CookieTestCase[testCount][];
testPolicies = new CookiePolicy[testCount];
! try {
! localHostAddr = InetAddress.getLocalHost().getHostAddress();
! } catch (Exception ignored) {
! };
int count = 0;
// an http session with Netscape cookies exchanged
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
testCases[count++] = new CookieTestCase[]{
--- 205,216 ----
CookieTransactionHandler() {
testCases = new CookieTestCase[testCount][];
testPolicies = new CookiePolicy[testCount];
! String localHostAddr = CookieManagerTest.hostAddress;
!
int count = 0;
// an http session with Netscape cookies exchanged
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
testCases[count++] = new CookieTestCase[]{
< prev index next >