35 import java.io.UncheckedIOException;
36 import java.net.Authenticator;
37 import java.net.InetAddress;
38 import java.net.InetSocketAddress;
39 import java.net.PasswordAuthentication;
40 import java.net.ProxySelector;
41 import java.net.http.HttpResponse;
42 import java.net.http.WebSocket;
43 import java.net.http.WebSocketHandshakeException;
44 import java.nio.ByteBuffer;
45 import java.nio.charset.StandardCharsets;
46 import java.util.ArrayList;
47 import java.util.Base64;
48 import java.util.List;
49 import java.util.concurrent.CompletableFuture;
50 import java.util.concurrent.CompletionException;
51 import java.util.concurrent.CompletionStage;
52 import java.util.function.Function;
53 import java.util.function.Supplier;
54 import java.util.stream.Collectors;
55 import org.testng.annotations.DataProvider;
56 import org.testng.annotations.Test;
57 import static java.net.http.HttpClient.newBuilder;
58 import static java.nio.charset.StandardCharsets.UTF_8;
59 import static org.testng.Assert.assertEquals;
60 import static org.testng.FileAssert.fail;
61
62 public class WebSocketProxyTest {
63
64 // Used to verify a proxy/websocket server requiring Authentication
65 private static final String USERNAME = "wally";
66 private static final String PASSWORD = "xyz987";
67
68 static class WSAuthenticator extends Authenticator {
69 @Override
70 protected PasswordAuthentication getPasswordAuthentication() {
71 return new PasswordAuthentication(USERNAME, PASSWORD.toCharArray());
72 }
73 }
74
289 @Override protected PasswordAuthentication getPasswordAuthentication() {
290 return new PasswordAuthentication("BAD"+USERNAME, "".toCharArray());
291 }
292 };
293
294 CompletableFuture<WebSocket> cf = newBuilder()
295 .proxy(ProxySelector.of(proxyAddress))
296 .authenticator(authenticator)
297 .build()
298 .newWebSocketBuilder()
299 .buildAsync(server.getURI(), new WebSocket.Listener() { });
300
301 try {
302 var webSocket = cf.join();
303 fail("Expected exception not thrown");
304 } catch (CompletionException expected) {
305 System.out.println("caught expected exception:" + expected);
306 }
307 }
308 }
309 }
|
35 import java.io.UncheckedIOException;
36 import java.net.Authenticator;
37 import java.net.InetAddress;
38 import java.net.InetSocketAddress;
39 import java.net.PasswordAuthentication;
40 import java.net.ProxySelector;
41 import java.net.http.HttpResponse;
42 import java.net.http.WebSocket;
43 import java.net.http.WebSocketHandshakeException;
44 import java.nio.ByteBuffer;
45 import java.nio.charset.StandardCharsets;
46 import java.util.ArrayList;
47 import java.util.Base64;
48 import java.util.List;
49 import java.util.concurrent.CompletableFuture;
50 import java.util.concurrent.CompletionException;
51 import java.util.concurrent.CompletionStage;
52 import java.util.function.Function;
53 import java.util.function.Supplier;
54 import java.util.stream.Collectors;
55 import org.testng.annotations.BeforeMethod;
56 import org.testng.annotations.DataProvider;
57 import org.testng.annotations.Test;
58 import static java.net.http.HttpClient.newBuilder;
59 import static java.nio.charset.StandardCharsets.UTF_8;
60 import static org.testng.Assert.assertEquals;
61 import static org.testng.FileAssert.fail;
62
63 public class WebSocketProxyTest {
64
65 // Used to verify a proxy/websocket server requiring Authentication
66 private static final String USERNAME = "wally";
67 private static final String PASSWORD = "xyz987";
68
69 static class WSAuthenticator extends Authenticator {
70 @Override
71 protected PasswordAuthentication getPasswordAuthentication() {
72 return new PasswordAuthentication(USERNAME, PASSWORD.toCharArray());
73 }
74 }
75
290 @Override protected PasswordAuthentication getPasswordAuthentication() {
291 return new PasswordAuthentication("BAD"+USERNAME, "".toCharArray());
292 }
293 };
294
295 CompletableFuture<WebSocket> cf = newBuilder()
296 .proxy(ProxySelector.of(proxyAddress))
297 .authenticator(authenticator)
298 .build()
299 .newWebSocketBuilder()
300 .buildAsync(server.getURI(), new WebSocket.Listener() { });
301
302 try {
303 var webSocket = cf.join();
304 fail("Expected exception not thrown");
305 } catch (CompletionException expected) {
306 System.out.println("caught expected exception:" + expected);
307 }
308 }
309 }
310
311 @BeforeMethod
312 public void breakBetweenTests() {
313 System.out.println("\n-------\n");
314 }
315 }
|