Skip to content

Improve test cases to test header-less response #822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
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
7 changes: 6 additions & 1 deletion tests/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ class MinimalResponse(object): # Not for production use
def __init__(self, requests_resp=None, status_code=None, text=None, headers=None):
self.status_code = status_code or requests_resp.status_code
self.text = text if text is not None else requests_resp.text
self.headers = {} if headers is None else headers
if headers:
# Early versions of MSAL did not require http response to contain headers.
# As of April 2025, some Azure Identity code paths still yield response without headers.
# Here we mimic the behavior of header-less response by default,
# so that test cases can cover header-less response scenarios.
self.headers = headers
self._raw_resp = requests_resp

def raise_for_status(self):
Expand Down
23 changes: 8 additions & 15 deletions tests/test_throttled_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@ def close(self):
raise CloseMethodCalled("Not used by MSAL, but our customers may use it")


class DummyHttpClientWithoutResponseHeaders(DummyHttpClient):
def post(self, url, params=None, data=None, headers=None, **kwargs):
response = super().post(url, params, data, headers, **kwargs)
del response.headers # Early versions of MSAL did not require http client to return headers
return response

def get(self, url, params=None, headers=None, **kwargs):
response = super().get(url, params, headers, **kwargs)
del response.headers # Early versions of MSAL did not require http client to return headers
return response


class CloseMethodCalled(Exception):
pass

Expand Down Expand Up @@ -99,9 +87,14 @@ def assertValidResponse(self, response):
self.assertCleanPickle(response)

def test_throttled_http_client_base_response_should_tolerate_headerless_response(self):
http_client = ThrottledHttpClientBase(DummyHttpClientWithoutResponseHeaders(
status_code=200, response_text="foo"))
response = http_client.post("https://example.com")
# MSAL Python 1.32.1 had a regression that caused it to require headers in the response.
# This was fixed in 1.32.2
# https://github.com/AzureAD/microsoft-authentication-library-for-python/compare/1.32.1...1.32.2
# This test case is to ensure that we can tolerate headerless response.
http_client = DummyHttpClient(status_code=200, response_text="foo")
raw_response = http_client.post("https://example.com")
self.assertFalse(hasattr(raw_response, "headers"), "Should not contain headers")
response = ThrottledHttpClientBase(http_client).post("https://example.com")
self.assertEqual(response.text, "foo", "Should return the same response text")

def test_throttled_http_client_base_response_should_not_contain_signature(self):
Expand Down