|
4 | 4 | import pytest |
5 | 5 | import random |
6 | 6 | import string |
| 7 | +import sys |
7 | 8 | import time |
8 | 9 | import unittest |
9 | 10 |
|
@@ -455,3 +456,115 @@ def retry_cb(args, data, excp): |
455 | 456 | self.assertEqual(lp, ErrorResult["rd"].decode('utf-8')) |
456 | 457 | self.assertIsNotNone(ErrorResult["rx"]) |
457 | 458 | self.assertIsInstance(ErrorResult["rx"], Url3TimeoutError) |
| 459 | + |
| 460 | + def test_disable_grpc_compression(self): |
| 461 | + """ |
| 462 | + Test that disable_grpc_compression parameter controls query response compression. |
| 463 | +
|
| 464 | + On Python 3.10+: Uses mitmproxy to intercept and verify request+response headers. |
| 465 | + On Python <3.10: Only verifies data integrity (mitmproxy not available). |
| 466 | + """ |
| 467 | + # Test cases |
| 468 | + test_cases = [ |
| 469 | + { |
| 470 | + 'name': 'default', |
| 471 | + 'disable_grpc_compression': None, |
| 472 | + 'expected_req_encoding': 'identity, deflate, gzip', |
| 473 | + 'expected_resp_encoding': 'gzip', |
| 474 | + }, |
| 475 | + { |
| 476 | + 'name': 'disabled=False', |
| 477 | + 'disable_grpc_compression': False, |
| 478 | + 'expected_req_encoding': 'identity, deflate, gzip', |
| 479 | + 'expected_resp_encoding': 'gzip', |
| 480 | + }, |
| 481 | + { |
| 482 | + 'name': 'disabled=True', |
| 483 | + 'disable_grpc_compression': True, |
| 484 | + 'expected_req_encoding': 'identity', |
| 485 | + 'expected_resp_encoding': None, |
| 486 | + }, |
| 487 | + ] |
| 488 | + |
| 489 | + # Check if mitmproxy is available (Python 3.10+ only) |
| 490 | + mitmproxy_available = sys.version_info >= (3, 10) |
| 491 | + proxy = None |
| 492 | + proxy_url = None |
| 493 | + |
| 494 | + if mitmproxy_available: |
| 495 | + try: |
| 496 | + from tests.util.mitmproxy import MitmproxyServer |
| 497 | + proxy = MitmproxyServer() |
| 498 | + proxy.__enter__() |
| 499 | + proxy_url = proxy.url |
| 500 | + except ImportError as e: |
| 501 | + logging.error(f"mitmproxy not available: {e}") |
| 502 | + mitmproxy_available = False |
| 503 | + |
| 504 | + try: |
| 505 | + test_id = time.time_ns() |
| 506 | + measurement = f'grpc_compression_test_{random_hex(6)}' |
| 507 | + |
| 508 | + # Write test data points |
| 509 | + num_points = 10 |
| 510 | + lines = [ |
| 511 | + f'{measurement},type=test value={i}.0,counter={i}i,test_id={test_id}i {test_id + i * 1000000}' |
| 512 | + for i in range(num_points) |
| 513 | + ] |
| 514 | + self.client.write('\n'.join(lines)) |
| 515 | + |
| 516 | + test_query = f"SELECT * FROM \"{measurement}\" WHERE test_id = {test_id} ORDER BY counter" |
| 517 | + |
| 518 | + # Wait for data to be available |
| 519 | + result = None |
| 520 | + start = time.time() |
| 521 | + while time.time() - start < 10: |
| 522 | + result = self.client.query(test_query, mode="all") |
| 523 | + if len(result) >= num_points: |
| 524 | + break |
| 525 | + time.sleep(0.5) |
| 526 | + self.assertEqual(len(result), num_points, "Data not available after write") |
| 527 | + |
| 528 | + for tc in test_cases: |
| 529 | + name = tc['name'] |
| 530 | + |
| 531 | + # Build client kwargs |
| 532 | + client_kwargs = { |
| 533 | + 'host': self.host, |
| 534 | + 'database': self.database, |
| 535 | + 'token': self.token, |
| 536 | + 'proxy': proxy_url, |
| 537 | + 'verify_ssl': False if proxy_url else True, |
| 538 | + } |
| 539 | + if tc['disable_grpc_compression'] is not None: |
| 540 | + client_kwargs['disable_grpc_compression'] = tc['disable_grpc_compression'] |
| 541 | + |
| 542 | + client = InfluxDBClient3(**client_kwargs) |
| 543 | + try: |
| 544 | + result = client.query(test_query, mode="all") |
| 545 | + self.assertEqual(len(result), num_points, f"[{name}] Should return {num_points} rows") |
| 546 | + finally: |
| 547 | + client.close() |
| 548 | + |
| 549 | + # Verify headers (Python 3.10+ only) |
| 550 | + if mitmproxy_available and proxy: |
| 551 | + req_encoding = proxy.capture.get_last_request_header('grpc-accept-encoding') |
| 552 | + resp_encoding = proxy.capture.get_last_response_header('grpc-encoding') |
| 553 | + |
| 554 | + print(f"\n[{name}] Request grpc-accept-encoding: {req_encoding}") |
| 555 | + print(f"[{name}] Response grpc-encoding: {resp_encoding}") |
| 556 | + |
| 557 | + self.assertEqual(req_encoding, tc['expected_req_encoding'], |
| 558 | + f"[{name}] Unexpected request encoding") |
| 559 | + |
| 560 | + if tc['expected_resp_encoding']: |
| 561 | + self.assertEqual(resp_encoding, tc['expected_resp_encoding'], |
| 562 | + f"[{name}] Unexpected response encoding") |
| 563 | + else: |
| 564 | + self.assertTrue(resp_encoding is None or resp_encoding == 'identity', |
| 565 | + f"[{name}] Expected no compression, got: {resp_encoding}") |
| 566 | + |
| 567 | + proxy.capture.clear() |
| 568 | + finally: |
| 569 | + if proxy: |
| 570 | + proxy.__exit__(None, None, None) |
0 commit comments