66import org .junit .jupiter .api .Disabled ;
77import org .junit .jupiter .api .Test ;
88
9+ import java .io .BufferedReader ;
10+ import java .io .InputStreamReader ;
11+ import java .io .OutputStream ;
12+ import java .net .ServerSocket ;
13+ import java .net .Socket ;
14+ import java .nio .charset .StandardCharsets ;
15+ import java .util .concurrent .CountDownLatch ;
16+ import java .util .concurrent .TimeUnit ;
17+ import java .util .concurrent .atomic .AtomicReference ;
18+
919import static com .azure .security .keyvault .jca .implementation .utils .HttpUtil .DEFAULT_USER_AGENT_VALUE_PREFIX ;
1020import static com .azure .security .keyvault .jca .implementation .utils .HttpUtil .VERSION ;
1121import static org .junit .jupiter .api .Assertions .*;
@@ -18,6 +28,39 @@ public void getUserAgentPrefixTest() {
1828 assertEquals (DEFAULT_USER_AGENT_VALUE_PREFIX + VERSION , HttpUtil .USER_AGENT_VALUE );
1929 }
2030
31+ @ Test
32+ public void getUsesJvmProxySystemProperties () throws Exception {
33+ String previousProxyHost = System .getProperty ("http.proxyHost" );
34+ String previousProxyPort = System .getProperty ("http.proxyPort" );
35+ String previousNonProxyHosts = System .getProperty ("http.nonProxyHosts" );
36+
37+ try (ServerSocket proxyServer = new ServerSocket (0 )) {
38+ CountDownLatch requestReceived = new CountDownLatch (1 );
39+ AtomicReference <String > requestLine = new AtomicReference <>();
40+ AtomicReference <Exception > proxyFailure = new AtomicReference <>();
41+
42+ Thread proxyThread
43+ = new Thread (() -> handleProxyRequest (proxyServer , requestLine , requestReceived , proxyFailure ));
44+ proxyThread .setDaemon (true );
45+ proxyThread .start ();
46+
47+ System .setProperty ("http.proxyHost" , "localhost" );
48+ System .setProperty ("http.proxyPort" , String .valueOf (proxyServer .getLocalPort ()));
49+ System .clearProperty ("http.nonProxyHosts" );
50+
51+ String response = HttpUtil .get ("http://azure-keyvault-jca-proxy-test.invalid/path" , null );
52+
53+ assertTrue (requestReceived .await (5 , TimeUnit .SECONDS ), "Expected proxy server to receive the request." );
54+ assertNull (proxyFailure .get (), "Proxy server failed while handling the request." );
55+ assertEquals ("proxied" , response );
56+ assertEquals ("GET http://azure-keyvault-jca-proxy-test.invalid/path HTTP/1.1" , requestLine .get ());
57+ } finally {
58+ restoreProperty ("http.proxyHost" , previousProxyHost );
59+ restoreProperty ("http.proxyPort" , previousProxyPort );
60+ restoreProperty ("http.nonProxyHosts" , previousNonProxyHosts );
61+ }
62+ }
63+
2164 @ Test
2265 @ Disabled ("Disable this because it will cause pipeline failure: https://dev.azure.com/azure-sdk/internal/_build/results?buildId=1196171&view=logs&j=4a83f3be-c53d-53dd-7954-86872056fb11&t=54174aae-5a55-579d-08e2-94fb446f7b77&l=29" )
2366 public void testHttpUtilGet () {
@@ -35,4 +78,37 @@ public void testHttpUtilGet1() {
3578 assertNotNull (result );
3679 assertFalse (result .isEmpty ());
3780 }
81+
82+ private static void handleProxyRequest (ServerSocket proxyServer , AtomicReference <String > requestLine ,
83+ CountDownLatch requestReceived , AtomicReference <Exception > proxyFailure ) {
84+ try (Socket socket = proxyServer .accept ();
85+ BufferedReader reader = new BufferedReader (
86+ new InputStreamReader (socket .getInputStream (), StandardCharsets .UTF_8 ));
87+ OutputStream outputStream = socket .getOutputStream ()) {
88+
89+ requestLine .set (reader .readLine ());
90+ String line ;
91+ while ((line = reader .readLine ()) != null && !line .isEmpty ()) {
92+ // Consume request headers before writing the response.
93+ }
94+
95+ byte [] body = "proxied" .getBytes (StandardCharsets .UTF_8 );
96+ outputStream .write (("HTTP/1.1 200 OK\r \n Content-Length: " + body .length + "\r \n \r \n " )
97+ .getBytes (StandardCharsets .UTF_8 ));
98+ outputStream .write (body );
99+ outputStream .flush ();
100+ requestReceived .countDown ();
101+ } catch (Exception e ) {
102+ proxyFailure .set (e );
103+ requestReceived .countDown ();
104+ }
105+ }
106+
107+ private static void restoreProperty (String name , String value ) {
108+ if (value == null ) {
109+ System .clearProperty (name );
110+ } else {
111+ System .setProperty (name , value );
112+ }
113+ }
38114}
0 commit comments