|
1 /* |
|
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
|
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 import java.io.IOException; |
|
25 import java.io.InputStream; |
|
26 import java.net.InetSocketAddress; |
|
27 import java.util.concurrent.Executor; |
|
28 import java.util.concurrent.Executors; |
|
29 import com.sun.net.httpserver.HttpExchange; |
|
30 import com.sun.net.httpserver.HttpHandler; |
|
31 import com.sun.net.httpserver.HttpServer; |
|
32 import com.sun.net.httpserver.HttpsConfigurator; |
|
33 import com.sun.net.httpserver.HttpsServer; |
|
34 import jdk.incubator.http.HttpClient; |
|
35 import javax.net.ssl.SSLContext; |
|
36 import jdk.testlibrary.SimpleSSLContext; |
|
37 import org.testng.annotations.AfterTest; |
|
38 import org.testng.annotations.BeforeTest; |
|
39 import org.testng.annotations.DataProvider; |
|
40 |
|
41 public abstract class AbstractNoBody { |
|
42 |
|
43 SSLContext sslContext; |
|
44 HttpServer httpTestServer; // HTTP/1.1 [ 4 servers ] |
|
45 HttpsServer httpsTestServer; // HTTPS/1.1 |
|
46 Http2TestServer http2TestServer; // HTTP/2 ( h2c ) |
|
47 Http2TestServer https2TestServer; // HTTP/2 ( h2 ) |
|
48 String httpURI_fixed; |
|
49 String httpURI_chunk; |
|
50 String httpsURI_fixed; |
|
51 String httpsURI_chunk; |
|
52 String http2URI_fixed; |
|
53 String http2URI_chunk; |
|
54 String https2URI_fixed; |
|
55 String https2URI_chunk; |
|
56 |
|
57 static final String SIMPLE_STRING = "Hello world. Goodbye world"; |
|
58 static final int ITERATION_COUNT = 10; |
|
59 // a shared executor helps reduce the amount of threads created by the test |
|
60 static final Executor executor = Executors.newCachedThreadPool(); |
|
61 |
|
62 @DataProvider(name = "variants") |
|
63 public Object[][] variants() { |
|
64 return new Object[][]{ |
|
65 { httpURI_fixed, false }, |
|
66 { httpURI_chunk, false }, |
|
67 { httpsURI_fixed, false }, |
|
68 { httpsURI_chunk, false }, |
|
69 { http2URI_fixed, false }, |
|
70 { http2URI_chunk, false }, |
|
71 { https2URI_fixed, false,}, |
|
72 { https2URI_chunk, false }, |
|
73 |
|
74 { httpURI_fixed, true }, |
|
75 { httpURI_chunk, true }, |
|
76 { httpsURI_fixed, true }, |
|
77 { httpsURI_chunk, true }, |
|
78 { http2URI_fixed, true }, |
|
79 { http2URI_chunk, true }, |
|
80 { https2URI_fixed, true,}, |
|
81 { https2URI_chunk, true }, |
|
82 }; |
|
83 } |
|
84 |
|
85 HttpClient newHttpClient() { |
|
86 return HttpClient.newBuilder() |
|
87 .executor(executor) |
|
88 .sslContext(sslContext) |
|
89 .build(); |
|
90 } |
|
91 |
|
92 @BeforeTest |
|
93 public void setup() throws Exception { |
|
94 sslContext = new SimpleSSLContext().get(); |
|
95 if (sslContext == null) |
|
96 throw new AssertionError("Unexpected null sslContext"); |
|
97 |
|
98 // HTTP/1.1 |
|
99 HttpHandler h1_fixedLengthNoBodyHandler = new HTTP1_FixedLengthNoBodyHandler(); |
|
100 HttpHandler h1_chunkNoBodyHandler = new HTTP1_ChunkedNoBodyHandler(); |
|
101 InetSocketAddress sa = new InetSocketAddress("localhost", 0); |
|
102 httpTestServer = HttpServer.create(sa, 0); |
|
103 httpTestServer.createContext("/http1/noBodyFixed", h1_fixedLengthNoBodyHandler); |
|
104 httpTestServer.createContext("/http1/noBodyChunk", h1_chunkNoBodyHandler); |
|
105 httpURI_fixed = "http://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/http1/noBodyFixed"; |
|
106 httpURI_chunk = "http://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/http1/noBodyChunk"; |
|
107 |
|
108 httpsTestServer = HttpsServer.create(sa, 0); |
|
109 httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); |
|
110 httpsTestServer.createContext("/https1/noBodyFixed", h1_fixedLengthNoBodyHandler); |
|
111 httpsTestServer.createContext("/https1/noBodyChunk", h1_chunkNoBodyHandler); |
|
112 httpsURI_fixed = "https://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/https1/noBodyFixed"; |
|
113 httpsURI_chunk = "https://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/https1/noBodyChunk"; |
|
114 |
|
115 // HTTP/2 |
|
116 Http2Handler h2_fixedLengthNoBodyHandler = new HTTP2_FixedLengthNoBodyHandler(); |
|
117 Http2Handler h2_chunkedNoBodyHandler = new HTTP2_ChunkedNoBodyHandler(); |
|
118 |
|
119 http2TestServer = new Http2TestServer("127.0.0.1", false, 0); |
|
120 http2TestServer.addHandler(h2_fixedLengthNoBodyHandler, "/http2/noBodyFixed"); |
|
121 http2TestServer.addHandler(h2_chunkedNoBodyHandler, "/http2/noBodyChunk"); |
|
122 int port = http2TestServer.getAddress().getPort(); |
|
123 http2URI_fixed = "http://127.0.0.1:" + port + "/http2/noBodyFixed"; |
|
124 http2URI_chunk = "http://127.0.0.1:" + port + "/http2/noBodyChunk"; |
|
125 |
|
126 https2TestServer = new Http2TestServer("127.0.0.1", true, 0); |
|
127 https2TestServer.addHandler(h2_fixedLengthNoBodyHandler, "/https2/noBodyFixed"); |
|
128 https2TestServer.addHandler(h2_chunkedNoBodyHandler, "/https2/noBodyChunk"); |
|
129 port = https2TestServer.getAddress().getPort(); |
|
130 https2URI_fixed = "https://127.0.0.1:" + port + "/https2/noBodyFixed"; |
|
131 https2URI_chunk = "https://127.0.0.1:" + port + "/https2/noBodyChunk"; |
|
132 |
|
133 httpTestServer.start(); |
|
134 httpsTestServer.start(); |
|
135 http2TestServer.start(); |
|
136 https2TestServer.start(); |
|
137 } |
|
138 |
|
139 @AfterTest |
|
140 public void teardown() throws Exception { |
|
141 httpTestServer.stop(0); |
|
142 httpsTestServer.stop(0); |
|
143 http2TestServer.stop(); |
|
144 https2TestServer.stop(); |
|
145 } |
|
146 |
|
147 static class HTTP1_FixedLengthNoBodyHandler implements HttpHandler { |
|
148 @Override |
|
149 public void handle(HttpExchange t) throws IOException { |
|
150 //out.println("NoBodyHandler received request to " + t.getRequestURI()); |
|
151 try (InputStream is = t.getRequestBody()) { |
|
152 is.readAllBytes(); |
|
153 } |
|
154 t.sendResponseHeaders(200, -1); // no body |
|
155 } |
|
156 } |
|
157 |
|
158 static class HTTP1_ChunkedNoBodyHandler implements HttpHandler { |
|
159 @Override |
|
160 public void handle(HttpExchange t) throws IOException { |
|
161 //out.println("NoBodyHandler received request to " + t.getRequestURI()); |
|
162 try (InputStream is = t.getRequestBody()) { |
|
163 is.readAllBytes(); |
|
164 } |
|
165 t.sendResponseHeaders(200, 0); // chunked |
|
166 t.getResponseBody().close(); // write nothing |
|
167 } |
|
168 } |
|
169 |
|
170 static class HTTP2_FixedLengthNoBodyHandler implements Http2Handler { |
|
171 @Override |
|
172 public void handle(Http2TestExchange t) throws IOException { |
|
173 //out.println("NoBodyHandler received request to " + t.getRequestURI()); |
|
174 try (InputStream is = t.getRequestBody()) { |
|
175 is.readAllBytes(); |
|
176 } |
|
177 t.sendResponseHeaders(200, 0); |
|
178 } |
|
179 } |
|
180 |
|
181 static class HTTP2_ChunkedNoBodyHandler implements Http2Handler { |
|
182 @Override |
|
183 public void handle(Http2TestExchange t) throws IOException { |
|
184 //out.println("NoBodyHandler received request to " + t.getRequestURI()); |
|
185 try (InputStream is = t.getRequestBody()) { |
|
186 is.readAllBytes(); |
|
187 } |
|
188 t.sendResponseHeaders(200, -1); |
|
189 t.getResponseBody().close(); // write nothing |
|
190 } |
|
191 } |
|
192 } |