Fix #829 by adding the default encoding#833
Conversation
Codecov Report
@@ Coverage Diff @@
## main #833 +/- ##
==========================================
- Coverage 87.29% 87.23% -0.06%
==========================================
Files 30 30
Lines 3761 3761
Branches 337 337
==========================================
- Hits 3283 3281 -2
- Misses 329 331 +2
Partials 149 149
Continue to review full report at Codecov.
|
seratch
left a comment
There was a problem hiding this comment.
A few comments just for clarifying the intention
| if pattern == "html_response": | ||
| self.send_response(404) | ||
| self.set_common_headers() | ||
| self.send_header("content-type", "text/html;charset=utf-8") |
There was a problem hiding this comment.
I've changed this content-type that had been unintentionally application/json; charset=utf-8 from the beginning.
| except err.SlackApiError as e: | ||
| self.assertTrue( | ||
| str(e).startswith("Failed to parse the response body: Expecting value: line 1 column 1 (char 0)"), e) | ||
| self.assertEqual( |
There was a problem hiding this comment.
This is the expected behavior for async clients in this case.
| except err.SlackApiError as e: | ||
| self.assertTrue( | ||
| str(e).startswith("Failed to parse the response body: Expecting value: line 1 column 1 (char 0)"), e) | ||
| self.assertEqual( |
|
To reviewers, please let me know if you have any comments on this. I will be merging this within a few days. |
|
|
||
| html_response_body = '<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p>The requested URL /api/team.info was not found on this server.</p>\n</body></html>\n' | ||
|
|
||
| error_html_response_body = '<!DOCTYPE html>\n<html lang="en">\n<head>\n\t<meta charset="utf-8">\n\t<title>Server Error | Slack</title>\n\t<meta name="author" content="Slack">\n\t<style></style>\n</head>\n<body>\n\t<nav class="top persistent">\n\t\t<a href="https://status.slack.com/" class="logo" data-qa="logo"></a>\n\t</nav>\n\t<div id="page">\n\t\t<div id="page_contents">\n\t\t\t<h1>\n\t\t\t\t<svg width="30px" height="27px" viewBox="0 0 60 54" class="warning_icon"><path d="" fill="#D94827"/></svg>\n\t\t\t\tServer Error\n\t\t\t</h1>\n\t\t\t<div class="card">\n\t\t\t\t<p>It seems like there’s a problem connecting to our servers, and we’re investigating the issue.</p>\n\t\t\t\t<p>Please <a href="https://status.slack.com/">check our Status page for updates</a>.</p>\n\t\t\t</div>\n\t\t</div>\n\t</div>\n\t<script type="text/javascript">\n\t\tif (window.desktop) {\n\t\t\tdocument.documentElement.className = \'desktop\';\n\t\t}\n\n\t\tvar FIVE_MINS = 5 * 60 * 1000;\n\t\tvar TEN_MINS = 10 * 60 * 1000;\n\n\t\tfunction randomBetween(min, max) {\n\t\t\treturn Math.floor(Math.random() * (max - (min + 1))) + min;\n\t\t}\n\n\t\twindow.setTimeout(function () {\n\t\t\twindow.location.reload(true);\n\t\t}, randomBetween(FIVE_MINS, TEN_MINS));\n\t</script>\n</body>\n</html>' |
There was a problem hiding this comment.
Nice, I was trying to find the HTML response from the server! Super happy to see you have a copy.
There was a problem hiding this comment.
The original one is a bit long so that I removed the CSS in <style> tag from it 😺
| return | ||
|
|
||
| if pattern == "error_html_response": | ||
| self.send_response(503) |
There was a problem hiding this comment.
Nice, this was also the HTTP status code of the error that we experienced earlier this week. 👌🏻
| req, context=self.ssl, timeout=self.timeout | ||
| ) | ||
| charset = resp.headers.get_content_charset() | ||
| charset = resp.headers.get_content_charset() or "utf-8" |
There was a problem hiding this comment.
Why not add the type annotation?
charset: str = resp.headers.get_content_charset() or "utf-8"
There was a problem hiding this comment.
Good point. I'll update it later for consistency.
Summary
This pull request fixes #829 by adding default values for the charset that is supposed to be used for parsing response body from Slack. The situation may arise either when the
content-typeheader is missing orcontent-typeheader doesn't contain charset information in it.We can safely assume "utf-8" is the one it can fallback in the case where the server-side cannot return a valid response.
Category (place an
xin each of the[ ])Requirements (place an
xin each[ ])python setup.py validateafter making the changes.