|
10 | 10 | from ldclient.version import VERSION
|
11 | 11 | from ldclient.versioned_data_kind import FEATURES, SEGMENTS
|
12 | 12 | from testing.http_util import start_server, BasicResponse, CauseNetworkError, SequentialHandler
|
| 13 | +from testing.proxy_test_util import do_proxy_tests |
13 | 14 | from testing.stub_util import make_delete_event, make_patch_event, make_put_event, stream_content
|
14 | 15 |
|
15 | 16 | brief_delay = 0.001
|
@@ -210,54 +211,24 @@ def test_unrecoverable_http_error(status):
|
210 | 211 | assert not sp.initialized()
|
211 | 212 | server.should_have_requests(1)
|
212 | 213 |
|
213 |
| -def test_can_use_http_proxy_via_environment_var(monkeypatch): |
214 |
| - with start_server() as server: |
215 |
| - config = Config(sdk_key = 'sdk-key', stream_uri = 'http://not-real') |
216 |
| - monkeypatch.setenv('http_proxy', server.uri) |
217 |
| - _verify_http_proxy_is_used(server, config) |
218 |
| - |
219 |
| -def test_can_use_https_proxy_via_environment_var(monkeypatch): |
220 |
| - with start_server() as server: |
221 |
| - config = Config(sdk_key = 'sdk-key', stream_uri = 'https://not-real') |
222 |
| - monkeypatch.setenv('https_proxy', server.uri) |
223 |
| - _verify_https_proxy_is_used(server, config) |
224 |
| - |
225 |
| -def test_can_use_http_proxy_via_config(): |
226 |
| - with start_server() as server: |
227 |
| - config = Config(sdk_key = 'sdk-key', stream_uri = 'http://not-real', http_proxy=server.uri) |
228 |
| - _verify_http_proxy_is_used(server, config) |
229 |
| - |
230 |
| -def test_can_use_https_proxy_via_config(): |
231 |
| - with start_server() as server: |
232 |
| - config = Config(sdk_key = 'sdk-key', stream_uri = 'https://not-real', http_proxy=server.uri) |
233 |
| - _verify_https_proxy_is_used(server, config) |
234 |
| - |
235 |
| -def _verify_http_proxy_is_used(server, config): |
236 |
| - store = InMemoryFeatureStore() |
237 |
| - ready = Event() |
238 |
| - with stream_content(make_put_event()) as stream: |
239 |
| - server.for_path(config.stream_base_uri + '/all', stream) |
240 |
| - with StreamingUpdateProcessor(config, store, ready, None) as sp: |
241 |
| - sp.start() |
242 |
| - # For an insecure proxy request, our stub server behaves enough like the real thing to satisfy the |
243 |
| - # HTTP client, so we should be able to see the request go through. Note that the URI path will |
244 |
| - # actually be an absolute URI for a proxy request. |
245 |
| - req = server.await_request() |
246 |
| - assert req.method == 'GET' |
247 |
| - ready.wait(start_wait) |
248 |
| - assert sp.initialized() |
249 |
| - |
250 |
| -def _verify_https_proxy_is_used(server, config): |
251 |
| - store = InMemoryFeatureStore() |
252 |
| - ready = Event() |
253 |
| - with stream_content(make_put_event()) as stream: |
254 |
| - server.for_path(config.stream_base_uri + '/all', stream) |
255 |
| - with StreamingUpdateProcessor(config, store, ready, None) as sp: |
256 |
| - sp.start() |
257 |
| - # Our simple stub server implementation can't really do HTTPS proxying, so the request will fail, but |
258 |
| - # it can still record that it *got* the request, which proves that the request went to the proxy. |
259 |
| - req = server.await_request() |
260 |
| - assert req.method == 'CONNECT' |
| 214 | +def test_http_proxy(monkeypatch): |
| 215 | + def _stream_processor_proxy_test(server, config, secure): |
| 216 | + store = InMemoryFeatureStore() |
| 217 | + ready = Event() |
| 218 | + with stream_content(make_put_event()) as stream: |
| 219 | + server.for_path(config.stream_base_uri + '/all', stream) |
| 220 | + with StreamingUpdateProcessor(config, store, ready, None) as sp: |
| 221 | + sp.start() |
| 222 | + # Wait till the server has received a request. We need to do this even though do_proxy_tests also |
| 223 | + # does it, because if we return too soon out of this block, the object returned by stream_content |
| 224 | + # could be closed and the test server would no longer work. |
| 225 | + server.wait_until_request_received() |
| 226 | + if not secure: |
| 227 | + # We only do this part with HTTP, because with HTTPS we don't have a real enough proxy server |
| 228 | + # for the stream connection to work correctly - we can only detect the request. |
| 229 | + ready.wait(start_wait) |
| 230 | + assert sp.initialized() |
| 231 | + do_proxy_tests(_stream_processor_proxy_test, 'GET', monkeypatch) |
261 | 232 |
|
262 | 233 | def test_records_diagnostic_on_stream_init_success():
|
263 | 234 | store = InMemoryFeatureStore()
|
|
0 commit comments