|
| 1 | +import os |
| 2 | +import urllib.parse |
| 3 | +import pytest |
| 4 | +from databricks.sdk.config import Config |
| 5 | +from databricks.sdk._base_client import _BaseClient |
| 6 | +from databricks.sdk.core import ApiClient |
| 7 | + |
| 8 | +def test_config_proxy_attributes(): |
| 9 | + cfg = Config( |
| 10 | + host="https://test.databricks.com", |
| 11 | + token="test-token", |
| 12 | + proxy_url="http://proxy.example.com:8080", |
| 13 | + proxy_username="user123", |
| 14 | + proxy_password="secretpassword", |
| 15 | + proxy_auth_type="BASIC", |
| 16 | + ) |
| 17 | + assert cfg.proxy_url == "http://proxy.example.com:8080" |
| 18 | + assert cfg.proxy_username == "user123" |
| 19 | + assert cfg.proxy_password == "secretpassword" |
| 20 | + assert cfg.proxy_auth_type == "BASIC" |
| 21 | + assert "proxy_password=***" in cfg.debug_string() |
| 22 | + |
| 23 | +def test_config_proxy_from_env(monkeypatch): |
| 24 | + monkeypatch.setenv("DATABRICKS_PROXY_URL", "http://proxy.env.com:8080") |
| 25 | + monkeypatch.setenv("DATABRICKS_PROXY_USERNAME", "envuser") |
| 26 | + monkeypatch.setenv("DATABRICKS_PROXY_PASSWORD", "envpass") |
| 27 | + monkeypatch.setenv("DATABRICKS_PROXY_AUTH_TYPE", "BASIC") |
| 28 | + |
| 29 | + cfg = Config(host="https://test.databricks.com", token="test-token") |
| 30 | + assert cfg.proxy_url == "http://proxy.env.com:8080" |
| 31 | + assert cfg.proxy_username == "envuser" |
| 32 | + assert cfg.proxy_password == "envpass" |
| 33 | + assert cfg.proxy_auth_type == "BASIC" |
| 34 | + |
| 35 | +def test_base_client_proxy_url_construction(): |
| 36 | + # Test proxy with username and password containing special characters that need encoding |
| 37 | + client = _BaseClient( |
| 38 | + proxy_url="http://proxy.example.com:8080", |
| 39 | + proxy_username="user@domain", |
| 40 | + proxy_password="pass:word", |
| 41 | + ) |
| 42 | + # The constructed proxy URL should have encoded credentials |
| 43 | + expected_url = "http://user%40domain:pass%3Aword@proxy.example.com:8080" |
| 44 | + assert client._session.proxies == { |
| 45 | + "http": expected_url, |
| 46 | + "https": expected_url, |
| 47 | + } |
| 48 | + |
| 49 | +def test_base_client_proxy_no_credentials(): |
| 50 | + client = _BaseClient(proxy_url="proxy.example.com:8080") |
| 51 | + # Scheme should be prepended |
| 52 | + expected_url = "http://proxy.example.com:8080" |
| 53 | + assert client._session.proxies == { |
| 54 | + "http": expected_url, |
| 55 | + "https": expected_url, |
| 56 | + } |
| 57 | + |
| 58 | +def test_base_client_proxy_from_env(monkeypatch): |
| 59 | + monkeypatch.setenv("DATABRICKS_PROXY_URL", "http://envproxy.com:8080") |
| 60 | + monkeypatch.setenv("DATABRICKS_PROXY_USERNAME", "envuser") |
| 61 | + monkeypatch.setenv("DATABRICKS_PROXY_PASSWORD", "envpass") |
| 62 | + |
| 63 | + client = _BaseClient() |
| 64 | + expected_url = "http://envuser:envpass@envproxy.com:8080" |
| 65 | + assert client._session.proxies == { |
| 66 | + "http": expected_url, |
| 67 | + "https": expected_url, |
| 68 | + } |
0 commit comments