Skip to content
Open
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
4 changes: 3 additions & 1 deletion plaid/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ def __init__(self, status=None, reason=None, http_resp=None):
self.status = http_resp.status
self.reason = http_resp.reason
self.body = http_resp.data
self.headers = http_resp.getheaders()
self.headers = getattr(http_resp, "headers", None)
if self.headers is None:
self.headers = http_resp.getheaders()
else:
self.status = status
self.reason = reason
Expand Down
48 changes: 48 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import unittest
import warnings

from plaid.exceptions import ApiException


class RawHttpResponse:
status = 400
reason = "Bad Request"
data = b""
headers = {"X-Test": "value"}

def getheaders(self):
warnings.warn(
"HTTPResponse.getheaders() is deprecated",
DeprecationWarning,
stacklevel=2,
)
return self.headers


class WrappedHttpResponse:
status = 400
reason = "Bad Request"
data = b""

def getheaders(self):
return {"X-Test": "value"}


class ApiExceptionTest(unittest.TestCase):
def test_raw_response_uses_headers_without_deprecated_getheaders(self):
response = RawHttpResponse()

with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
exception = ApiException(http_resp=response)

self.assertEqual(exception.headers, response.headers)

def test_wrapped_response_keeps_getheaders_fallback(self):
exception = ApiException(http_resp=WrappedHttpResponse())

self.assertEqual(exception.headers, {"X-Test": "value"})


if __name__ == "__main__":
unittest.main()