Skip to content

Commit

Permalink
Support 202 HTTP responses
Browse files Browse the repository at this point in the history
(cherry picked from commit c185be7)
  • Loading branch information
MaybeNetwork authored and LilSpazJoekp committed Jun 15, 2021
1 parent 733283c commit 15aedd8
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.rst
Expand Up @@ -4,6 +4,10 @@ Change Log
Unreleased
----------

**Added**

- Support 202 "Accepted" HTTP responses.

2.1.0 (2021/06/15)
------------------

Expand Down
2 changes: 1 addition & 1 deletion asyncprawcore/sessions.py
Expand Up @@ -106,7 +106,7 @@ class Session(object):
520: ServerError,
522: ServerError,
}
SUCCESS_STATUSES = {codes["created"], codes["ok"]}
SUCCESS_STATUSES = {codes["accepted"], codes["created"], codes["ok"]}

@staticmethod
def _log_request(data, method, params, url):
Expand Down
114 changes: 114 additions & 0 deletions tests/cassettes/Session_request__accepted.json
@@ -0,0 +1,114 @@
{
"interactions": [
{
"request": {
"body": [
[
"grant_type",
"password"
],
[
"password",
"<PASSWORD>"
],
[
"username",
"<USERNAME>"
]
],
"headers": {
"AUTHORIZATION": [
"Basic <BASIC_AUTH>"
],
"Connection": [
"close"
],
"User-Agent": [
"asyncprawcore:test (by /u/Lil_SpazJoekp) asyncprawcore/2.1.1.dev0"
]
},
"method": "POST",
"uri": "https://www.reddit.com/api/v1/access_token"
},
"response": {
"body": {
"string": "{\"access_token\": \"<ACCESS_TOKEN>\", \"token_type\": \"bearer\", \"expires_in\": 3600, \"scope\": \"*\"}"
},
"headers": {
"Accept-Ranges": "bytes",
"Cache-Control": "max-age=0, must-revalidate",
"Connection": "close",
"Content-Length": "121",
"Content-Type": "application/json; charset=UTF-8",
"Date": "Tue, 15 Jun 2021 03:31:32 GMT",
"Server": "snooserv",
"Set-Cookie": "edgebucket=o244HKpZJ88X6IL9eQ; Domain=reddit.com; Max-Age=63071999; Path=/; secure",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Via": "1.1 varnish",
"X-Clacks-Overhead": "GNU Terry Pratchett",
"X-Moose": "majestic",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-ratelimit-remaining": "268",
"x-ratelimit-reset": "508",
"x-ratelimit-used": "32",
"x-xss-protection": "1; mode=block"
},
"status": {
"code": 200,
"message": "OK"
},
"url": "https://www.reddit.com/api/v1/access_token"
}
},
{
"request": {
"body": null,
"headers": {
"Authorization": [
"bearer 266290221070-kE8lqRExM_Eo9Mnob4h9yV8RWcqp-Q"
],
"User-Agent": [
"asyncprawcore:test (by /u/Lil_SpazJoekp) asyncprawcore/2.1.1.dev0"
]
},
"method": "POST",
"uri": "https://oauth.reddit.com/api/read_all_messages?raw_json=1"
},
"response": {
"body": {
"string": "{}"
},
"headers": {
"Accept-Ranges": "bytes",
"Cache-Control": "private, s-maxage=0, max-age=0, must-revalidate, no-store, max-age=0, must-revalidate",
"Connection": "keep-alive",
"Content-Length": "2",
"Content-Type": "application/json; charset=UTF-8",
"Date": "Tue, 15 Jun 2021 03:31:33 GMT",
"Expires": "-1",
"Server": "snooserv",
"Set-Cookie": "session_tracker=ljdfdirrcrhijofccl.0.1623727892999.Z0FBQUFBQmd5QjhWaGI1MlBuMlB0WHFvV01rWlRqV0V4bHZ1ZFhlMDJPQUtBeDBySkRSVWhnNmFiOFg2bDJfXzVRbERLbVdVZWlqMzREMXUxa0NpeUhSRTFSTEZJVXAxSDA4RTBRYTRqbnVzZEd0MDFXbGg0NVh0b3hOWlJlWXFBejFfOXMteXZWWUk; Domain=reddit.com; Max-Age=7199; Path=/; expires=Tue, 15-Jun-2021 05:31:33 GMT; secure",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Via": "1.1 varnish",
"X-Clacks-Overhead": "GNU Terry Pratchett",
"X-Moose": "majestic",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-ratelimit-remaining": "599.0",
"x-ratelimit-reset": "507",
"x-ratelimit-used": "1",
"x-ua-compatible": "IE=edge",
"x-xss-protection": "1; mode=block"
},
"status": {
"code": 202,
"message": "Accepted"
},
"url": "https://oauth.reddit.com/api/read_all_messages?raw_json=1"
}
}
],
"recorded_at": "2021-06-14T22:31:32",
"version": 1
}
9 changes: 9 additions & 0 deletions tests/test_sessions.py
Expand Up @@ -111,6 +111,15 @@ def test_init__with_implicit_authorizer(self):
authorizer = asyncprawcore.ImplicitAuthorizer(authenticator, None, 0, "")
asyncprawcore.Session(authorizer)

async def test_request__accepted(self):
with VCR.use_cassette("Session_request__accepted"):
session = asyncprawcore.Session(await script_authorizer())
with LogCapture(level=logging.DEBUG) as log_capture:
await session.request("POST", "api/read_all_messages")
log_capture.check_present(
("asyncprawcore", "DEBUG", "Response: 202 (2 bytes)")
)

@patch("aiohttp.ClientSession")
async def test_request__connection_error_retry(self, mock_session):
session_instance = mock_session.return_value
Expand Down

0 comments on commit 15aedd8

Please sign in to comment.