23 |
23 |
24 /* |
24 /* |
25 * @test |
25 * @test |
26 * @bug 8175814 |
26 * @bug 8175814 |
27 * @modules jdk.incubator.httpclient java.logging jdk.httpserver |
27 * @modules jdk.incubator.httpclient java.logging jdk.httpserver |
|
28 * @library /lib/testlibrary/ / |
|
29 * @build ProxyServer |
28 * @run main/othervm -Djdk.httpclient.HttpClient.log=errors,requests,headers,trace VersionTest |
30 * @run main/othervm -Djdk.httpclient.HttpClient.log=errors,requests,headers,trace VersionTest |
29 */ |
31 */ |
30 |
32 |
31 import com.sun.net.httpserver.Headers; |
33 import com.sun.net.httpserver.Headers; |
32 import com.sun.net.httpserver.HttpContext; |
34 import com.sun.net.httpserver.HttpContext; |
34 import com.sun.net.httpserver.HttpHandler; |
36 import com.sun.net.httpserver.HttpHandler; |
35 import com.sun.net.httpserver.HttpServer; |
37 import com.sun.net.httpserver.HttpServer; |
36 import java.io.IOException; |
38 import java.io.IOException; |
37 import java.io.OutputStream; |
39 import java.io.OutputStream; |
38 import java.net.URI; |
40 import java.net.URI; |
|
41 import java.net.InetSocketAddress; |
|
42 import java.net.ProxySelector; |
39 import java.util.concurrent.Executors; |
43 import java.util.concurrent.Executors; |
40 import java.util.concurrent.ExecutorService; |
44 import java.util.concurrent.ExecutorService; |
41 import java.net.InetSocketAddress; |
45 import java.net.InetSocketAddress; |
42 import jdk.incubator.http.HttpClient; |
46 import jdk.incubator.http.HttpClient; |
43 import jdk.incubator.http.HttpRequest; |
47 import jdk.incubator.http.HttpRequest; |
50 |
54 |
51 /** |
55 /** |
52 */ |
56 */ |
53 public class VersionTest { |
57 public class VersionTest { |
54 static HttpServer s1 ; |
58 static HttpServer s1 ; |
|
59 static ProxyServer proxy; |
55 static ExecutorService executor; |
60 static ExecutorService executor; |
56 static int port; |
61 static int port; |
57 static HttpClient client; |
62 static InetSocketAddress proxyAddr; |
|
63 static HttpClient client, clientWithProxy; |
58 static URI uri; |
64 static URI uri; |
59 static volatile boolean error = false; |
65 static volatile boolean error = false; |
60 |
66 |
61 public static void main(String[] args) throws Exception { |
67 public static void main(String[] args) throws Exception { |
62 initServer(); |
68 initServer(); |
63 |
69 |
64 client = HttpClient.newBuilder() |
70 client = HttpClient.newBuilder() |
65 .executor(executor) |
71 .executor(executor) |
66 .build(); |
72 .build(); |
|
73 |
|
74 clientWithProxy = HttpClient.newBuilder() |
|
75 .executor(executor) |
|
76 .proxy(ProxySelector.of(proxyAddr)) |
|
77 .build(); |
|
78 |
67 // first check that the version is HTTP/2 |
79 // first check that the version is HTTP/2 |
68 if (client.version() != HttpClient.Version.HTTP_2) { |
80 if (client.version() != HttpClient.Version.HTTP_2) { |
69 throw new RuntimeException("Default version not HTTP_2"); |
81 throw new RuntimeException("Default version not HTTP_2"); |
70 } |
82 } |
71 try { |
83 try { |
72 test(HTTP_1_1); |
84 test(HTTP_1_1, false); |
73 test(HTTP_2); |
85 test(HTTP_2, false); |
|
86 test(HTTP_2, true); |
74 } finally { |
87 } finally { |
75 s1.stop(0); |
88 s1.stop(0); |
76 executor.shutdownNow(); |
89 executor.shutdownNow(); |
77 } |
90 } |
78 if (error) |
91 if (error) |
79 throw new RuntimeException(); |
92 throw new RuntimeException(); |
80 } |
93 } |
81 |
94 |
82 public static void test(HttpClient.Version version) throws Exception { |
95 public static void test(HttpClient.Version version, boolean proxy) throws Exception { |
83 HttpRequest r = HttpRequest.newBuilder(uri) |
96 HttpRequest r = HttpRequest.newBuilder(uri) |
84 .version(version) |
97 .version(version) |
85 .GET() |
98 .GET() |
86 .build(); |
99 .build(); |
87 HttpResponse<Void> resp = client.send(r, discard(null)); |
100 HttpClient c = proxy ? clientWithProxy : client; |
|
101 HttpResponse<Void> resp = c.send(r, discard(null)); |
88 System.out.printf("Client: response is %d\n", resp.statusCode()); |
102 System.out.printf("Client: response is %d\n", resp.statusCode()); |
89 if (resp.version() != HTTP_1_1) { |
103 if (resp.version() != HTTP_1_1) { |
90 throw new RuntimeException(); |
104 throw new RuntimeException(); |
91 } |
105 } |
92 //System.out.printf("Client: response body is %s\n", resp.body()); |
106 //System.out.printf("Client: response body is %s\n", resp.body()); |
104 s1.start(); |
118 s1.start(); |
105 |
119 |
106 port = s1.getAddress().getPort(); |
120 port = s1.getAddress().getPort(); |
107 uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/foo"); |
121 uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/foo"); |
108 System.out.println("HTTP server port = " + port); |
122 System.out.println("HTTP server port = " + port); |
|
123 proxy = new ProxyServer(0, false); |
|
124 int proxyPort = proxy.getPort(); |
|
125 proxyAddr = new InetSocketAddress("127.0.0.1", proxyPort); |
109 } |
126 } |
110 } |
127 } |
111 |
128 |
112 class Handler implements HttpHandler { |
129 class Handler implements HttpHandler { |
113 int counter = 0; |
130 int counter = 0; |
115 void checkHeader(Headers h) { |
132 void checkHeader(Headers h) { |
116 counter++; |
133 counter++; |
117 if (counter == 1 && h.containsKey("Upgrade")) { |
134 if (counter == 1 && h.containsKey("Upgrade")) { |
118 VersionTest.error = true; |
135 VersionTest.error = true; |
119 } |
136 } |
120 if (counter > 1 && !h.containsKey("Upgrade")) { |
137 if (counter == 2 && !h.containsKey("Upgrade")) { |
|
138 VersionTest.error = true; |
|
139 } |
|
140 if (counter == 3 && h.containsKey("Upgrade")) { |
121 VersionTest.error = true; |
141 VersionTest.error = true; |
122 } |
142 } |
123 } |
143 } |
124 |
144 |
125 @Override |
145 @Override |