|
1 | 1 | import pytest |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +from click.testing import CliRunner |
2 | 5 |
|
3 | 6 | from clickhouse_cli.cli import run_cli |
| 7 | +from clickhouse_cli.clickhouse.client import Client |
4 | 8 |
|
5 | 9 |
|
6 | 10 | def test_main_help(): |
7 | 11 | # Call with the --help option as a basic sanity check. |
8 | 12 | with pytest.raises(SystemExit) as exinfo: |
9 | 13 | run_cli(["--help", ]) |
10 | 14 | assert exinfo.value.code == 0 |
| 15 | + |
| 16 | + |
| 17 | +def test_custom_headers_sent_with_request(): |
| 18 | + """tests -H --header arguments on CLI""" |
| 19 | + runner = CliRunner() |
| 20 | + |
| 21 | + mock_response = MagicMock() |
| 22 | + mock_response.status_code = 200 |
| 23 | + mock_response.elapsed.total_seconds.return_value = 0.1 |
| 24 | + mock_response.text = "" |
| 25 | + |
| 26 | + captured = {} |
| 27 | + |
| 28 | + def fake_request(method, url, **kwargs): |
| 29 | + captured["headers"] = kwargs.get("headers", {}) |
| 30 | + return mock_response |
| 31 | + |
| 32 | + with patch("requests.Session.request", side_effect=fake_request): |
| 33 | + runner.invoke(run_cli, [ |
| 34 | + "-h", "localhost", |
| 35 | + "-p", "8123", |
| 36 | + "-H", "X-My-Header: test-value", |
| 37 | + "-H", "X-Another: hello", |
| 38 | + "-q", "SELECT 1", |
| 39 | + ]) |
| 40 | + |
| 41 | + assert captured["headers"].get("X-My-Header") == "test-value" |
| 42 | + assert captured["headers"].get("X-Another") == "hello" |
| 43 | + |
| 44 | + |
| 45 | +def test_custom_headers_stored_on_client(): |
| 46 | + """tests headers suppliet for client object""" |
| 47 | + client = Client( |
| 48 | + url="http://localhost:8123/", |
| 49 | + user="default", |
| 50 | + password="", |
| 51 | + database="default", |
| 52 | + cookie=None, |
| 53 | + headers={"X-Custom": "value"}, |
| 54 | + ) |
| 55 | + assert client.headers == {"X-Custom": "value"} |
| 56 | + |
| 57 | + mock_response = MagicMock() |
| 58 | + mock_response.status_code = 200 |
| 59 | + mock_response.elapsed.total_seconds.return_value = 0.0 |
| 60 | + mock_response.text = "" |
| 61 | + |
| 62 | + captured = {} |
| 63 | + |
| 64 | + def fake_request(method, url, **kwargs): |
| 65 | + captured["headers"] = kwargs.get("headers", {}) |
| 66 | + return mock_response |
| 67 | + |
| 68 | + with patch("requests.Session.request", side_effect=fake_request): |
| 69 | + client._query("GET", "SELECT 1", {}, fmt="Null", stream=False) |
| 70 | + |
| 71 | + assert captured["headers"].get("X-Custom") == "value" |
| 72 | + |
| 73 | + |
| 74 | +def test_custom_headers_override_defaults(): |
| 75 | + """user supplied headers override defaults""" |
| 76 | + client = Client( |
| 77 | + url="http://localhost:8123/", |
| 78 | + user="default", |
| 79 | + password="", |
| 80 | + database="default", |
| 81 | + cookie=None, |
| 82 | + headers={"User-Agent": "my-custom-agent"}, |
| 83 | + ) |
| 84 | + |
| 85 | + mock_response = MagicMock() |
| 86 | + mock_response.status_code = 200 |
| 87 | + mock_response.elapsed.total_seconds.return_value = 0.0 |
| 88 | + mock_response.text = "" |
| 89 | + |
| 90 | + captured = {} |
| 91 | + |
| 92 | + def fake_request(method, url, **kwargs): |
| 93 | + captured["headers"] = kwargs.get("headers", {}) |
| 94 | + return mock_response |
| 95 | + |
| 96 | + with patch("requests.Session.request", side_effect=fake_request): |
| 97 | + client._query("GET", "SELECT 1", {}, fmt="Null", stream=False) |
| 98 | + |
| 99 | + assert captured["headers"]["User-Agent"] == "my-custom-agent" |
0 commit comments