@@ -291,4 +291,49 @@ public void proxy_returns_400_for_erroneous_FormMultipartPost_requests() throws
291291 assertEquals ("Returned Status matched" , HttpProxyService .ERR_MSG_400_COMMON , responseEntity .getBody ());
292292 }
293293 }
294+
295+ @ ParameterizedTest
296+ @ ValueSource (strings = {"http" , "https" })
297+ @ DisplayName ("Should use system proxy settings when forwarding request" )
298+ public void proxy_uses_system_proxy_settings (String scheme ) throws Exception {
299+ // Set system proxy properties
300+ final String proxyHost = "1.2.2.4" ;
301+ System .setProperty ("%s.proxyHost" .formatted (scheme ), proxyHost );
302+ System .setProperty ("%s.proxyPort" .formatted (scheme ), "8888" );
303+
304+ HttpServletRequest mockedRequest = mock (HttpServletRequest .class );
305+ final String baseUrl = "%s://my-test-example.org/resource" .formatted (scheme );
306+ final URI baseUri = new URI (baseUrl );
307+ HttpResponse mockedResponse = mock (HttpResponse .class );
308+
309+ final HttpHeaders headers = new HttpHeaders ();
310+ headers .add (HttpHeaders .CONTENT_TYPE , "text/plain" );
311+ HttpStatus status = HttpStatus .OK ;
312+
313+ when (mockedResponse .getHeaders ()).thenReturn (headers );
314+ when (mockedResponse .getBody ()).thenReturn ("proxied" .getBytes ());
315+ when (mockedResponse .getStatusCode ()).thenReturn (status );
316+
317+ try (
318+ MockedStatic <HttpUtil > httpUtilMock = mockStatic (HttpUtil .class )
319+ ) {
320+ httpUtilMock .when (() -> HttpUtil .isHttpGetRequest (mockedRequest )).thenReturn (true );
321+ httpUtilMock .when (() -> HttpUtil .forwardGet (baseUri , mockedRequest , false )).thenAnswer (invocation -> {
322+ // Assert system proxy properties are set
323+ assertEquals ("Proxy host does not match" , proxyHost , System .getProperty ("%s.proxyHost" .formatted (scheme )));
324+ assertEquals ("Proxy port does not match" , "8888" , System .getProperty ("%s.proxyPort" .formatted (scheme )));
325+ return mockedResponse ;
326+ });
327+
328+ final ResponseEntity <?> responseEntity = httpProxyService .doProxy (mockedRequest , baseUrl , null );
329+ assertEquals ("HttpStatus does not match" , HttpStatus .OK , responseEntity .getStatusCode ());
330+ assertNotNull ("Response body is null." , responseEntity .getBody ());
331+ assertEquals ("Proxies request body does not match" , "proxied" , new String ((byte []) responseEntity .getBody ()));
332+ } finally {
333+ // Cleanup system properties
334+ System .clearProperty ("%s.proxyHost" .formatted (scheme ));
335+ System .clearProperty ("%s.proxyPort" .formatted (scheme ));
336+ }
337+ }
338+
294339}
0 commit comments