test/java/net/CookieHandler/CookieManagerTest.java

Print this page




   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary Unit test for java.net.CookieManager
  27  * @bug 6244040
  28  * @library ../../../sun/net/www/httptest/
  29  * @build HttpCallback HttpServer ClosedChannelList HttpTransaction
  30  * @run main/othervm -ea CookieManagerTest
  31  * @author Edward Wang
  32  */
  33 
  34 import java.net.*;
  35 import java.util.*;
  36 import java.io.*;
  37 import sun.net.www.MessageHeader;
  38 
  39 public class CookieManagerTest {
  40     static CookieHttpTransaction httpTrans;
  41     static HttpServer server;
  42 
  43     public static void main(String[] args) throws Exception {
  44         startHttpServer();
  45         makeHttpCall();
  46 
  47         if (httpTrans.badRequest) {
  48             throw new RuntimeException("Test failed : bad cookie header");
  49         }
  50     }
  51 
  52     public static void startHttpServer() {
  53         try {
  54             httpTrans = new CookieHttpTransaction();
  55             server = new HttpServer(httpTrans, 1, 1, 0);
  56         } catch (IOException e) {
  57             e.printStackTrace();
  58         }
  59     }
  60 
  61     public static void makeHttpCall() {
  62         try {
  63             System.out.println("http server listen on: " + server.getLocalPort());
  64 
  65             // install CookieManager to use
  66             CookieHandler.setDefault(new CookieManager());
  67 
  68             for (int i = 0; i < CookieHttpTransaction.testCount; i++) {
  69                 System.out.println("====== CookieManager test " + (i+1) + " ======");
  70                 ((CookieManager)CookieHandler.getDefault()).setCookiePolicy(CookieHttpTransaction.testPolicies[i]);
  71                 ((CookieManager)CookieHandler.getDefault()).getCookieStore().removeAll();
  72                 URL url = new URL("http" , InetAddress.getLocalHost().getHostAddress(),
  73                                     server.getLocalPort(), CookieHttpTransaction.testCases[i][0].serverPath);
  74                 HttpURLConnection uc = (HttpURLConnection)url.openConnection();
  75                 uc.getResponseCode();




   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary Unit test for java.net.CookieManager
  27  * @bug 6244040
  28  * @library ../../../sun/net/www/httptest/
  29  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
  30  * @run main/othervm -ea CookieManagerTest
  31  * @author Edward Wang
  32  */
  33 
  34 import java.net.*;
  35 import java.util.*;
  36 import java.io.*;
  37 import sun.net.www.MessageHeader;
  38 
  39 public class CookieManagerTest {
  40     static CookieHttpTransaction httpTrans;
  41     static TestHttpServer server;
  42 
  43     public static void main(String[] args) throws Exception {
  44         startHttpServer();
  45         makeHttpCall();
  46 
  47         if (httpTrans.badRequest) {
  48             throw new RuntimeException("Test failed : bad cookie header");
  49         }
  50     }
  51 
  52     public static void startHttpServer() {
  53         try {
  54             httpTrans = new CookieHttpTransaction();
  55             server = new TestHttpServer(httpTrans, 1, 1, 0);
  56         } catch (IOException e) {
  57             e.printStackTrace();
  58         }
  59     }
  60 
  61     public static void makeHttpCall() {
  62         try {
  63             System.out.println("http server listen on: " + server.getLocalPort());
  64 
  65             // install CookieManager to use
  66             CookieHandler.setDefault(new CookieManager());
  67 
  68             for (int i = 0; i < CookieHttpTransaction.testCount; i++) {
  69                 System.out.println("====== CookieManager test " + (i+1) + " ======");
  70                 ((CookieManager)CookieHandler.getDefault()).setCookiePolicy(CookieHttpTransaction.testPolicies[i]);
  71                 ((CookieManager)CookieHandler.getDefault()).getCookieStore().removeAll();
  72                 URL url = new URL("http" , InetAddress.getLocalHost().getHostAddress(),
  73                                     server.getLocalPort(), CookieHttpTransaction.testCases[i][0].serverPath);
  74                 HttpURLConnection uc = (HttpURLConnection)url.openConnection();
  75                 uc.getResponseCode();