test/jdk/java/net/Authenticator/BasicTest.java
changeset 55399 46049b8a5658
parent 47216 71c04702a3d5
equal deleted inserted replaced
55398:e53ec3b362f4 55399:46049b8a5658
     1 /*
     1 /*
     2  * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  */
    22  */
    23 
    23 
    24 import java.io.*;
    24 import java.io.*;
    25 import java.net.*;
    25 import java.net.*;
    26 import java.util.*;
    26 import java.util.*;
       
    27 import jdk.test.lib.net.URIBuilder;
    27 
    28 
    28 /**
    29 /**
    29  * @test
    30  * @test
    30  * @bug 4474947
    31  * @bug 4474947
    31  * @summary  fix for bug #4244472 is incomplete - HTTP authorization still needs work
    32  * @summary  fix for bug #4244472 is incomplete - HTTP authorization still needs work
       
    33  * @library /test/lib
       
    34  * @run main/othervm BasicTest
       
    35  * @run main/othervm -Djava.net.preferIPv6Addresses=true BasicTest
    32  */
    36  */
    33 
    37 
    34 /*
    38 /*
    35  * Note, this is not a general purpose test for Basic Authentication because
    39  * Note, this is not a general purpose test for Basic Authentication because
    36  * it does not check the correctness of the data, only whether the user
    40  * it does not check the correctness of the data, only whether the user
   149     }
   153     }
   150 
   154 
   151     public static void main (String args[]) throws Exception {
   155     public static void main (String args[]) throws Exception {
   152         MyAuthenticator auth = new MyAuthenticator ();
   156         MyAuthenticator auth = new MyAuthenticator ();
   153         Authenticator.setDefault (auth);
   157         Authenticator.setDefault (auth);
   154         ServerSocket ss = new ServerSocket (0);
   158         InetAddress loopback = InetAddress.getLoopbackAddress();
       
   159         ServerSocket ss = new ServerSocket();
       
   160         ss.bind(new InetSocketAddress(loopback, 0));
   155         int port = ss.getLocalPort ();
   161         int port = ss.getLocalPort ();
   156         BasicServer server = new BasicServer (ss);
   162         BasicServer server = new BasicServer (ss);
   157         synchronized (server) {
   163         synchronized (server) {
   158             server.start();
   164             server.start();
   159             System.out.println ("client 1");
   165             System.out.println ("client 1");
   160             URL url = new URL ("http://localhost:"+port+"/d1/d2/d3/foo.html");
   166             String base = URIBuilder.newBuilder()
   161             URLConnection urlc = url.openConnection ();
   167                 .scheme("http")
       
   168                 .loopback()
       
   169                 .port(port)
       
   170                 .path("/")
       
   171                 .build()
       
   172                 .toString();
       
   173             URL url = new URL(base + "d1/d2/d3/foo.html");
       
   174             URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
   162             InputStream is = urlc.getInputStream ();
   175             InputStream is = urlc.getInputStream ();
   163             read (is);
   176             read (is);
   164             System.out.println ("client 2");
   177             System.out.println ("client 2");
   165             url = new URL ("http://localhost:"+port+"/d1/foo.html");
   178             url = new URL(base + "d1/foo.html");
   166             urlc = url.openConnection ();
   179             urlc = url.openConnection(Proxy.NO_PROXY);
   167             is = urlc.getInputStream ();
   180             is = urlc.getInputStream ();
   168             read (is);
   181             read (is);
   169             server.wait ();
   182             server.wait ();
   170             // check if authenticator was called once (ok) or twice (not)
   183             // check if authenticator was called once (ok) or twice (not)
   171                 int f = auth.getCount();
   184                 int f = auth.getCount();