jdk/test/java/net/CookieHandler/CookieManagerTest.java
author jccollet
Wed, 05 Mar 2008 11:40:22 +0100
changeset 74 068494063b1b
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6641309: Wrong Cookie separator used in HttpURLConnection Summary: Added a space to cookie separator. Generified the code and added tags. Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Unit test for java.net.CookieManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 6244040
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @library ../../../sun/net/www/httptest/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @build HttpCallback HttpServer ClosedChannelList HttpTransaction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main/othervm -ea CookieManagerTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author Edward Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.net.www.MessageHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class CookieManagerTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static CookieHttpTransaction httpTrans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static HttpServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        startHttpServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        makeHttpCall();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        if (httpTrans.badRequest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            throw new RuntimeException("Test failed : bad cookie header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static void startHttpServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            httpTrans = new CookieHttpTransaction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            server = new HttpServer(httpTrans, 1, 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static void makeHttpCall() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.out.println("http server listen on: " + server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            // install CookieManager to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            CookieHandler.setDefault(new CookieManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            for (int i = 0; i < CookieHttpTransaction.testCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                System.out.println("====== CookieManager test " + (i+1) + " ======");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                ((CookieManager)CookieHandler.getDefault()).setCookiePolicy(CookieHttpTransaction.testPolicies[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                ((CookieManager)CookieHandler.getDefault()).getCookieStore().removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                URL url = new URL("http" , InetAddress.getLocalHost().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                    server.getLocalPort(), CookieHttpTransaction.testCases[i][0].serverPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                HttpURLConnection uc = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                uc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                uc.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
class CookieHttpTransaction implements HttpCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static boolean badRequest = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // the main test control logic will also loop exactly this number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // to send http request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final int testCount = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private String localHostAddr = "127.0.0.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // test cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static class CookieTestCase {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        public String headerToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        public String cookieToSend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        public String cookieToRecv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        public String serverPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        public CookieTestCase(String h, String cts, String ctr, String sp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            headerToken = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            cookieToSend = cts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            cookieToRecv = ctr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            serverPath = sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // these two must match each other, i.e. testCases.length == testPolicies.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static CookieTestCase[][] testCases = null;  // the test cases to run; each test case may contain multiple roundtrips
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static CookiePolicy[] testPolicies = null;   // indicates what CookiePolicy to use with each test cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    CookieHttpTransaction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        testCases = new CookieTestCase[testCount][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        testPolicies = new CookiePolicy[testCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            localHostAddr = InetAddress.getLocalHost().getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } catch (Exception ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // an http session with Netscape cookies exchanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        testCases[count++] = new CookieTestCase[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                new CookieTestCase("Set-Cookie",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                "CUSTOMER=WILE:BOB; path=/; expires=Wednesday, 09-Nov-2030 23:12:40 GMT;" + "domain=." + localHostAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                "CUSTOMER=WILE:BOB",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                new CookieTestCase("Set-Cookie",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                "PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   135
                "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                new CookieTestCase("Set-Cookie",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                "SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   140
                "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                new CookieTestCase("Set-Cookie",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                "SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   145
                "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                "/foo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // check whether or not path rule is applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        testCases[count++] = new CookieTestCase[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                new CookieTestCase("Set-Cookie",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                "PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                "PART_NUMBER=ROCKET_LAUNCHER_0001",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                new CookieTestCase("Set-Cookie",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                "PART_NUMBER=RIDING_ROCKET_0023; path=/ammo;" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   160
                "PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                "/ammo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // an http session with rfc2965 cookies exchanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        testCases[count++] = new CookieTestCase[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                "Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   170
                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                "/acme/login"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\";" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   175
                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr  + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr +  "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                "/acme/pickitem"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                "Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   180
                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr  + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr  + "\"" + "; Shipping=\"FedEx\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                "/acme/shipping"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // check whether or not the path rule is applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        testCases[count++] = new CookieTestCase[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   190
                "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                "/acme/ammo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                "Part_Number=\"Riding_Rocket_0023\"; Version=\"1\"; Path=\"/acme/ammo\";" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   195
                "$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\";$Path=\"/acme/ammo\";$Domain=\"." + localHostAddr  + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                "/acme/ammo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                new CookieTestCase("",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                "",
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   200
                "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                "/acme/parts"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // new cookie should overwrite old cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        testCases[count++] = new CookieTestCase[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   210
                "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                "/acme"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                "Part_Number=\"Rocket_Launcher_2000\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   215
                "$Version=\"1\"; Part_Number=\"Rocket_Launcher_2000\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                "/acme"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // cookies without domain attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        testPolicies[count] = CookiePolicy.ACCEPT_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        testCases[count++] = new CookieTestCase[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                "Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"",
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   225
                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                "/acme/login"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\"",
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   230
                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                "/acme/pickitem"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                new CookieTestCase("Set-Cookie2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                "Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\"",
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents: 2
diff changeset
   235
                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\"" + "; Shipping=\"FedEx\";$Path=\"/acme\"",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                "/acme/shipping"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        assert count == testCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private int testcaseDone = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private int testDone = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Our http server which is conducted by testCases array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public void request(HttpTransaction trans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (testDone < testCases[testcaseDone].length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                // still have other tests to run,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                // check the Cookie header and then redirect it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                if (testDone > 0) checkResquest(trans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                trans.addResponseHeader("Location", testCases[testcaseDone][testDone].serverPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                trans.addResponseHeader(testCases[testcaseDone][testDone].headerToken,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                        testCases[testcaseDone][testDone].cookieToSend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                testDone++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                trans.sendResponse(302, "Moved Temporarily");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                // the last test of this test case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (testDone > 0) checkResquest(trans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                testcaseDone++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                testDone = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                trans.sendResponse(200, "OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private void checkResquest(HttpTransaction trans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        String cookieHeader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        assert testDone > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        cookieHeader = trans.getRequestHeader("Cookie");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (cookieHeader != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            cookieHeader.equalsIgnoreCase(testCases[testcaseDone][testDone-1].cookieToRecv))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            System.out.printf("%15s %s\n", "PASSED:", cookieHeader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            System.out.printf("%15s %s\n", "FAILED:", cookieHeader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            System.out.printf("%15s %s\n\n", "should be:", testCases[testcaseDone][testDone-1].cookieToRecv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            badRequest = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
}