jdk/test/sun/net/www/HeaderTests.java
author alanb
Fri, 11 Jan 2013 20:19:55 +0000
changeset 15254 3997a6f357cb
parent 5506 202f599c92aa
child 30820 0d4717a011d3
permissions -rw-r--r--
8005978: shell tests need to use the $COMPILEJDK for javac, jar and other tools Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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 Tests for HeaderParser and MessageHeader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * Test of HeaderParser, MessageHeader changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * introduced for bug fix 4722333.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.net.www.HeaderParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.net.www.MessageHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.net.www.protocol.http.AuthenticationHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class HeaderTests {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static MessageHeader createMessageHeader (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        ByteArrayInputStream bis = new ByteArrayInputStream (s.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        MessageHeader h = new MessageHeader ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            h.parseHeader (bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            throw new RuntimeException ("IOException parsing header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /* String to parse */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static String s1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                "Foo: bar\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                "Fub: abc\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                "Foo:\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                "Fub: \r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                "Foo: param1=one param2=\"two\" param3 param4=\"value=4\"\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                "Fub: xparam1=one xparam2=\"two\" xparam3 xparam4=\"value=4\"\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static String s2 = "p1=1 p2=2 p3=3 p4=4 p5=5 p6=\"six\" p7=7 p8=8 p9=9 p10=10 p11=11 p12=12";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static String s3 = "p1=1, p2=2, p3=3, p4=4, p5=5, p6=\"six\", p7=7, p8=8, p9=9, p10=10, p11=11, p12=12";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static String s23_expect[][] = {{"p2","2"},{"p3","3"},{"p4","4"},{"p5","5"},{"p6","six"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                    {"p7","7"},{"p8","8"},{"p9","9"},{"p10","10"},{"p11","11"},{"p12","12"}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static String s23_expect1[][] = {{"p1","1"},{"p2","2"},{"p3","3"},{"p4","4"},{"p5","5"},{"p6","six"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                    {"p7","7"},{"p8","8"},{"p9","9"},{"p10","10"},{"p11","11"}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static String s23_expect2[][] = {{"p5","5"},{"p6","six"}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /* Expected header parser contents for Foo headers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static String s1_Foo[][][] = {{{"bar", null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                  },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                  {{"param1","one"},{"param2","two"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                   {"param3", null},{"param4","value=4"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /* Expected header parser contents for Fub headers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static String s1_Fub[][][] = {{{"abc", null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                  },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                  {{"xparam1","one"},{"xparam2","two"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                   {"xparam3", null},{"xparam4","value=4"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static void main (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        MessageHeader head = createMessageHeader (s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Iterator iter = head.multiValueIterator ("Foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        checkHeader (iter, s1_Foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        iter = head.multiValueIterator ("Fub");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        checkHeader (iter, s1_Fub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        HeaderParser hp = new HeaderParser (s2).subsequence (1,12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        check (hp, s23_expect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        hp = new HeaderParser (s3).subsequence (1,12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        check (hp, s23_expect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        hp = new HeaderParser (s3).subsequence (0,11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        check (hp, s23_expect1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        hp = new HeaderParser (s2).subsequence (4,6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        check (hp, s23_expect2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static void checkHeader (Iterator iter, String[][][] expect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for (int i=0; iter.hasNext (); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            String s = (String) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            HeaderParser p = new HeaderParser (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            boolean empty = check (p, expect[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (!empty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        static boolean check (HeaderParser p, String[][]expect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            Iterator keys = p.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            Iterator vals = p.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            boolean empty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            for (int j=0; keys.hasNext(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                empty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                String key = (String)keys.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                String ival = (String)vals.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                String val = p.findValue (key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                if (val == null && ival == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                if (!val.equals(ival)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    throw new RuntimeException ("Error " + val + "/" + ival);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                if (!expect[j][0].equals (key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    throw new RuntimeException ("Error "+key+"/" + expect[j][0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                if (expect[j][1] != null && !expect[j][1].equals (val)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    throw new RuntimeException ("Error "+val+"/" + expect[j][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return empty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}