1515# Set environment variables to change this configuration.
1616# Example: export DEEPL_SERVER_URL=http://localhost:3000/
1717# export DEEPL_MOCK_SERVER_PORT=3000
18+ # export DEEPL_PROXY_URL=http://localhost:3001/
19+ # export DEEPL_MOCK_PROXY_SERVER_PORT=3001
1820#
1921# supported use cases:
2022# - using real API
@@ -28,6 +30,8 @@ class Config(BaseSettings):
2830 auth_key : str = None
2931 server_url : str = None
3032 mock_server_port : int = None
33+ proxy_url : str = None
34+ mock_proxy_server_port : int = None
3135
3236 class Config :
3337 env_prefix = "DEEPL_"
@@ -49,9 +53,11 @@ def __init__(self):
4953 uu = str (uuid .uuid1 ())
5054 session_uuid = f"{ os .getenv ('PYTEST_CURRENT_TEST' )} /{ uu } "
5155 self .headers ["mock-server-session" ] = session_uuid
56+ self .proxy = config .proxy_url
5257 else :
5358 self .auth_key = config .auth_key
5459 self .server_url = config .server_url
60+ self .proxy = config .proxy_url
5561
5662 def no_response (self , count ):
5763 """Instructs the mock server to ignore N requests from this
@@ -113,15 +119,24 @@ def set_doc_translate_time(self, milliseconds):
113119 milliseconds
114120 )
115121
122+ def expect_proxy (self , value : bool = True ):
123+ """Instructs the mock server to only accept requests via the proxy."""
124+ if config .mock_server_port is not None :
125+ self .headers ["mock-server-session-expect-proxy" ] = (
126+ "1" if value else "0"
127+ )
128+
116129 return Server ()
117130
118131
119- def _make_translator (server , auth_key = None ):
132+ def _make_translator (server , auth_key = None , proxy = None ):
120133 """Returns a deepl.Translator for the specified server test fixture.
121134 The server auth_key is used unless specifically overridden."""
122135 if auth_key is None :
123136 auth_key = server .auth_key
124- translator = deepl .Translator (auth_key , server_url = server .server_url )
137+ translator = deepl .Translator (
138+ auth_key , server_url = server .server_url , proxy = proxy
139+ )
125140
126141 # If the server test fixture has custom headers defined, update the
127142 # translator headers and replace with the server headers dictionary.
@@ -146,6 +161,15 @@ def translator_with_random_auth_key(server):
146161 return _make_translator (server , auth_key = str (uuid .uuid1 ()))
147162
148163
164+ @pytest .fixture
165+ def translator_with_random_auth_key_and_proxy (server ):
166+ """Returns a deepl.Translator with randomized authentication key,
167+ for use in mock-server tests."""
168+ return _make_translator (
169+ server , auth_key = str (uuid .uuid1 ()), proxy = server .proxy
170+ )
171+
172+
149173@pytest .fixture
150174def cleanup_matching_glossaries (translator ):
151175 """
@@ -302,6 +326,13 @@ def output_document_path(tmpdir):
302326 Config ().mock_server_port is None ,
303327 reason = "this test requires a mock server" ,
304328)
329+ # Decorate test functions with "@needs_mock_proxy_server" to skip them if a real
330+ # server is used or mock proxy server is not configured
331+ needs_mock_proxy_server = pytest .mark .skipif (
332+ Config ().mock_proxy_server_port is None
333+ or Config ().mock_server_port is None ,
334+ reason = "this test requires a mock proxy server" ,
335+ )
305336# Decorate test functions with "@needs_real_server" to skip them if a mock
306337# server is used
307338needs_real_server = pytest .mark .skipif (
0 commit comments