|
1 /* |
|
2 * Copyright (c) 2018, 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 /* |
|
25 * @test |
|
26 * @summary Basic test for Expect 100-Continue ( HTTP/1.1 only ) |
|
27 * @modules java.net.http |
|
28 * jdk.httpserver |
|
29 * @library /lib/testlibrary |
|
30 * @build jdk.testlibrary.SimpleSSLContext |
|
31 * @run testng/othervm ExpectContinue |
|
32 */ |
|
33 |
|
34 import com.sun.net.httpserver.HttpExchange; |
|
35 import com.sun.net.httpserver.HttpHandler; |
|
36 import com.sun.net.httpserver.HttpServer; |
|
37 import com.sun.net.httpserver.HttpsConfigurator; |
|
38 import com.sun.net.httpserver.HttpsServer; |
|
39 import java.io.IOException; |
|
40 import java.io.InputStream; |
|
41 import java.io.OutputStream; |
|
42 import java.net.InetSocketAddress; |
|
43 import java.net.URI; |
|
44 import java.net.http.HttpClient; |
|
45 import java.net.http.HttpRequest; |
|
46 import java.net.http.HttpRequest.BodyPublishers; |
|
47 import java.net.http.HttpResponse; |
|
48 import java.net.http.HttpResponse.BodyHandlers; |
|
49 import java.util.List; |
|
50 import javax.net.ssl.SSLContext; |
|
51 import jdk.testlibrary.SimpleSSLContext; |
|
52 import org.testng.annotations.AfterTest; |
|
53 import org.testng.annotations.BeforeTest; |
|
54 import org.testng.annotations.DataProvider; |
|
55 import org.testng.annotations.Test; |
|
56 import static java.lang.System.out; |
|
57 import static org.testng.Assert.assertEquals; |
|
58 |
|
59 public class ExpectContinue { |
|
60 |
|
61 SSLContext sslContext; |
|
62 HttpServer httpTestServer; // HTTP/1.1 [ 2 servers ] |
|
63 HttpsServer httpsTestServer; // HTTPS/1.1 |
|
64 String httpURI; |
|
65 String httpsURI; |
|
66 |
|
67 @DataProvider(name = "positive") |
|
68 public Object[][] positive() { |
|
69 return new Object[][] { |
|
70 { httpURI, false, "Billy" }, |
|
71 { httpURI, false, "Bob" }, |
|
72 { httpURI, true, "Jimmy" }, |
|
73 { httpsURI, true, "Jack" }, |
|
74 }; |
|
75 } |
|
76 |
|
77 @Test(dataProvider = "positive") |
|
78 void test(String uriString, boolean expectedContinue, String data) |
|
79 throws Exception |
|
80 { |
|
81 out.printf("test(%s, %s, %s): starting%n", uriString, expectedContinue, data); |
|
82 HttpClient client = HttpClient.newBuilder() |
|
83 .sslContext(sslContext) |
|
84 .build(); |
|
85 |
|
86 URI uri = URI.create(uriString); |
|
87 HttpRequest request = HttpRequest.newBuilder(uri) |
|
88 .expectContinue(expectedContinue) |
|
89 .POST(BodyPublishers.ofString(data)) |
|
90 .build(); |
|
91 |
|
92 HttpResponse<String> response = client.send(request, |
|
93 BodyHandlers.ofString()); |
|
94 System.out.println("First response: " + response); |
|
95 assertEquals(response.statusCode(), 200); |
|
96 assertEquals(response.body(), data); |
|
97 |
|
98 // again with the same request, to ensure no Expect header duplication |
|
99 response = client.send(request, BodyHandlers.ofString()); |
|
100 System.out.println("Second response: " + response); |
|
101 assertEquals(response.statusCode(), 200); |
|
102 assertEquals(response.body(), data); |
|
103 } |
|
104 |
|
105 @Test(dataProvider = "positive") |
|
106 void testAsync(String uriString, boolean expectedContinue, String data) { |
|
107 out.printf("test(%s, %s, %s): starting%n", uriString, expectedContinue, data); |
|
108 HttpClient client = HttpClient.newBuilder() |
|
109 .sslContext(sslContext) |
|
110 .build(); |
|
111 |
|
112 URI uri = URI.create(uriString); |
|
113 HttpRequest request = HttpRequest.newBuilder(uri) |
|
114 .expectContinue(expectedContinue) |
|
115 .POST(BodyPublishers.ofString(data)) |
|
116 .build(); |
|
117 |
|
118 HttpResponse<String> response = client.sendAsync(request, |
|
119 BodyHandlers.ofString()).join(); |
|
120 System.out.println("First response: " + response); |
|
121 assertEquals(response.statusCode(), 200); |
|
122 assertEquals(response.body(), data); |
|
123 |
|
124 // again with the same request, to ensure no Expect header duplication |
|
125 response = client.sendAsync(request, BodyHandlers.ofString()).join(); |
|
126 System.out.println("Second response: " + response); |
|
127 assertEquals(response.statusCode(), 200); |
|
128 assertEquals(response.body(), data); |
|
129 } |
|
130 |
|
131 // -- Infrastructure |
|
132 |
|
133 @BeforeTest |
|
134 public void setup() throws Exception { |
|
135 sslContext = new SimpleSSLContext().get(); |
|
136 if (sslContext == null) |
|
137 throw new AssertionError("Unexpected null sslContext"); |
|
138 |
|
139 InetSocketAddress sa = new InetSocketAddress(0); |
|
140 httpTestServer = HttpServer.create(sa, 0); |
|
141 httpTestServer.createContext("/http1/ec", new Http1ExpectContinueHandler()); |
|
142 httpURI = "http://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/http1/ec"; |
|
143 |
|
144 httpsTestServer = HttpsServer.create(sa, 0); |
|
145 httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); |
|
146 httpsTestServer.createContext("/https1/ec", new Http1ExpectContinueHandler()); |
|
147 httpsURI = "https://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/https1/ec"; |
|
148 |
|
149 httpTestServer.start(); |
|
150 httpsTestServer.start(); |
|
151 } |
|
152 |
|
153 @AfterTest |
|
154 public void teardown() throws Exception { |
|
155 httpTestServer.stop(0); |
|
156 httpsTestServer.stop(0); |
|
157 } |
|
158 |
|
159 static class Http1ExpectContinueHandler implements HttpHandler { |
|
160 @Override |
|
161 public void handle(HttpExchange t) throws IOException { |
|
162 try (InputStream is = t.getRequestBody(); |
|
163 OutputStream os = t.getResponseBody()) { |
|
164 byte[] bytes = is.readAllBytes(); |
|
165 |
|
166 List<String> expect = t.getRequestHeaders().get("Expect"); |
|
167 if (expect != null && expect.size() != 1) { |
|
168 System.out.println("Server: Expect: " + expect); |
|
169 Throwable ex = new AssertionError("Expect: " + expect); |
|
170 ex.printStackTrace(); |
|
171 t.sendResponseHeaders(500, 0); |
|
172 } else { |
|
173 t.sendResponseHeaders(200, bytes.length); |
|
174 os.write(bytes); |
|
175 } |
|
176 } |
|
177 } |
|
178 } |
|
179 } |