author | chegar |
Wed, 08 Nov 2017 16:39:47 +0000 | |
branch | http-client-branch |
changeset 55788 | 5a59fa72a72a |
parent 55763 | 634d8e14c172 |
child 55973 | 4d9b002587db |
permissions | -rw-r--r-- |
55763 | 1 |
/* |
55788
5a59fa72a72a
http-client-branch: additional Header test scenarios
chegar
parents:
55763
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
55763 | 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 |
* |
|
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 |
* @bug 8087112 |
|
27 |
* @summary Basic test for headers |
|
28 |
*/ |
|
29 |
||
30 |
import jdk.incubator.http.HttpHeaders; |
|
31 |
import jdk.incubator.http.HttpRequest; |
|
32 |
import java.net.URI; |
|
33 |
import java.util.List; |
|
34 |
import java.util.Iterator; |
|
35 |
||
36 |
public class HeadersTest2 { |
|
37 |
static URI uri = URI.create("http://www.foo.com/"); |
|
38 |
||
39 |
static class CompareTest { |
|
40 |
boolean succeed; |
|
41 |
List<String> nameValues1; |
|
42 |
List<String> nameValues2; |
|
43 |
||
44 |
||
45 |
/** |
|
46 |
* Each list contains header-name, header-value, header-name, header-value |
|
47 |
* sequences. The test creates two requests with the two lists |
|
48 |
* and compares the HttpHeaders objects returned from the requests |
|
49 |
*/ |
|
50 |
CompareTest(boolean succeed, List<String> l1, List<String> l2) { |
|
51 |
this.succeed = succeed; |
|
52 |
this.nameValues1 = l1; |
|
53 |
this.nameValues2 = l2; |
|
54 |
} |
|
55 |
||
56 |
public void run() { |
|
57 |
HttpRequest r1 = getRequest(nameValues1); |
|
58 |
HttpRequest r2 = getRequest(nameValues2); |
|
59 |
HttpHeaders h1 = r1.headers(); |
|
60 |
HttpHeaders h2 = r2.headers(); |
|
61 |
boolean equal = h1.equals(h2); |
|
62 |
if (equal && !succeed) { |
|
63 |
System.err.println("Expected failure"); |
|
64 |
print(nameValues1); |
|
65 |
print(nameValues2); |
|
66 |
throw new RuntimeException(); |
|
67 |
} else if (!equal && succeed) { |
|
68 |
System.err.println("Expected success"); |
|
69 |
print(nameValues1); |
|
70 |
print(nameValues2); |
|
71 |
throw new RuntimeException(); |
|
72 |
} |
|
55788
5a59fa72a72a
http-client-branch: additional Header test scenarios
chegar
parents:
55763
diff
changeset
|
73 |
|
5a59fa72a72a
http-client-branch: additional Header test scenarios
chegar
parents:
55763
diff
changeset
|
74 |
// Ensures that headers never equal a non-HttpHeaders type |
5a59fa72a72a
http-client-branch: additional Header test scenarios
chegar
parents:
55763
diff
changeset
|
75 |
if (h1.equals(new Object())) |
5a59fa72a72a
http-client-branch: additional Header test scenarios
chegar
parents:
55763
diff
changeset
|
76 |
throw new RuntimeException("Unexpected h1 equals Object"); |
5a59fa72a72a
http-client-branch: additional Header test scenarios
chegar
parents:
55763
diff
changeset
|
77 |
|
5a59fa72a72a
http-client-branch: additional Header test scenarios
chegar
parents:
55763
diff
changeset
|
78 |
if (h2.equals(r1)) |
5a59fa72a72a
http-client-branch: additional Header test scenarios
chegar
parents:
55763
diff
changeset
|
79 |
throw new RuntimeException("Unexpected h2 equals r1"); |
55763 | 80 |
} |
81 |
||
82 |
static void print(List<String> list) { |
|
83 |
System.err.print("{"); |
|
84 |
for (String s : list) { |
|
85 |
System.err.print(s + " "); |
|
86 |
} |
|
87 |
System.err.println("}"); |
|
88 |
} |
|
89 |
||
90 |
HttpRequest getRequest(List<String> headers) { |
|
91 |
HttpRequest.Builder builder = HttpRequest.newBuilder(uri); |
|
92 |
Iterator<String> iterator = headers.iterator(); |
|
93 |
while (iterator.hasNext()) { |
|
94 |
String name = iterator.next(); |
|
95 |
String value = iterator.next(); |
|
96 |
builder.header(name, value); |
|
97 |
} |
|
98 |
return builder.GET().build(); |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
static CompareTest test(boolean s, List<String> l1, List<String> l2) { |
|
103 |
return new CompareTest(s, l1, l2); |
|
104 |
} |
|
105 |
||
106 |
static CompareTest[] compareTests = new CompareTest[] { |
|
107 |
test(true, List.of("Dontent-length", "99"), List.of("dontent-length", "99")), |
|
108 |
test(false, List.of("Dontent-length", "99"), List.of("dontent-length", "100")), |
|
109 |
test(false, List.of("Name1", "val1", "Name1", "val2", "name1", "val3"), |
|
110 |
List.of("Name1", "val1", "Name1", "val2")), |
|
111 |
test(true, List.of("Name1", "val1", "Name1", "val2", "name1", "val3"), |
|
112 |
List.of("NaMe1", "val1", "NAme1", "val2", "name1", "val3")) |
|
113 |
}; |
|
114 |
||
115 |
public static void main(String[] args) { |
|
116 |
for (CompareTest test : compareTests) { |
|
117 |
test.run(); |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |