1111import os
1212import re
1313import pytest
14+ from http .server import BaseHTTPRequestHandler , HTTPServer
15+ from threading import Thread
1416
1517
1618# Use the same port across tests
1719port = get_free_port ()
1820mock_server = None
1921mock_server_thread = None
2022
23+ # Simple HTTP proxy server for testing proxy functionality
24+ class SimpleProxyHandler (BaseHTTPRequestHandler ):
25+ """A simple HTTP proxy handler that logs requests"""
26+
27+ def do_GET (self ):
28+ self .send_response (200 )
29+ self .send_header ('Content-Type' , 'text/plain' )
30+ self .end_headers ()
31+ self .wfile .write (b'Request proxied successfully' )
32+ print (f"Proxy handled request: { self .path } " )
33+
34+ def do_PUT (self ):
35+ self .send_response (200 )
36+ self .send_header ('Content-Type' , 'text/plain' )
37+ self .end_headers ()
38+ self .wfile .write (b'Request proxied successfully' )
39+ print (f"Proxy handled PUT request: { self .path } " )
40+
41+ def log_message (self , format , * args ):
42+ # Customize logging to show it's from the proxy
43+ print (f"PROXY LOG: { format % args } " )
44+
45+ # Global variables for proxy server
46+ proxy_port = get_free_port ()
47+ proxy_server = None
48+ proxy_server_thread = None
49+
2150
2251def setup_module (module ):
2352 """setup any state specific to the execution of the given module."""
2453 global mock_server
2554 global mock_server_thread
55+ global proxy_server
56+ global proxy_server_thread
57+
58+ # Start the mock registry server
2659 mock_server , mock_server_thread = start_mock_server (port )
60+
61+ # Start the proxy server
62+ proxy_server = HTTPServer (('localhost' , proxy_port ), SimpleProxyHandler )
63+ proxy_server_thread = Thread (target = proxy_server .serve_forever )
64+ proxy_server_thread .setDaemon (True )
65+ proxy_server_thread .start ()
66+ print (f"Proxy server started on port { proxy_port } " )
2767
2868
2969def teardown_module (module ):
3070 """teardown any state that was previously setup with a setup_module
3171 method.
3272 """
3373 mock_server .server_close ()
74+ proxy_server .server_close ()
3475
3576
3677def test_distribution_mock_server (tmp_path ):
@@ -46,6 +87,22 @@ def test_distribution_mock_server(tmp_path):
4687 WithUserAgent ("reggie-tests" ),
4788 )
4889 assert not client .Config .Debug
90+
91+ print ("Testing creation of client with proxy" )
92+ proxy_url = f"http://localhost:{ proxy_port } "
93+ proxy_client = NewClient (
94+ mock_url ,
95+ WithUsernamePassword ("testuser" , "testpass" ),
96+ WithDefaultName ("testname" ),
97+ WithUserAgent ("reggie-tests" ),
98+ WithProxy (proxy_url ),
99+ )
100+ assert proxy_client .Config .Proxy == proxy_url
101+
102+ # Make a request with the proxy client
103+ req = proxy_client .NewRequest ("GET" , "/v2/<n>/tags/list" )
104+ response = proxy_client .Do (req )
105+ assert response .status_code == 200 , f"Expected status code 200, got { response .status_code } "
49106
50107 print ("Testing setting debug option" )
51108 clientDebug = NewClient (mock_url , WithDebug (True ))
@@ -188,6 +245,21 @@ def test_distribution_mock_server(tmp_path):
188245
189246 print ("Check that the body did not get lost somewhere" )
190247 assert req .body == "abc"
248+
249+ print ("Test proxy request with different configuration" )
250+ # Create a client with a different proxy configuration
251+ alt_proxy_url = f"http://localhost:{ proxy_port } /alternate"
252+ alt_proxy_client = NewClient (
253+ mock_url ,
254+ WithProxy (alt_proxy_url ),
255+ )
256+ assert alt_proxy_client .Config .Proxy == alt_proxy_url
257+
258+ # Verify that proxy setting is correctly passed to the request
259+ proxy_req = alt_proxy_client .NewRequest ("GET" , "/v2/test/tags/list" )
260+ assert proxy_req .proxies , "Request should have non-empty proxies dictionary when proxy is set"
261+ assert proxy_req .proxies .get ("http" ) == alt_proxy_url , "HTTP proxy not correctly set"
262+ assert proxy_req .proxies .get ("https" ) == alt_proxy_url , "HTTPS proxy not correctly set"
191263
192264 print ("Test that the retry callback is invoked, if configured." )
193265 newBody = "not the original body"
@@ -214,3 +286,12 @@ def errorFunc(r):
214286 )
215287 except Exception as exc :
216288 assert "ruhroh" in str (exc )
289+
290+ print ("Test proxy setting in request client" )
291+ # Directly test the SetProxy method on the request client
292+ req = client .NewRequest ("GET" , "/test/endpoint" )
293+ proxy_addr = f"http://localhost:{ proxy_port } /direct-test"
294+ req .SetProxy (proxy_addr )
295+ # Verify that the proxy is set in the underlying request object when it's executed
296+ response = client .Do (req )
297+ assert response .status_code == 200 , "Request through proxy should succeed"
0 commit comments