author | chegar |
Mon, 28 Jan 2019 13:51:16 +0000 | |
changeset 53521 | 41fa3e6f2785 |
parent 50681 | 4254bed3c09d |
child 58289 | 3a79d4cccbcb |
permissions | -rw-r--r-- |
49765 | 1 |
/* |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
49765 | 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 |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
26 |
* @bug 8217429 |
49765 | 27 |
* @build DummyWebSocketServer |
28 |
* @run testng/othervm |
|
29 |
* WebSocketTest |
|
30 |
*/ |
|
31 |
||
32 |
import org.testng.annotations.AfterTest; |
|
50681 | 33 |
import org.testng.annotations.DataProvider; |
49765 | 34 |
import org.testng.annotations.Test; |
35 |
||
36 |
import java.io.IOException; |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
37 |
import java.net.Authenticator; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
38 |
import java.net.PasswordAuthentication; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
39 |
import java.net.http.HttpResponse; |
49765 | 40 |
import java.net.http.WebSocket; |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
41 |
import java.net.http.WebSocketHandshakeException; |
49765 | 42 |
import java.nio.ByteBuffer; |
43 |
import java.nio.charset.StandardCharsets; |
|
44 |
import java.util.ArrayList; |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
45 |
import java.util.Base64; |
49765 | 46 |
import java.util.List; |
47 |
import java.util.concurrent.CompletableFuture; |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
48 |
import java.util.concurrent.CompletionException; |
49765 | 49 |
import java.util.concurrent.CompletionStage; |
50 |
import java.util.concurrent.TimeUnit; |
|
50681 | 51 |
import java.util.concurrent.atomic.AtomicBoolean; |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
52 |
import java.util.function.Function; |
50681 | 53 |
import java.util.function.Supplier; |
49765 | 54 |
import java.util.stream.Collectors; |
50681 | 55 |
|
49944 | 56 |
import static java.net.http.HttpClient.Builder.NO_PROXY; |
57 |
import static java.net.http.HttpClient.newBuilder; |
|
49765 | 58 |
import static java.net.http.WebSocket.NORMAL_CLOSURE; |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
59 |
import static java.nio.charset.StandardCharsets.UTF_8; |
49765 | 60 |
import static org.testng.Assert.assertEquals; |
61 |
import static org.testng.Assert.assertThrows; |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
62 |
import static org.testng.Assert.fail; |
49765 | 63 |
|
64 |
public class WebSocketTest { |
|
65 |
||
66 |
private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class; |
|
67 |
private static final Class<IllegalStateException> ISE = IllegalStateException.class; |
|
68 |
private static final Class<IOException> IOE = IOException.class; |
|
69 |
||
70 |
/* shortcut */ |
|
71 |
private static void assertFails(Class<? extends Throwable> clazz, |
|
72 |
CompletionStage<?> stage) { |
|
73 |
Support.assertCompletesExceptionally(clazz, stage); |
|
74 |
} |
|
75 |
||
76 |
private DummyWebSocketServer server; |
|
77 |
private WebSocket webSocket; |
|
78 |
||
79 |
@AfterTest |
|
80 |
public void cleanup() { |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
81 |
System.out.println("AFTER TEST"); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
82 |
if (server != null) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
83 |
server.close(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
84 |
if (webSocket != null) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
85 |
webSocket.abort(); |
49765 | 86 |
} |
87 |
||
88 |
@Test |
|
89 |
public void illegalArgument() throws IOException { |
|
90 |
server = new DummyWebSocketServer(); |
|
91 |
server.open(); |
|
49944 | 92 |
webSocket = newBuilder().proxy(NO_PROXY).build() |
49765 | 93 |
.newWebSocketBuilder() |
94 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
95 |
.join(); |
|
96 |
||
97 |
assertFails(IAE, webSocket.sendPing(ByteBuffer.allocate(126))); |
|
98 |
assertFails(IAE, webSocket.sendPing(ByteBuffer.allocate(127))); |
|
99 |
assertFails(IAE, webSocket.sendPing(ByteBuffer.allocate(128))); |
|
100 |
assertFails(IAE, webSocket.sendPing(ByteBuffer.allocate(129))); |
|
101 |
assertFails(IAE, webSocket.sendPing(ByteBuffer.allocate(256))); |
|
102 |
||
103 |
assertFails(IAE, webSocket.sendPong(ByteBuffer.allocate(126))); |
|
104 |
assertFails(IAE, webSocket.sendPong(ByteBuffer.allocate(127))); |
|
105 |
assertFails(IAE, webSocket.sendPong(ByteBuffer.allocate(128))); |
|
106 |
assertFails(IAE, webSocket.sendPong(ByteBuffer.allocate(129))); |
|
107 |
assertFails(IAE, webSocket.sendPong(ByteBuffer.allocate(256))); |
|
108 |
||
109 |
assertFails(IOE, webSocket.sendText(Support.incompleteString(), true)); |
|
110 |
assertFails(IOE, webSocket.sendText(Support.incompleteString(), false)); |
|
111 |
assertFails(IOE, webSocket.sendText(Support.malformedString(), true)); |
|
112 |
assertFails(IOE, webSocket.sendText(Support.malformedString(), false)); |
|
113 |
||
114 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.stringWithNBytes(124))); |
|
115 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.stringWithNBytes(125))); |
|
116 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.stringWithNBytes(128))); |
|
117 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.stringWithNBytes(256))); |
|
118 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.stringWithNBytes(257))); |
|
119 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.stringWith2NBytes((123 / 2) + 1))); |
|
120 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.malformedString())); |
|
121 |
assertFails(IAE, webSocket.sendClose(NORMAL_CLOSURE, Support.incompleteString())); |
|
122 |
||
123 |
assertFails(IAE, webSocket.sendClose(-2, "a reason")); |
|
124 |
assertFails(IAE, webSocket.sendClose(-1, "a reason")); |
|
125 |
assertFails(IAE, webSocket.sendClose(0, "a reason")); |
|
126 |
assertFails(IAE, webSocket.sendClose(1, "a reason")); |
|
127 |
assertFails(IAE, webSocket.sendClose(500, "a reason")); |
|
128 |
assertFails(IAE, webSocket.sendClose(998, "a reason")); |
|
129 |
assertFails(IAE, webSocket.sendClose(999, "a reason")); |
|
130 |
assertFails(IAE, webSocket.sendClose(1002, "a reason")); |
|
131 |
assertFails(IAE, webSocket.sendClose(1003, "a reason")); |
|
132 |
assertFails(IAE, webSocket.sendClose(1006, "a reason")); |
|
133 |
assertFails(IAE, webSocket.sendClose(1007, "a reason")); |
|
134 |
assertFails(IAE, webSocket.sendClose(1009, "a reason")); |
|
135 |
assertFails(IAE, webSocket.sendClose(1010, "a reason")); |
|
136 |
assertFails(IAE, webSocket.sendClose(1012, "a reason")); |
|
137 |
assertFails(IAE, webSocket.sendClose(1013, "a reason")); |
|
138 |
assertFails(IAE, webSocket.sendClose(1015, "a reason")); |
|
139 |
assertFails(IAE, webSocket.sendClose(5000, "a reason")); |
|
140 |
assertFails(IAE, webSocket.sendClose(32768, "a reason")); |
|
141 |
assertFails(IAE, webSocket.sendClose(65535, "a reason")); |
|
142 |
assertFails(IAE, webSocket.sendClose(65536, "a reason")); |
|
143 |
assertFails(IAE, webSocket.sendClose(Integer.MAX_VALUE, "a reason")); |
|
144 |
assertFails(IAE, webSocket.sendClose(Integer.MIN_VALUE, "a reason")); |
|
145 |
||
146 |
assertThrows(IAE, () -> webSocket.request(Integer.MIN_VALUE)); |
|
147 |
assertThrows(IAE, () -> webSocket.request(Long.MIN_VALUE)); |
|
148 |
assertThrows(IAE, () -> webSocket.request(-1)); |
|
149 |
assertThrows(IAE, () -> webSocket.request(0)); |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
150 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
151 |
server.close(); |
49765 | 152 |
} |
153 |
||
154 |
@Test |
|
155 |
public void partialBinaryThenText() throws IOException { |
|
156 |
server = new DummyWebSocketServer(); |
|
157 |
server.open(); |
|
49944 | 158 |
webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder() |
49765 | 159 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
160 |
.join(); |
|
161 |
webSocket.sendBinary(ByteBuffer.allocate(16), false).join(); |
|
162 |
assertFails(ISE, webSocket.sendText("text", false)); |
|
163 |
assertFails(ISE, webSocket.sendText("text", true)); |
|
164 |
// Pings & Pongs are fine |
|
165 |
webSocket.sendPing(ByteBuffer.allocate(125)).join(); |
|
166 |
webSocket.sendPong(ByteBuffer.allocate(125)).join(); |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
167 |
server.close(); |
49765 | 168 |
} |
169 |
||
170 |
@Test |
|
171 |
public void partialTextThenBinary() throws IOException { |
|
172 |
server = new DummyWebSocketServer(); |
|
173 |
server.open(); |
|
49944 | 174 |
webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder() |
49765 | 175 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
176 |
.join(); |
|
177 |
||
178 |
webSocket.sendText("text", false).join(); |
|
179 |
assertFails(ISE, webSocket.sendBinary(ByteBuffer.allocate(16), false)); |
|
180 |
assertFails(ISE, webSocket.sendBinary(ByteBuffer.allocate(16), true)); |
|
181 |
// Pings & Pongs are fine |
|
182 |
webSocket.sendPing(ByteBuffer.allocate(125)).join(); |
|
183 |
webSocket.sendPong(ByteBuffer.allocate(125)).join(); |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
184 |
server.close(); |
49765 | 185 |
} |
186 |
||
187 |
@Test |
|
188 |
public void sendMethodsThrowIOE1() throws IOException { |
|
189 |
server = new DummyWebSocketServer(); |
|
190 |
server.open(); |
|
49944 | 191 |
webSocket = newBuilder().proxy(NO_PROXY).build() |
49765 | 192 |
.newWebSocketBuilder() |
193 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
|
194 |
.join(); |
|
195 |
||
196 |
webSocket.sendClose(NORMAL_CLOSURE, "ok").join(); |
|
197 |
||
198 |
assertFails(IOE, webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
|
199 |
||
200 |
assertFails(IOE, webSocket.sendText("", true)); |
|
201 |
assertFails(IOE, webSocket.sendText("", false)); |
|
202 |
assertFails(IOE, webSocket.sendText("abc", true)); |
|
203 |
assertFails(IOE, webSocket.sendText("abc", false)); |
|
204 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(0), true)); |
|
205 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(0), false)); |
|
206 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(1), true)); |
|
207 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(1), false)); |
|
208 |
||
209 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(125))); |
|
210 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(124))); |
|
211 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(1))); |
|
212 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(0))); |
|
213 |
||
214 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(125))); |
|
215 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(124))); |
|
216 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(1))); |
|
217 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(0))); |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
218 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
219 |
server.close(); |
49765 | 220 |
} |
221 |
||
50681 | 222 |
@DataProvider(name = "sequence") |
223 |
public Object[][] data1() { |
|
224 |
int[] CLOSE = { |
|
225 |
0x81, 0x00, // "" |
|
226 |
0x82, 0x00, // [] |
|
227 |
0x89, 0x00, // <PING> |
|
228 |
0x8a, 0x00, // <PONG> |
|
229 |
0x88, 0x00, // <CLOSE> |
|
230 |
}; |
|
231 |
int[] ERROR = { |
|
232 |
0x81, 0x00, // "" |
|
233 |
0x82, 0x00, // [] |
|
234 |
0x89, 0x00, // <PING> |
|
235 |
0x8a, 0x00, // <PONG> |
|
236 |
0x8b, 0x00, // 0xB control frame (causes an error) |
|
237 |
}; |
|
238 |
return new Object[][]{ |
|
239 |
{CLOSE, 1}, |
|
240 |
{CLOSE, 3}, |
|
241 |
{CLOSE, 4}, |
|
242 |
{CLOSE, Long.MAX_VALUE}, |
|
243 |
{ERROR, 1}, |
|
244 |
{ERROR, 3}, |
|
245 |
{ERROR, 4}, |
|
246 |
{ERROR, Long.MAX_VALUE}, |
|
247 |
}; |
|
248 |
} |
|
249 |
||
250 |
@Test(dataProvider = "sequence") |
|
251 |
public void listenerSequentialOrder(int[] binary, long requestSize) |
|
252 |
throws IOException |
|
253 |
{ |
|
254 |
||
255 |
server = Support.serverWithCannedData(binary); |
|
256 |
server.open(); |
|
257 |
||
258 |
CompletableFuture<Void> violation = new CompletableFuture<>(); |
|
259 |
||
260 |
MockListener listener = new MockListener(requestSize) { |
|
261 |
||
262 |
final AtomicBoolean guard = new AtomicBoolean(); |
|
263 |
||
264 |
private <T> T checkRunExclusively(Supplier<T> action) { |
|
265 |
if (guard.getAndSet(true)) { |
|
266 |
violation.completeExceptionally(new RuntimeException()); |
|
267 |
} |
|
268 |
try { |
|
269 |
return action.get(); |
|
270 |
} finally { |
|
271 |
if (!guard.getAndSet(false)) { |
|
272 |
violation.completeExceptionally(new RuntimeException()); |
|
273 |
} |
|
274 |
} |
|
275 |
} |
|
276 |
||
277 |
@Override |
|
278 |
public void onOpen(WebSocket webSocket) { |
|
279 |
checkRunExclusively(() -> { |
|
280 |
super.onOpen(webSocket); |
|
281 |
return null; |
|
282 |
}); |
|
283 |
} |
|
284 |
||
285 |
@Override |
|
286 |
public CompletionStage<?> onText(WebSocket webSocket, |
|
287 |
CharSequence data, |
|
288 |
boolean last) { |
|
289 |
return checkRunExclusively( |
|
290 |
() -> super.onText(webSocket, data, last)); |
|
291 |
} |
|
292 |
||
293 |
@Override |
|
294 |
public CompletionStage<?> onBinary(WebSocket webSocket, |
|
295 |
ByteBuffer data, |
|
296 |
boolean last) { |
|
297 |
return checkRunExclusively( |
|
298 |
() -> super.onBinary(webSocket, data, last)); |
|
299 |
} |
|
300 |
||
301 |
@Override |
|
302 |
public CompletionStage<?> onPing(WebSocket webSocket, |
|
303 |
ByteBuffer message) { |
|
304 |
return checkRunExclusively( |
|
305 |
() -> super.onPing(webSocket, message)); |
|
306 |
} |
|
307 |
||
308 |
@Override |
|
309 |
public CompletionStage<?> onPong(WebSocket webSocket, |
|
310 |
ByteBuffer message) { |
|
311 |
return checkRunExclusively( |
|
312 |
() -> super.onPong(webSocket, message)); |
|
313 |
} |
|
314 |
||
315 |
@Override |
|
316 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
317 |
int statusCode, |
|
318 |
String reason) { |
|
319 |
return checkRunExclusively( |
|
320 |
() -> super.onClose(webSocket, statusCode, reason)); |
|
321 |
} |
|
322 |
||
323 |
@Override |
|
324 |
public void onError(WebSocket webSocket, Throwable error) { |
|
325 |
checkRunExclusively(() -> { |
|
326 |
super.onError(webSocket, error); |
|
327 |
return null; |
|
328 |
}); |
|
329 |
} |
|
330 |
}; |
|
331 |
||
332 |
webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder() |
|
333 |
.buildAsync(server.getURI(), listener) |
|
334 |
.join(); |
|
335 |
||
336 |
||
337 |
listener.invocations(); |
|
338 |
violation.complete(null); // won't affect if completed exceptionally |
|
339 |
violation.join(); |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
340 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
341 |
server.close(); |
50681 | 342 |
} |
343 |
||
49765 | 344 |
@Test |
345 |
public void sendMethodsThrowIOE2() throws Exception { |
|
346 |
server = Support.serverWithCannedData(0x88, 0x00); |
|
347 |
server.open(); |
|
348 |
CompletableFuture<Void> onCloseCalled = new CompletableFuture<>(); |
|
349 |
CompletableFuture<Void> canClose = new CompletableFuture<>(); |
|
350 |
||
351 |
WebSocket.Listener listener = new WebSocket.Listener() { |
|
352 |
@Override |
|
353 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
354 |
int statusCode, |
|
355 |
String reason) { |
|
356 |
System.out.printf("onClose(%s, '%s')%n", statusCode, reason); |
|
357 |
onCloseCalled.complete(null); |
|
358 |
return canClose; |
|
359 |
} |
|
360 |
||
361 |
@Override |
|
362 |
public void onError(WebSocket webSocket, Throwable error) { |
|
363 |
System.out.println("onError(" + error + ")"); |
|
364 |
onCloseCalled.completeExceptionally(error); |
|
365 |
} |
|
366 |
}; |
|
367 |
||
49944 | 368 |
webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder() |
49765 | 369 |
.buildAsync(server.getURI(), listener) |
370 |
.join(); |
|
371 |
||
372 |
onCloseCalled.join(); // Wait for onClose to be called |
|
373 |
canClose.complete(null); // Signal to the WebSocket it can close the output |
|
374 |
TimeUnit.SECONDS.sleep(5); // Give canClose some time to reach the WebSocket |
|
375 |
||
376 |
assertFails(IOE, webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok")); |
|
377 |
||
378 |
assertFails(IOE, webSocket.sendText("", true)); |
|
379 |
assertFails(IOE, webSocket.sendText("", false)); |
|
380 |
assertFails(IOE, webSocket.sendText("abc", true)); |
|
381 |
assertFails(IOE, webSocket.sendText("abc", false)); |
|
382 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(0), true)); |
|
383 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(0), false)); |
|
384 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(1), true)); |
|
385 |
assertFails(IOE, webSocket.sendBinary(ByteBuffer.allocate(1), false)); |
|
386 |
||
387 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(125))); |
|
388 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(124))); |
|
389 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(1))); |
|
390 |
assertFails(IOE, webSocket.sendPing(ByteBuffer.allocate(0))); |
|
391 |
||
392 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(125))); |
|
393 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(124))); |
|
394 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(1))); |
|
395 |
assertFails(IOE, webSocket.sendPong(ByteBuffer.allocate(0))); |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
396 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
397 |
server.close(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
398 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
399 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
400 |
// Used to verify a server requiring Authentication |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
401 |
private static final String USERNAME = "chegar"; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
402 |
private static final String PASSWORD = "a1b2c3"; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
403 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
404 |
static class WSAuthenticator extends Authenticator { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
405 |
@Override |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
406 |
protected PasswordAuthentication getPasswordAuthentication() { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
407 |
return new PasswordAuthentication(USERNAME, PASSWORD.toCharArray()); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
408 |
} |
49765 | 409 |
} |
410 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
411 |
static final Function<int[],DummyWebSocketServer> SERVER_WITH_CANNED_DATA = |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
412 |
new Function<>() { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
413 |
@Override public DummyWebSocketServer apply(int[] data) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
414 |
return Support.serverWithCannedData(data); } |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
415 |
@Override public String toString() { return "SERVER_WITH_CANNED_DATA"; } |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
416 |
}; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
417 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
418 |
static final Function<int[],DummyWebSocketServer> AUTH_SERVER_WITH_CANNED_DATA = |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
419 |
new Function<>() { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
420 |
@Override public DummyWebSocketServer apply(int[] data) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
421 |
return Support.serverWithCannedDataAndAuthentication(USERNAME, PASSWORD, data); } |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
422 |
@Override public String toString() { return "AUTH_SERVER_WITH_CANNED_DATA"; } |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
423 |
}; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
424 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
425 |
@DataProvider(name = "servers") |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
426 |
public Object[][] servers() { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
427 |
return new Object[][] { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
428 |
{ SERVER_WITH_CANNED_DATA }, |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
429 |
{ AUTH_SERVER_WITH_CANNED_DATA }, |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
430 |
}; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
431 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
432 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
433 |
@Test(dataProvider = "servers") |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
434 |
public void simpleAggregatingBinaryMessages |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
435 |
(Function<int[],DummyWebSocketServer> serverSupplier) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
436 |
throws IOException |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
437 |
{ |
49765 | 438 |
List<byte[]> expected = List.of("alpha", "beta", "gamma", "delta") |
439 |
.stream() |
|
440 |
.map(s -> s.getBytes(StandardCharsets.US_ASCII)) |
|
441 |
.collect(Collectors.toList()); |
|
442 |
int[] binary = new int[]{ |
|
443 |
0x82, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // [alpha] |
|
444 |
0x02, 0x02, 0x62, 0x65, // [be |
|
445 |
0x80, 0x02, 0x74, 0x61, // ta] |
|
446 |
0x02, 0x01, 0x67, // [g |
|
447 |
0x00, 0x01, 0x61, // a |
|
448 |
0x00, 0x00, // |
|
449 |
0x00, 0x00, // |
|
450 |
0x00, 0x01, 0x6d, // m |
|
451 |
0x00, 0x01, 0x6d, // m |
|
452 |
0x80, 0x01, 0x61, // a] |
|
453 |
0x8a, 0x00, // <PONG> |
|
454 |
0x02, 0x04, 0x64, 0x65, 0x6c, 0x74, // [delt |
|
455 |
0x00, 0x01, 0x61, // a |
|
456 |
0x80, 0x00, // ] |
|
457 |
0x88, 0x00 // <CLOSE> |
|
458 |
}; |
|
459 |
CompletableFuture<List<byte[]>> actual = new CompletableFuture<>(); |
|
460 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
461 |
server = serverSupplier.apply(binary); |
49765 | 462 |
server.open(); |
463 |
||
464 |
WebSocket.Listener listener = new WebSocket.Listener() { |
|
465 |
||
466 |
List<byte[]> collectedBytes = new ArrayList<>(); |
|
467 |
ByteBuffer buffer = ByteBuffer.allocate(1024); |
|
468 |
||
469 |
@Override |
|
470 |
public CompletionStage<?> onBinary(WebSocket webSocket, |
|
471 |
ByteBuffer message, |
|
472 |
boolean last) { |
|
473 |
System.out.printf("onBinary(%s, %s)%n", message, last); |
|
474 |
webSocket.request(1); |
|
475 |
||
476 |
append(message); |
|
477 |
if (last) { |
|
478 |
buffer.flip(); |
|
479 |
byte[] bytes = new byte[buffer.remaining()]; |
|
480 |
buffer.get(bytes); |
|
481 |
buffer.clear(); |
|
482 |
processWholeBinary(bytes); |
|
483 |
} |
|
484 |
return null; |
|
485 |
} |
|
486 |
||
487 |
private void append(ByteBuffer message) { |
|
488 |
if (buffer.remaining() < message.remaining()) { |
|
489 |
assert message.remaining() > 0; |
|
490 |
int cap = (buffer.capacity() + message.remaining()) * 2; |
|
491 |
ByteBuffer b = ByteBuffer.allocate(cap); |
|
492 |
b.put(buffer.flip()); |
|
493 |
buffer = b; |
|
494 |
} |
|
495 |
buffer.put(message); |
|
496 |
} |
|
497 |
||
498 |
private void processWholeBinary(byte[] bytes) { |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
499 |
String stringBytes = new String(bytes, UTF_8); |
49765 | 500 |
System.out.println("processWholeBinary: " + stringBytes); |
501 |
collectedBytes.add(bytes); |
|
502 |
} |
|
503 |
||
504 |
@Override |
|
505 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
506 |
int statusCode, |
|
507 |
String reason) { |
|
508 |
actual.complete(collectedBytes); |
|
509 |
return null; |
|
510 |
} |
|
511 |
||
512 |
@Override |
|
513 |
public void onError(WebSocket webSocket, Throwable error) { |
|
514 |
actual.completeExceptionally(error); |
|
515 |
} |
|
516 |
}; |
|
517 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
518 |
webSocket = newBuilder() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
519 |
.proxy(NO_PROXY) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
520 |
.authenticator(new WSAuthenticator()) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
521 |
.build().newWebSocketBuilder() |
49765 | 522 |
.buildAsync(server.getURI(), listener) |
523 |
.join(); |
|
524 |
||
525 |
List<byte[]> a = actual.join(); |
|
526 |
assertEquals(a, expected); |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
527 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
528 |
server.close(); |
49765 | 529 |
} |
530 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
531 |
@Test(dataProvider = "servers") |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
532 |
public void simpleAggregatingTextMessages |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
533 |
(Function<int[],DummyWebSocketServer> serverSupplier) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
534 |
throws IOException |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
535 |
{ |
49765 | 536 |
List<String> expected = List.of("alpha", "beta", "gamma", "delta"); |
537 |
||
538 |
int[] binary = new int[]{ |
|
539 |
0x81, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // "alpha" |
|
540 |
0x01, 0x02, 0x62, 0x65, // "be |
|
541 |
0x80, 0x02, 0x74, 0x61, // ta" |
|
542 |
0x01, 0x01, 0x67, // "g |
|
543 |
0x00, 0x01, 0x61, // a |
|
544 |
0x00, 0x00, // |
|
545 |
0x00, 0x00, // |
|
546 |
0x00, 0x01, 0x6d, // m |
|
547 |
0x00, 0x01, 0x6d, // m |
|
548 |
0x80, 0x01, 0x61, // a" |
|
549 |
0x8a, 0x00, // <PONG> |
|
550 |
0x01, 0x04, 0x64, 0x65, 0x6c, 0x74, // "delt |
|
551 |
0x00, 0x01, 0x61, // a |
|
552 |
0x80, 0x00, // " |
|
553 |
0x88, 0x00 // <CLOSE> |
|
554 |
}; |
|
555 |
CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
|
556 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
557 |
server = serverSupplier.apply(binary); |
49765 | 558 |
server.open(); |
559 |
||
560 |
WebSocket.Listener listener = new WebSocket.Listener() { |
|
561 |
||
562 |
List<String> collectedStrings = new ArrayList<>(); |
|
563 |
StringBuilder text = new StringBuilder(); |
|
564 |
||
565 |
@Override |
|
566 |
public CompletionStage<?> onText(WebSocket webSocket, |
|
567 |
CharSequence message, |
|
568 |
boolean last) { |
|
569 |
System.out.printf("onText(%s, %s)%n", message, last); |
|
570 |
webSocket.request(1); |
|
571 |
text.append(message); |
|
572 |
if (last) { |
|
573 |
String str = text.toString(); |
|
574 |
text.setLength(0); |
|
575 |
processWholeText(str); |
|
576 |
} |
|
577 |
return null; |
|
578 |
} |
|
579 |
||
580 |
private void processWholeText(String string) { |
|
581 |
System.out.println(string); |
|
582 |
collectedStrings.add(string); |
|
583 |
} |
|
584 |
||
585 |
@Override |
|
586 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
587 |
int statusCode, |
|
588 |
String reason) { |
|
589 |
actual.complete(collectedStrings); |
|
590 |
return null; |
|
591 |
} |
|
592 |
||
593 |
@Override |
|
594 |
public void onError(WebSocket webSocket, Throwable error) { |
|
595 |
actual.completeExceptionally(error); |
|
596 |
} |
|
597 |
}; |
|
598 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
599 |
webSocket = newBuilder() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
600 |
.proxy(NO_PROXY) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
601 |
.authenticator(new WSAuthenticator()) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
602 |
.build().newWebSocketBuilder() |
49765 | 603 |
.buildAsync(server.getURI(), listener) |
604 |
.join(); |
|
605 |
||
606 |
List<String> a = actual.join(); |
|
607 |
assertEquals(a, expected); |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
608 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
609 |
server.close(); |
49765 | 610 |
} |
611 |
||
612 |
/* |
|
613 |
* Exercises the scenario where requests for more messages are made prior to |
|
614 |
* completing the returned CompletionStage instances. |
|
615 |
*/ |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
616 |
@Test(dataProvider = "servers") |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
617 |
public void aggregatingTextMessages |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
618 |
(Function<int[],DummyWebSocketServer> serverSupplier) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
619 |
throws IOException |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
620 |
{ |
49765 | 621 |
List<String> expected = List.of("alpha", "beta", "gamma", "delta"); |
622 |
||
623 |
int[] binary = new int[]{ |
|
624 |
0x81, 0x05, 0x61, 0x6c, 0x70, 0x68, 0x61, // "alpha" |
|
625 |
0x01, 0x02, 0x62, 0x65, // "be |
|
626 |
0x80, 0x02, 0x74, 0x61, // ta" |
|
627 |
0x01, 0x01, 0x67, // "g |
|
628 |
0x00, 0x01, 0x61, // a |
|
629 |
0x00, 0x00, // |
|
630 |
0x00, 0x00, // |
|
631 |
0x00, 0x01, 0x6d, // m |
|
632 |
0x00, 0x01, 0x6d, // m |
|
633 |
0x80, 0x01, 0x61, // a" |
|
634 |
0x8a, 0x00, // <PONG> |
|
635 |
0x01, 0x04, 0x64, 0x65, 0x6c, 0x74, // "delt |
|
636 |
0x00, 0x01, 0x61, // a |
|
637 |
0x80, 0x00, // " |
|
638 |
0x88, 0x00 // <CLOSE> |
|
639 |
}; |
|
640 |
CompletableFuture<List<String>> actual = new CompletableFuture<>(); |
|
641 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
642 |
server = serverSupplier.apply(binary); |
49765 | 643 |
server.open(); |
644 |
||
645 |
WebSocket.Listener listener = new WebSocket.Listener() { |
|
646 |
||
647 |
List<CharSequence> parts = new ArrayList<>(); |
|
648 |
/* |
|
649 |
* A CompletableFuture which will complete once the current |
|
650 |
* message has been fully assembled. Until then the listener |
|
651 |
* returns this instance for every call. |
|
652 |
*/ |
|
653 |
CompletableFuture<?> currentCf = new CompletableFuture<>(); |
|
654 |
List<String> collected = new ArrayList<>(); |
|
655 |
||
656 |
@Override |
|
657 |
public CompletionStage<?> onText(WebSocket webSocket, |
|
658 |
CharSequence message, |
|
659 |
boolean last) { |
|
660 |
parts.add(message); |
|
661 |
if (!last) { |
|
662 |
webSocket.request(1); |
|
663 |
} else { |
|
664 |
this.currentCf.thenRun(() -> webSocket.request(1)); |
|
665 |
CompletableFuture<?> refCf = this.currentCf; |
|
666 |
processWholeMessage(new ArrayList<>(parts), refCf); |
|
667 |
currentCf = new CompletableFuture<>(); |
|
668 |
parts.clear(); |
|
669 |
return refCf; |
|
670 |
} |
|
671 |
return currentCf; |
|
672 |
} |
|
673 |
||
674 |
@Override |
|
675 |
public CompletionStage<?> onClose(WebSocket webSocket, |
|
676 |
int statusCode, |
|
677 |
String reason) { |
|
678 |
actual.complete(collected); |
|
679 |
return null; |
|
680 |
} |
|
681 |
||
682 |
@Override |
|
683 |
public void onError(WebSocket webSocket, Throwable error) { |
|
684 |
actual.completeExceptionally(error); |
|
685 |
} |
|
686 |
||
687 |
public void processWholeMessage(List<CharSequence> data, |
|
688 |
CompletableFuture<?> cf) { |
|
689 |
StringBuilder b = new StringBuilder(); |
|
690 |
data.forEach(b::append); |
|
691 |
String s = b.toString(); |
|
692 |
System.out.println(s); |
|
693 |
cf.complete(null); |
|
694 |
collected.add(s); |
|
695 |
} |
|
696 |
}; |
|
697 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
698 |
webSocket = newBuilder() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
699 |
.proxy(NO_PROXY) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
700 |
.authenticator(new WSAuthenticator()) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
701 |
.build().newWebSocketBuilder() |
49765 | 702 |
.buildAsync(server.getURI(), listener) |
703 |
.join(); |
|
704 |
||
705 |
List<String> a = actual.join(); |
|
706 |
assertEquals(a, expected); |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
707 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
708 |
server.close(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
709 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
710 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
711 |
// -- authentication specific tests |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
712 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
713 |
/* |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
714 |
* Ensures authentication succeeds when an Authenticator set on client builder. |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
715 |
*/ |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
716 |
@Test |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
717 |
public void clientAuthenticate() throws IOException { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
718 |
try (var server = new DummyWebSocketServer(USERNAME, PASSWORD)){ |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
719 |
server.open(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
720 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
721 |
var webSocket = newBuilder() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
722 |
.proxy(NO_PROXY) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
723 |
.authenticator(new WSAuthenticator()) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
724 |
.build() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
725 |
.newWebSocketBuilder() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
726 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
727 |
.join(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
728 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
729 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
730 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
731 |
/* |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
732 |
* Ensures authentication succeeds when an `Authorization` header is explicitly set. |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
733 |
*/ |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
734 |
@Test |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
735 |
public void explicitAuthenticate() throws IOException { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
736 |
try (var server = new DummyWebSocketServer(USERNAME, PASSWORD)) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
737 |
server.open(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
738 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
739 |
String hv = "Basic " + Base64.getEncoder().encodeToString( |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
740 |
(USERNAME + ":" + PASSWORD).getBytes(UTF_8)); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
741 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
742 |
var webSocket = newBuilder() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
743 |
.proxy(NO_PROXY).build() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
744 |
.newWebSocketBuilder() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
745 |
.header("Authorization", hv) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
746 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
747 |
.join(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
748 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
749 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
750 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
751 |
/* |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
752 |
* Ensures authentication does not succeed when no authenticator is present. |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
753 |
*/ |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
754 |
@Test |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
755 |
public void failNoAuthenticator() throws IOException { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
756 |
try (var server = new DummyWebSocketServer(USERNAME, PASSWORD)) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
757 |
server.open(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
758 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
759 |
CompletableFuture<WebSocket> cf = newBuilder() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
760 |
.proxy(NO_PROXY).build() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
761 |
.newWebSocketBuilder() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
762 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
763 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
764 |
try { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
765 |
var webSocket = cf.join(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
766 |
fail("Expected exception not thrown"); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
767 |
} catch (CompletionException expected) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
768 |
WebSocketHandshakeException e = (WebSocketHandshakeException)expected.getCause(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
769 |
HttpResponse<?> response = e.getResponse(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
770 |
assertEquals(response.statusCode(), 401); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
771 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
772 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
773 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
774 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
775 |
/* |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
776 |
* Ensures authentication does not succeed when the authenticator presents |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
777 |
* unauthorized credentials. |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
778 |
*/ |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
779 |
@Test |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
780 |
public void failBadCredentials() throws IOException { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
781 |
try (var server = new DummyWebSocketServer(USERNAME, PASSWORD)) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
782 |
server.open(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
783 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
784 |
Authenticator authenticator = new Authenticator() { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
785 |
@Override protected PasswordAuthentication getPasswordAuthentication() { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
786 |
return new PasswordAuthentication("BAD"+USERNAME, "".toCharArray()); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
787 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
788 |
}; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
789 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
790 |
CompletableFuture<WebSocket> cf = newBuilder() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
791 |
.proxy(NO_PROXY) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
792 |
.authenticator(authenticator) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
793 |
.build() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
794 |
.newWebSocketBuilder() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
795 |
.buildAsync(server.getURI(), new WebSocket.Listener() { }); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
796 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
797 |
try { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
798 |
var webSocket = cf.join(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
799 |
fail("Expected exception not thrown"); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
800 |
} catch (CompletionException expected) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
801 |
System.out.println("caught expected exception:" + expected); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
802 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
803 |
} |
49765 | 804 |
} |
805 |
} |