author | chegar |
Tue, 19 Jun 2018 09:13:58 +0100 | |
branch | http-client-branch |
changeset 56771 | 73a6534bce94 |
parent 56751 | aa677dd4c174 |
child 56795 | 03ece2518428 |
permissions | -rw-r--r-- |
56751
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
1 |
/* |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
4 |
* |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
8 |
* |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
13 |
* accompanied this code). |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
14 |
* |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
18 |
* |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
21 |
* questions. |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
22 |
*/ |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
23 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
24 |
/* |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
25 |
* @test |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
26 |
* @summary Verify that some special headers - such as User-Agent |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
27 |
* can be specified by the caller. |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
28 |
* @bug 8203771 |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
29 |
* @modules java.base/sun.net.www.http |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
30 |
* java.net.http/jdk.internal.net.http.common |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
31 |
* java.net.http/jdk.internal.net.http.frame |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
32 |
* java.net.http/jdk.internal.net.http.hpack |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
33 |
* java.logging |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
34 |
* jdk.httpserver |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
35 |
* @library /lib/testlibrary http2/server |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
36 |
* @build Http2TestServer HttpServerAdapters SpecialHeadersTest |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
37 |
* @build jdk.testlibrary.SimpleSSLContext |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
38 |
* @run testng/othervm |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
39 |
* -Djdk.httpclient.HttpClient.log=requests,headers,errors |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
40 |
* SpecialHeadersTest |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
41 |
*/ |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
42 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
43 |
import com.sun.net.httpserver.HttpServer; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
44 |
import com.sun.net.httpserver.HttpsConfigurator; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
45 |
import com.sun.net.httpserver.HttpsServer; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
46 |
import jdk.testlibrary.SimpleSSLContext; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
47 |
import org.testng.annotations.AfterTest; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
48 |
import org.testng.annotations.BeforeTest; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
49 |
import org.testng.annotations.DataProvider; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
50 |
import org.testng.annotations.Test; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
51 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
52 |
import javax.net.ssl.SSLContext; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
53 |
import java.io.IOException; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
54 |
import java.io.InputStream; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
55 |
import java.io.OutputStream; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
56 |
import java.net.InetAddress; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
57 |
import java.net.InetSocketAddress; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
58 |
import java.net.URI; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
59 |
import java.net.http.HttpClient; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
60 |
import java.net.http.HttpRequest; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
61 |
import java.net.http.HttpResponse; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
62 |
import java.net.http.HttpResponse.BodyHandlers; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
63 |
import java.security.AccessController; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
64 |
import java.security.PrivilegedAction; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
65 |
import java.util.ArrayList; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
66 |
import java.util.Arrays; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
67 |
import java.util.List; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
68 |
import java.util.Locale; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
69 |
import java.util.Map; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
70 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
71 |
import static java.lang.System.err; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
72 |
import static java.lang.System.out; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
73 |
import static java.net.http.HttpClient.Builder.NO_PROXY; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
74 |
import static java.nio.charset.StandardCharsets.US_ASCII; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
75 |
import static org.testng.Assert.assertEquals; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
76 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
77 |
public class SpecialHeadersTest implements HttpServerAdapters { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
78 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
79 |
SSLContext sslContext; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
80 |
HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ] |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
81 |
HttpTestServer httpsTestServer; // HTTPS/1.1 |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
82 |
HttpTestServer http2TestServer; // HTTP/2 ( h2c ) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
83 |
HttpTestServer https2TestServer; // HTTP/2 ( h2 ) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
84 |
String httpURI; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
85 |
String httpsURI; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
86 |
String http2URI; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
87 |
String https2URI; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
88 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
89 |
static final String[][] headerNamesAndValues = new String[][]{ |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
90 |
{"User-Agent: <DEFAULT>"}, |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
91 |
{"User-Agent: camel-cased"}, |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
92 |
{"user-agent: all-lower-case"}, |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
93 |
{"user-Agent: mixed"}, |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
94 |
}; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
95 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
96 |
@DataProvider(name = "variants") |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
97 |
public Object[][] variants() { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
98 |
List<Object[]> list = new ArrayList<>(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
99 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
100 |
for (boolean sameClient : new boolean[] { false, true }) { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
101 |
Arrays.asList(headerNamesAndValues).stream() |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
102 |
.map(e -> new Object[] {httpURI, e[0], sameClient}) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
103 |
.forEach(list::add); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
104 |
Arrays.asList(headerNamesAndValues).stream() |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
105 |
.map(e -> new Object[] {httpsURI, e[0], sameClient}) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
106 |
.forEach(list::add); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
107 |
Arrays.asList(headerNamesAndValues).stream() |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
108 |
.map(e -> new Object[] {http2URI, e[0], sameClient}) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
109 |
.forEach(list::add); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
110 |
Arrays.asList(headerNamesAndValues).stream() |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
111 |
.map(e -> new Object[] {https2URI, e[0], sameClient}) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
112 |
.forEach(list::add); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
113 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
114 |
return list.stream().toArray(Object[][]::new); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
115 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
116 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
117 |
static final int ITERATION_COUNT = 3; // checks upgrade and re-use |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
118 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
119 |
static String userAgent() { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
120 |
return "Java-http-client/" + System.getProperty("java.version"); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
121 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
122 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
123 |
static final Map<String, String> DEFAULTS = Map.of("USER-AGENT", userAgent()); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
124 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
125 |
@Test(dataProvider = "variants") |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
126 |
void test(String uriString, String headerNameAndValue, boolean sameClient) throws Exception { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
127 |
out.println("\n--- Starting "); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
128 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
129 |
int index = headerNameAndValue.indexOf(":"); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
130 |
String name = headerNameAndValue.substring(0, index); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
131 |
String v = headerNameAndValue.substring(index+1).trim(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
132 |
String key = name.toUpperCase(Locale.ROOT); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
133 |
boolean useDefault = "<DEFAULT>".equals(v); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
134 |
String value = useDefault ? DEFAULTS.get(key) : v; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
135 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
136 |
URI uri = URI.create(uriString+"?name="+key); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
137 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
138 |
HttpClient client = null; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
139 |
for (int i=0; i< ITERATION_COUNT; i++) { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
140 |
if (!sameClient || client == null) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
141 |
client = HttpClient.newBuilder() |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
142 |
.proxy(NO_PROXY) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
143 |
.sslContext(sslContext) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
144 |
.build(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
145 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
146 |
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
147 |
if (!useDefault) { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
148 |
requestBuilder.header(name, value); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
149 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
150 |
HttpRequest request = requestBuilder.build(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
151 |
HttpResponse<String> resp = client.send(request, BodyHandlers.ofString()); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
152 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
153 |
out.println("Got response: " + resp); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
154 |
out.println("Got body: " + resp.body()); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
155 |
assertEquals(resp.statusCode(), 200, |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
156 |
"Expected 200, got:" + resp.statusCode()); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
157 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
158 |
String receivedHeaderString = value == null ? null |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
159 |
: resp.headers().firstValue("X-"+key).get(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
160 |
out.println("Got X-" + key + ": " + resp.headers().allValues("X-"+key)); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
161 |
if (value != null) { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
162 |
assertEquals(receivedHeaderString, value); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
163 |
assertEquals(resp.headers().allValues("X-"+key), List.of(value)); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
164 |
} else { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
165 |
assertEquals(resp.headers().allValues("X-"+key).size(), 0); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
166 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
167 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
168 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
169 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
170 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
171 |
@Test(dataProvider = "variants") |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
172 |
void testAsync(String uriString, String headerNameAndValue, boolean sameClient) { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
173 |
out.println("\n--- Starting "); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
174 |
int index = headerNameAndValue.indexOf(":"); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
175 |
String name = headerNameAndValue.substring(0, index); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
176 |
String v = headerNameAndValue.substring(index+1).trim(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
177 |
String key = name.toUpperCase(Locale.ROOT); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
178 |
boolean useDefault = "<DEFAULT>".equals(v); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
179 |
String value = useDefault ? DEFAULTS.get(key) : v; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
180 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
181 |
URI uri = URI.create(uriString+"?name="+key); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
182 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
183 |
HttpClient client = null; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
184 |
for (int i=0; i< ITERATION_COUNT; i++) { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
185 |
if (!sameClient || client == null) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
186 |
client = HttpClient.newBuilder() |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
187 |
.proxy(NO_PROXY) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
188 |
.sslContext(sslContext) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
189 |
.build(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
190 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
191 |
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
192 |
if (!useDefault) { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
193 |
requestBuilder.header(name, value); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
194 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
195 |
HttpRequest request = requestBuilder.build(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
196 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
197 |
client.sendAsync(request, BodyHandlers.ofString()) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
198 |
.thenApply(response -> { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
199 |
out.println("Got response: " + response); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
200 |
out.println("Got body: " + response.body()); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
201 |
assertEquals(response.statusCode(), 200); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
202 |
return response;}) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
203 |
.thenAccept(resp -> { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
204 |
String receivedHeaderString = value == null ? null |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
205 |
: resp.headers().firstValue("X-"+key).get(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
206 |
out.println("Got X-" + key + ": " + resp.headers().allValues("X-"+key)); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
207 |
if (value != null) { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
208 |
assertEquals(receivedHeaderString, value); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
209 |
assertEquals(resp.headers().allValues("X-" + key), List.of(value)); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
210 |
} else { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
211 |
assertEquals(resp.headers().allValues("X-" + key).size(), 1); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
212 |
} }) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
213 |
.join(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
214 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
215 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
216 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
217 |
static String serverAuthority(HttpTestServer server) { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
218 |
return InetAddress.getLoopbackAddress().getHostName() + ":" |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
219 |
+ server.getAddress().getPort(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
220 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
221 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
222 |
@BeforeTest |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
223 |
public void setup() throws Exception { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
224 |
sslContext = new SimpleSSLContext().get(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
225 |
if (sslContext == null) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
226 |
throw new AssertionError("Unexpected null sslContext"); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
227 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
228 |
HttpTestHandler handler = new HttpUriStringHandler(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
229 |
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
230 |
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0)); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
231 |
httpTestServer.addHandler(handler, "/http1"); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
232 |
httpURI = "http://" + serverAuthority(httpTestServer) + "/http1"; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
233 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
234 |
HttpsServer httpsServer = HttpsServer.create(sa, 0); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
235 |
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
236 |
httpsTestServer = HttpTestServer.of(httpsServer); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
237 |
httpsTestServer.addHandler(handler, "/https1"); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
238 |
httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1"; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
239 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
240 |
http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0)); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
241 |
http2TestServer.addHandler(handler, "/http2"); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
242 |
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2"; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
243 |
|
56771 | 244 |
https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext)); |
56751
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
245 |
https2TestServer.addHandler(handler, "/https2"); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
246 |
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2"; |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
247 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
248 |
httpTestServer.start(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
249 |
httpsTestServer.start(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
250 |
http2TestServer.start(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
251 |
https2TestServer.start(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
252 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
253 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
254 |
@AfterTest |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
255 |
public void teardown() throws Exception { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
256 |
httpTestServer.stop(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
257 |
httpsTestServer.stop(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
258 |
http2TestServer.stop(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
259 |
https2TestServer.stop(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
260 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
261 |
|
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
262 |
/** A handler that returns, as its body, the exact received request URI. */ |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
263 |
static class HttpUriStringHandler implements HttpTestHandler { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
264 |
@Override |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
265 |
public void handle(HttpTestExchange t) throws IOException { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
266 |
URI uri = t.getRequestURI(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
267 |
String uriString = uri.toString(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
268 |
out.println("Http1UriStringHandler received, uri: " + uriString); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
269 |
String query = uri.getQuery(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
270 |
String headerName = query.substring(query.indexOf("=")+1).trim(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
271 |
try (InputStream is = t.getRequestBody(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
272 |
OutputStream os = t.getResponseBody()) { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
273 |
is.readAllBytes(); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
274 |
byte[] bytes = uriString.getBytes(US_ASCII); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
275 |
t.getRequestHeaders().keySet().stream() |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
276 |
.filter(headerName::equalsIgnoreCase) |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
277 |
.forEach(h -> { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
278 |
for (String v : t.getRequestHeaders().get(headerName)) { |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
279 |
t.getResponseHeaders().addHeader("X-"+h, v); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
280 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
281 |
}); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
282 |
t.sendResponseHeaders(200, bytes.length); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
283 |
os.write(bytes); |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
284 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
285 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
286 |
} |
aa677dd4c174
http-client-branch: 8203771 - It's not possible to override default user agent with java.net.http HTTPClient
dfuchs
parents:
diff
changeset
|
287 |
} |