Skip to content

Commit

Permalink
[ONCALL #6327] have headers match during HTTP cache hit (#45926)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxi297 authored Sep 27, 2024
1 parent c4f00ab commit 53af0c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def _request_session(self) -> requests.Session:
sqlite_path = str(Path(cache_dir) / self.cache_filename)
else:
sqlite_path = "file::memory:?cache=shared"
return CachedLimiterSession(sqlite_path, backend="sqlite", api_budget=self._api_budget) # type: ignore # there are no typeshed stubs for requests_cache
return CachedLimiterSession(sqlite_path, backend="sqlite", api_budget=self._api_budget, match_headers=True) # type: ignore # there are no typeshed stubs for requests_cache
else:
return LimiterSession(api_budget=self._api_budget)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,16 @@ def test_backoff_strategy_endless(exit_on_rate_limit, expected_call_count, expec
http_method="get", url="https://test_base_url.com/v1/endpoint", request_kwargs={}, exit_on_rate_limit=exit_on_rate_limit
)
assert mocked_send.call_count == expected_call_count


def test_given_different_headers_then_response_is_not_cached(requests_mock):
http_client = HttpClient(name="test", logger=MagicMock(), use_cache=True)
first_request_headers = {"header_key": "first"}
second_request_headers = {"header_key": "second"}
requests_mock.register_uri("GET", "https://google.com/", request_headers=first_request_headers, json={"test": "first response"})
requests_mock.register_uri("GET", "https://google.com/", request_headers=second_request_headers, json={"test": "second response"})

http_client.send_request("GET", "https://google.com/", headers=first_request_headers, request_kwargs={})
_, second_response = http_client.send_request("GET", "https://google.com/", headers=second_request_headers, request_kwargs={})

assert second_response.json()["test"] == "second response"

0 comments on commit 53af0c2

Please sign in to comment.