Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion UnleashClient/connectors/polling_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from UnleashClient.api import get_feature_toggles
from UnleashClient.cache import BaseCache
from UnleashClient.constants import ETAG, FEATURES_URL
from UnleashClient.constants import APPLICATION_HEADERS, ETAG, FEATURES_URL
from UnleashClient.events import UnleashEventType, UnleashFetchedEvent
from UnleashClient.utils import LOGGER

Expand Down Expand Up @@ -58,6 +58,7 @@ def _fetch_and_load(self):
app_name=self.app_name,
instance_id=self.instance_id,
headers={
**APPLICATION_HEADERS,
**self.headers,
"unleash-interval": str(self.refresh_interval * 1000),
},
Expand Down
40 changes: 39 additions & 1 deletion tests/unit_tests/connectors/test_polling_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
URL,
)
from UnleashClient.connectors import PollingConnector
from UnleashClient.constants import ETAG, FEATURES_URL
from UnleashClient.constants import (
CLIENT_SPEC_VERSION,
ETAG,
FEATURES_URL,
)

FULL_FEATURE_URL = URL + FEATURES_URL

Expand Down Expand Up @@ -56,6 +60,40 @@ def test_polling_connector_fetch_and_load(cache_empty):
assert temp_cache.get(ETAG) == ETAG_VALUE


@responses.activate
def test_polling_connector_sends_spec_version_header(cache_empty):
"""Test that the polling connector sends the spec version header automatically"""
engine = UnleashEngine()
scheduler = BackgroundScheduler()
responses.add(
responses.GET,
FULL_FEATURE_URL,
json=MOCK_FEATURE_RESPONSE,
status=200,
headers={"etag": ETAG_VALUE},
)
temp_cache = cache_empty

connector = PollingConnector(
engine=engine,
cache=temp_cache,
scheduler=scheduler,
url=URL,
app_name=APP_NAME,
instance_id=INSTANCE_ID,
custom_options=CUSTOM_OPTIONS,
request_timeout=REQUEST_TIMEOUT,
request_retries=REQUEST_RETRIES,
)

connector._fetch_and_load()

assert len(responses.calls) == 1
request = responses.calls[0].request
assert "Unleash-Client-Spec" in request.headers
assert request.headers["Unleash-Client-Spec"] == CLIENT_SPEC_VERSION


@responses.activate
def test_polling_connector_fetch_and_load_project(cache_empty):
engine = UnleashEngine()
Expand Down
36 changes: 35 additions & 1 deletion tests/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@
)
from UnleashClient import INSTANCES, UnleashClient
from UnleashClient.cache import FileCache
from UnleashClient.constants import FEATURES_URL, METRICS_URL, REGISTER_URL
from UnleashClient.constants import (
CLIENT_SPEC_VERSION,
FEATURES_URL,
METRICS_URL,
REGISTER_URL,
)
from UnleashClient.events import BaseEvent, UnleashEvent, UnleashEventType
from UnleashClient.utils import InstanceAllowType

Expand Down Expand Up @@ -1470,6 +1475,35 @@ def test_identification_values_are_passed_in():
assert False, "Invalid UUID format in UNLEASH-CONNECTION-ID"


@responses.activate
def test_uc_polling_connector_sends_spec_version_header(readyable_unleash_client):
"""Test that the client sends spec version header when using polling connector"""
unleash_client, ready_signal, _ = readyable_unleash_client

responses.add(responses.POST, URL + REGISTER_URL, json={}, status=202)
responses.add(
responses.GET, URL + FEATURES_URL, json=MOCK_FEATURE_RESPONSE, status=200
)
responses.add(responses.POST, URL + METRICS_URL, json={}, status=202)

unleash_client.initialize_client()
ready_signal.wait(timeout=1)

features_requests = [
call
for call in responses.calls
if call.request.method == "GET" and FEATURES_URL in call.request.url
]

assert len(features_requests) >= 1, "No features request found"
features_request = features_requests[0].request

assert (
"Unleash-Client-Spec" in features_request.headers
), "Spec version header missing"
assert features_request.headers["Unleash-Client-Spec"] == CLIENT_SPEC_VERSION


def test_uc_bootstrap_initializes_offline_connector():
"""Test that UnleashClient initializes OfflineConnector when bootstrapped."""
cache = FileCache("MOCK_CACHE")
Expand Down
Loading