atlassian-python-api
atlassian-python-api copied to clipboard
Cannot add a test mocking a JSON list response
There is no test for the get_issue_remote_links method, calling the REST API listing remote links of an issue.
Trying to add one, it seems the test framework does not handle a JSON response containing a list:
atlassian/jira.py:1731: in get_issue_remote_links
return self.get(url, params=params)
atlassian/rest_client.py:350: in get
advanced_mode=advanced_mode,
atlassian/rest_client.py:309: in request
log.debug("HTTP: Response text -> %s", response.text)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <Response [200]>
@property
def text(self):
"""Content of the response, in unicode.
If Response.encoding is None, encoding will be guessed using
``charset_normalizer`` or ``chardet``.
The encoding of the response content is determined based solely on HTTP
headers, following RFC 2616 to the letter. If you can take advantage of
non-HTTP knowledge to make a better guess at the encoding, you should
set ``r.encoding`` appropriately before accessing this property.
"""
# Try charset from content-type
content = None
encoding = self.encoding
if not self.content:
return ""
# Fallback to auto-detected encoding.
if self.encoding is None:
encoding = self.apparent_encoding
# Decode unicode from given encoding.
try:
content = str(self.content, encoding, errors="replace")
except (LookupError, TypeError):
# A LookupError is raised if the encoding was not found which could
# indicate a misspelling or similar mistake.
#
# A TypeError can be raised if encoding is None
#
# So we try blindly encoding.
> content = str(self.content, errors="replace")
E TypeError: decoding to str: need a bytes-like object, list found
Can you post the working branch? Here no mock is visible.
I was merely suggesting an API route which response has a list type, but you can test this behaviour with whichever you prefer, even modifying one which already exists if you so prefer.
The response payload would look like this:
responses[None] = [
…
]
Now I get you. Long time since I added the request mockup. It expects a dict or uses the content as it is. You can update the code in mockup.py to handle also other types. I suggest to change the current else to check if data is a byte string, in all other cases the element should be dumped as JSON string and convert it to a bytes string.