2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
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
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
30820
|
26 |
* @modules java.base/sun.net.www
|
2
|
27 |
* @summary Tests for HeaderParser and MessageHeader
|
|
28 |
*
|
|
29 |
* Test of HeaderParser, MessageHeader changes
|
|
30 |
* introduced for bug fix 4722333.
|
|
31 |
*/
|
|
32 |
|
|
33 |
import sun.net.www.HeaderParser;
|
|
34 |
import sun.net.www.MessageHeader;
|
|
35 |
import java.io.*;
|
|
36 |
import java.util.Iterator;
|
|
37 |
|
|
38 |
public class HeaderTests {
|
|
39 |
|
|
40 |
static MessageHeader createMessageHeader (String s) {
|
|
41 |
ByteArrayInputStream bis = new ByteArrayInputStream (s.getBytes());
|
|
42 |
MessageHeader h = new MessageHeader ();
|
|
43 |
try {
|
|
44 |
h.parseHeader (bis);
|
|
45 |
} catch (IOException e) {
|
|
46 |
throw new RuntimeException ("IOException parsing header");
|
|
47 |
}
|
|
48 |
return h;
|
|
49 |
}
|
|
50 |
|
|
51 |
/* String to parse */
|
|
52 |
|
|
53 |
static String s1 =
|
|
54 |
"Foo: bar\r\n"+
|
|
55 |
"Fub: abc\r\n"+
|
|
56 |
"Foo:\r\n"+
|
|
57 |
"Fub: \r\n"+
|
|
58 |
"Foo: param1=one param2=\"two\" param3 param4=\"value=4\"\r\n"+
|
|
59 |
"Fub: xparam1=one xparam2=\"two\" xparam3 xparam4=\"value=4\"\r\n";
|
|
60 |
|
|
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";
|
|
62 |
|
|
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";
|
|
64 |
|
|
65 |
static String s23_expect[][] = {{"p2","2"},{"p3","3"},{"p4","4"},{"p5","5"},{"p6","six"},
|
|
66 |
{"p7","7"},{"p8","8"},{"p9","9"},{"p10","10"},{"p11","11"},{"p12","12"}};
|
|
67 |
|
|
68 |
static String s23_expect1[][] = {{"p1","1"},{"p2","2"},{"p3","3"},{"p4","4"},{"p5","5"},{"p6","six"},
|
|
69 |
{"p7","7"},{"p8","8"},{"p9","9"},{"p10","10"},{"p11","11"}};
|
|
70 |
|
|
71 |
static String s23_expect2[][] = {{"p5","5"},{"p6","six"}};
|
|
72 |
|
|
73 |
/* Expected header parser contents for Foo headers */
|
|
74 |
static String s1_Foo[][][] = {{{"bar", null}
|
|
75 |
},
|
|
76 |
{{"param1","one"},{"param2","two"},
|
|
77 |
{"param3", null},{"param4","value=4"}
|
|
78 |
}
|
|
79 |
};
|
|
80 |
|
|
81 |
/* Expected header parser contents for Fub headers */
|
|
82 |
static String s1_Fub[][][] = {{{"abc", null}
|
|
83 |
},
|
|
84 |
{{"xparam1","one"},{"xparam2","two"},
|
|
85 |
{"xparam3", null},{"xparam4","value=4"}
|
|
86 |
}
|
|
87 |
};
|
|
88 |
|
|
89 |
public static void main (String args[]) {
|
|
90 |
MessageHeader head = createMessageHeader (s1);
|
|
91 |
Iterator iter = head.multiValueIterator ("Foo");
|
|
92 |
checkHeader (iter, s1_Foo);
|
|
93 |
iter = head.multiValueIterator ("Fub");
|
|
94 |
checkHeader (iter, s1_Fub);
|
|
95 |
|
|
96 |
HeaderParser hp = new HeaderParser (s2).subsequence (1,12);
|
|
97 |
check (hp, s23_expect);
|
|
98 |
|
|
99 |
hp = new HeaderParser (s3).subsequence (1,12);
|
|
100 |
check (hp, s23_expect);
|
|
101 |
|
|
102 |
hp = new HeaderParser (s3).subsequence (0,11);
|
|
103 |
check (hp, s23_expect1);
|
|
104 |
|
|
105 |
hp = new HeaderParser (s2).subsequence (4,6);
|
|
106 |
check (hp, s23_expect2);
|
|
107 |
}
|
|
108 |
|
|
109 |
static void checkHeader (Iterator iter, String[][][] expect) {
|
|
110 |
for (int i=0; iter.hasNext (); ) {
|
|
111 |
String s = (String) iter.next();
|
|
112 |
HeaderParser p = new HeaderParser (s);
|
|
113 |
boolean empty = check (p, expect[i]);
|
|
114 |
if (!empty) {
|
|
115 |
i++;
|
|
116 |
}
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
static boolean check (HeaderParser p, String[][]expect) {
|
|
121 |
Iterator keys = p.keys();
|
|
122 |
Iterator vals = p.values();
|
|
123 |
boolean empty = true;
|
|
124 |
for (int j=0; keys.hasNext(); j++) {
|
|
125 |
empty = false;
|
|
126 |
String key = (String)keys.next();
|
|
127 |
String ival = (String)vals.next();
|
|
128 |
String val = p.findValue (key);
|
|
129 |
if (val == null && ival == null)
|
|
130 |
continue;
|
|
131 |
if (!val.equals(ival)) {
|
|
132 |
throw new RuntimeException ("Error " + val + "/" + ival);
|
|
133 |
}
|
|
134 |
if (!expect[j][0].equals (key)) {
|
|
135 |
throw new RuntimeException ("Error "+key+"/" + expect[j][0]);
|
|
136 |
}
|
|
137 |
if (expect[j][1] != null && !expect[j][1].equals (val)) {
|
|
138 |
throw new RuntimeException ("Error "+val+"/" + expect[j][1]);
|
|
139 |
}
|
|
140 |
}
|
|
141 |
return empty;
|
|
142 |
}
|
|
143 |
}
|