|
11 | 11 | from pytest_mock import MockerFixture |
12 | 12 | from responses import matchers |
13 | 13 |
|
14 | | -from flagsmith import Flagsmith |
| 14 | +from flagsmith import Flagsmith, __version__ |
15 | 15 | from flagsmith.exceptions import ( |
16 | 16 | FlagsmithAPIError, |
17 | 17 | FlagsmithFeatureDoesNotExistError, |
@@ -717,3 +717,96 @@ def test_custom_feature_error_raised_when_invalid_feature( |
717 | 717 | with pytest.raises(FlagsmithFeatureDoesNotExistError): |
718 | 718 | # When |
719 | 719 | flags.is_feature_enabled("non-existing-feature") |
| 720 | + |
| 721 | + |
| 722 | +@pytest.fixture |
| 723 | +def default_headers() -> typing.Dict[str, str]: |
| 724 | + return { |
| 725 | + "User-Agent": f"flagsmith-python-client/{__version__} python-requests/2.32.4", |
| 726 | + "Accept-Encoding": "gzip, deflate", |
| 727 | + "Accept": "*/*", |
| 728 | + "Connection": "keep-alive", |
| 729 | + } |
| 730 | + |
| 731 | + |
| 732 | +@pytest.mark.parametrize( |
| 733 | + "kwargs,expected_headers", |
| 734 | + [ |
| 735 | + ( |
| 736 | + { |
| 737 | + "environment_key": "test-key", |
| 738 | + "application_metadata": {"name": "test-app", "version": "1.0.0"}, |
| 739 | + }, |
| 740 | + { |
| 741 | + "Flagsmith-Application-Name": "test-app", |
| 742 | + "Flagsmith-Application-Version": "1.0.0", |
| 743 | + "X-Environment-Key": "test-key", |
| 744 | + }, |
| 745 | + ), |
| 746 | + ( |
| 747 | + { |
| 748 | + "environment_key": "test-key", |
| 749 | + "application_metadata": {"name": "test-app"}, |
| 750 | + }, |
| 751 | + { |
| 752 | + "Flagsmith-Application-Name": "test-app", |
| 753 | + "X-Environment-Key": "test-key", |
| 754 | + }, |
| 755 | + ), |
| 756 | + ( |
| 757 | + { |
| 758 | + "environment_key": "test-key", |
| 759 | + "application_metadata": {"version": "1.0.0"}, |
| 760 | + }, |
| 761 | + { |
| 762 | + "Flagsmith-Application-Version": "1.0.0", |
| 763 | + "X-Environment-Key": "test-key", |
| 764 | + }, |
| 765 | + ), |
| 766 | + ( |
| 767 | + { |
| 768 | + "environment_key": "test-key", |
| 769 | + "application_metadata": {"version": "1.0.0"}, |
| 770 | + "custom_headers": {"X-Custom-Header": "CustomValue"}, |
| 771 | + }, |
| 772 | + { |
| 773 | + "Flagsmith-Application-Version": "1.0.0", |
| 774 | + "X-Environment-Key": "test-key", |
| 775 | + "X-Custom-Header": "CustomValue", |
| 776 | + }, |
| 777 | + ), |
| 778 | + ( |
| 779 | + { |
| 780 | + "environment_key": "test-key", |
| 781 | + "application_metadata": None, |
| 782 | + "custom_headers": {"X-Custom-Header": "CustomValue"}, |
| 783 | + }, |
| 784 | + { |
| 785 | + "X-Environment-Key": "test-key", |
| 786 | + "X-Custom-Header": "CustomValue", |
| 787 | + }, |
| 788 | + ), |
| 789 | + ( |
| 790 | + {"environment_key": "test-key"}, |
| 791 | + { |
| 792 | + "X-Environment-Key": "test-key", |
| 793 | + }, |
| 794 | + ), |
| 795 | + ], |
| 796 | +) |
| 797 | +@responses.activate() |
| 798 | +def test_flagsmith__init__expected_headers_sent( |
| 799 | + default_headers: typing.Dict[str, str], |
| 800 | + kwargs: typing.Dict[str, typing.Any], |
| 801 | + expected_headers: typing.Dict[str, str], |
| 802 | +) -> None: |
| 803 | + # Given |
| 804 | + flagsmith = Flagsmith(**kwargs) |
| 805 | + responses.add(method="GET", url=flagsmith.environment_flags_url, body="{}") |
| 806 | + |
| 807 | + # When |
| 808 | + flagsmith.get_environment_flags() |
| 809 | + |
| 810 | + # Then |
| 811 | + headers = responses.calls[0].request.headers |
| 812 | + assert headers == {**default_headers, **expected_headers} |
0 commit comments