diff --git a/src/requests/cookies.py b/src/requests/cookies.py index f69d0cda9e..4953465bb9 100644 --- a/src/requests/cookies.py +++ b/src/requests/cookies.py @@ -118,7 +118,7 @@ def info(self): return self._headers def getheaders(self, name): - self._headers.getheaders(name) + return self._headers.getheaders(name) def extract_cookies_to_jar(jar, request, response): diff --git a/tests/test_requests.py b/tests/test_requests.py index 75d2deff2e..8eefccb04d 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -1406,6 +1406,50 @@ class MyCookiePolicy(cookielib.DefaultCookiePolicy): jar.set_policy(MyCookiePolicy()) assert isinstance(jar.copy().get_policy(), MyCookiePolicy) + def test_mock_response_getheaders_returns_headers_list(self): + """Test that MockResponse.getheaders() returns the list of headers.""" + from http.client import HTTPMessage + from requests.cookies import MockResponse + + # Create a mock HTTPMessage with getheaders method + mock_headers = mock.Mock(spec=HTTPMessage) + mock_headers.getheaders = mock.Mock( + return_value=["cookie1=value1", "cookie2=value2"] + ) + + # Create MockResponse + mock_response = MockResponse(mock_headers) + + # Test that getheaders returns a result (not None) + result = mock_response.getheaders("Set-Cookie") + assert result is not None, "getheaders() should return a value, not None" + assert isinstance(result, list), "getheaders() should return a list" + assert result == ["cookie1=value1", "cookie2=value2"] + + # Verify that the underlying getheaders method was called with correct argument + mock_headers.getheaders.assert_called_once_with("Set-Cookie") + + def test_mock_response_getheaders_returns_empty_list(self): + """Test that MockResponse.getheaders() returns an empty list when no headers match.""" + from http.client import HTTPMessage + from requests.cookies import MockResponse + + # Create a mock HTTPMessage with getheaders method returning empty list + mock_headers = mock.Mock(spec=HTTPMessage) + mock_headers.getheaders = mock.Mock(return_value=[]) + + # Create MockResponse + mock_response = MockResponse(mock_headers) + + # Test that getheaders returns an empty list (not None) + result = mock_response.getheaders("NonExistent-Header") + assert result is not None, "getheaders() should return a list, not None" + assert isinstance(result, list), "getheaders() should return a list" + assert len(result) == 0, "getheaders() should return an empty list" + + # Verify that the underlying getheaders method was called with correct argument + mock_headers.getheaders.assert_called_once_with("NonExistent-Header") + def test_time_elapsed_blank(self, httpbin): r = requests.get(httpbin("get")) td = r.elapsed