Skip to content

Only attempt to parse JSON for JSON responses - #84

Merged
maxdeviant merged 1 commit into
masterfrom
dont-parse-json-for-non-json-responses
Sep 13, 2021
Merged

maxdeviant merged 1 commit into
masterfrom
dont-parse-json-for-non-json-responses

Conversation

@maxdeviant

@maxdeviant maxdeviant commented Sep 10, 2021 •

Copy link
Copy Markdown
Contributor

This PR fixes an issue where we were trying to parse the response body as JSON for API responses that did not return JSON.

We now only parse the response body as JSON if it has a Content-Type of application/json.

Closes #80.

@maxdeviant
maxdeviant requested a review from a team as a code owner September 10, 2021 18:58
@maxdeviant maxdeviant self-assigned this Sep 10, 2021
@maxdeviant maxdeviant changed the title Only attempt to parse the JSON for JSON responses Only attempt to parse JSON for JSON responses Sep 10, 2021
Comment thread tests/conftest.py
def __init__(self, content, status_code, headers=None):
self.content = content
self.status_code = status_code
self.headers = {} if headers is None else headers

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this {} default up to the parameter declaration in these mock helpers?

class MockRawResponse(object):
    def __init__(self, content, status_code, headers={}):
        self.content = content
        self.status_code = status_code
        self.headers = headers

Not sure if there are any places in the tests that explicitly pass headers=None though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, that seems like a good simplification.

I can open up a follow-up PR.

Comment thread workos/utils/request.py
raise ServerException(response)
content_type = (
response.headers.get("content-type")
if response.headers is not None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This null check is reasonable, but do we know when response.headers is possibly None?

I'm not familiar with the API, so I could see it possibly always defaulting to an empty dictionary. But maybe it is None in the case of client errors when there is no response at all?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I added this check after I had seen it in my test cases. Not sure if it is because we have a specific instance where we're setting the headers to None.

@maxdeviant
maxdeviant merged commit 5c1e671 into master Sep 13, 2021
@maxdeviant
maxdeviant deleted the dont-parse-json-for-non-json-responses branch September 13, 2021 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants