Skip to content

Commit

Permalink
Improve coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
LilSpazJoekp committed Sep 29, 2020
1 parent a7f3970 commit a822236
Show file tree
Hide file tree
Showing 12 changed files with 555 additions and 155 deletions.
2 changes: 1 addition & 1 deletion asyncprawcore/requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Requestor(object):
"""Requestor provides an interface to HTTP requests."""

def __getattr__(self, attribute):
def __getattr__(self, attribute): # pragma: no cover
"""Pass all undefined attributes to the _http attribute."""
if attribute.startswith("__"):
raise AttributeError
Expand Down
11 changes: 2 additions & 9 deletions asyncprawcore/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,6 @@ def _log_request(data, method, params, url):
log.debug(f"Data: {data}")
log.debug(f"Params: {params}")

@staticmethod
async def _retry_sleep(retries):
if retries < 3:
base = 0 if retries == 2 else 2
sleep_seconds = base + 2 * random.random()
message = f"Sleeping: {sleep_seconds:0.2f} seconds prior to retry"
log.debug(message)
await asyncio.sleep(sleep_seconds)

def __init__(self, authorizer):
"""Preprare the connection to reddit's API.
Expand Down Expand Up @@ -218,6 +209,8 @@ async def _request_with_retries(
):
if retry_strategy_state is None:
retry_strategy_state = self._retry_strategy_class()

await retry_strategy_state.sleep()
self._log_request(data, method, params, url)
response, saved_exception = await self._make_request(
data,
Expand Down
2 changes: 1 addition & 1 deletion tests/cassettes/Authorizer_refresh.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
[
"refresh_token",
"fake_refresh_token"
"fake_refresh_token"
]
],
"headers": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
[
"refresh_token",
"fake_refresh_token"
"fake_refresh_token"
]
],
"headers": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
[
"refresh_token",
"fake_refresh_token"
"fake_refresh_token"
]
],
"headers": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"body": [
[
"code",
"fake_code"
"fake_code"
],
[
"grant_type",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
[
"refresh_token",
"fake_refresh_token"
"fake_refresh_token"
]
],
"headers": {
Expand Down
209 changes: 209 additions & 0 deletions tests/cassettes/Session_request__cloudflare_connection_timed_out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
{
"version": 1,
"interactions": [
{
"request": {
"method": "POST",
"uri": "https://www.reddit.com/api/v1/access_token",
"body": [
[
"grant_type",
"client_credentials"
]
],
"headers": {
"User-Agent": [
"asyncprawcore:test (by /u/bboe) asyncprawcore/1.4.0"
],
"Connection": [
"close"
],
"AUTHORIZATION": [
"Basic <placeholder_auth>"
]
}
},
"response": {
"status": {
"code": 200,
"message": "OK"
},
"headers": {
"Connection": "close",
"Content-Length": "106",
"Content-Type": "application/json; charset=UTF-8",
"x-reddit-loid": "00000000006wrw7w8p.2.1592041222022.Z0FBQUFBQmU1SjhHRUlOdWdkVGVzQXdZUnJyZFFZMFdsOVliODNWdi1CVVlkSjEtR1dmZmpiNGgzU2pjR251VDZLVG8xNVh4RmNEVjVWcWRQd3BpYWFVeEFUNzg4RGRRM1hfWUQyUGpXTXJIZEpqd1hIODc2a3FILXZVbEZfZ1Z6cW85MXZsM2duanM",
"x-frame-options": "SAMEORIGIN",
"x-content-type-options": "nosniff",
"x-xss-protection": "1; mode=block",
"cache-control": "max-age=0, must-revalidate",
"X-Moose": "majestic",
"Accept-Ranges": "bytes",
"Date": "Sat, 13 Jun 2020 09:40:22 GMT",
"Via": "1.1 varnish",
"X-Served-By": "cache-ams21024-AMS",
"X-Cache": "MISS",
"X-Cache-Hits": "0",
"X-Timer": "S1592041222.961421,VS0,VE125",
"Set-Cookie": "edgebucket=CGDzbl0TO4ItVFOpuU; Domain=reddit.com; Max-Age=63071999; Path=/; secure",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Server": "snooserv"
},
"body": {
"string": "{\"access_token\": \"fake_access_Token\", \"token_type\": \"bearer\", \"expires_in\": 3600, \"scope\": \"*\"}"
},
"url": "https://www.reddit.com/api/v1/access_token"
}
},
{
"request": {
"method": "GET",
"uri": "https://oauth.reddit.com/?raw_json=1",
"body": null,
"headers": {
"User-Agent": [
"asyncprawcore:test (by /u/bboe) asyncprawcore/1.4.0"
],
"Authorization": [
"bearer <beaerer_token>"
]
}
},
"response": {
"status": {
"code": 522,
"message": "CLOUDFLARE TIMEOUT"
},
"headers": {
"Connection": "keep-alive",
"Content-Length": "49696",
"Content-Type": "application/json; charset=UTF-8",
"x-ua-compatible": "IE=edge",
"x-frame-options": "SAMEORIGIN",
"x-content-type-options": "nosniff",
"x-xss-protection": "1; mode=block",
"access-control-allow-origin": "*",
"access-control-expose-headers": "X-Moose",
"Content-Encoding": "gzip",
"cache-control": "max-age=0, must-revalidate",
"X-Moose": "majestic",
"Accept-Ranges": "bytes",
"Date": "Sat, 13 Jun 2020 09:40:23 GMT",
"Via": "1.1 varnish",
"X-Served-By": "cache-ams21035-AMS",
"X-Cache": "MISS",
"X-Cache-Hits": "0",
"X-Timer": "S1592041222.249128,VS0,VE979",
"Vary": "accept-encoding",
"Set-Cookie": "csv=1; Max-Age=63072000; Domain=.reddit.com; Path=/; Secure; SameSite=None",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Server": "snooserv"
},
"body": {
"string": ""
},
"url": "https://oauth.reddit.com/?raw_json=1"
}
},
{
"request": {
"method": "GET",
"uri": "https://oauth.reddit.com/?raw_json=1",
"body": null,
"headers": {
"User-Agent": [
"asyncprawcore:test (by /u/bboe) asyncprawcore/1.4.0"
],
"Authorization": [
"bearer <beaerer_token>"
]
}
},
"response": {
"status": {
"code": 522,
"message": "CLOUDFLARE TIMEOUT"
},
"headers": {
"Connection": "keep-alive",
"Content-Length": "49710",
"Content-Type": "application/json; charset=UTF-8",
"x-ua-compatible": "IE=edge",
"x-frame-options": "SAMEORIGIN",
"x-content-type-options": "nosniff",
"x-xss-protection": "1; mode=block",
"access-control-allow-origin": "*",
"access-control-expose-headers": "X-Moose",
"Content-Encoding": "gzip",
"cache-control": "max-age=0, must-revalidate",
"X-Moose": "majestic",
"Accept-Ranges": "bytes",
"Date": "Sat, 13 Jun 2020 09:40:24 GMT",
"Via": "1.1 varnish",
"X-Served-By": "cache-ams21035-AMS",
"X-Cache": "MISS",
"X-Cache-Hits": "0",
"X-Timer": "S1592041223.350620,VS0,VE1141",
"Vary": "accept-encoding",
"Set-Cookie": "session_tracker=ckirohpqnjrmedgqnn.0.1592041223428.Z0FBQUFBQmU1SjhJeEhlNUZYMHVXOUMzSXRhdXZZbld1Skt6VXhxQ29paGg1bVNhMWJ5US1VRVhKNzN6UTRTTEc3RVpFVldxbXRXQ0p6d0pSeFJmSGNfTlVNVVhqUlhobVZFQzZJbVhvUUlVT2Z2bjNVX05zcHdUeXhCaXZWNUxSaElNV1lQRTdGaVU; Domain=reddit.com; Max-Age=7199; Path=/; expires=Sat, 13-Jun-2020 11:40:24 GMT; secure; SameSite=None; Secure",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Server": "snooserv"
},
"body": {
"string": ""
},
"url": "https://oauth.reddit.com/?raw_json=1"
}
},
{
"request": {
"method": "GET",
"uri": "https://oauth.reddit.com/?raw_json=1",
"body": null,
"headers": {
"User-Agent": [
"asyncprawcore:test (by /u/bboe) asyncprawcore/1.4.0"
],
"Authorization": [
"bearer <beaerer_token>"
]
}
},
"response": {
"status": {
"code": 522,
"message": "CLOUDFLARE TIMEOUT"
},
"headers": {
"Connection": "keep-alive",
"Content-Length": "49688",
"Content-Type": "application/json; charset=UTF-8",
"x-ua-compatible": "IE=edge",
"x-frame-options": "SAMEORIGIN",
"x-content-type-options": "nosniff",
"x-xss-protection": "1; mode=block",
"access-control-allow-origin": "*",
"access-control-expose-headers": "X-Moose",
"Content-Encoding": "gzip",
"cache-control": "max-age=0, must-revalidate",
"X-Moose": "majestic",
"Accept-Ranges": "bytes",
"Date": "Sat, 13 Jun 2020 09:40:25 GMT",
"Via": "1.1 varnish",
"X-Served-By": "cache-ams21035-AMS",
"X-Cache": "MISS",
"X-Cache-Hits": "0",
"X-Timer": "S1592041225.653837,VS0,VE999",
"Vary": "accept-encoding",
"Set-Cookie": "session_tracker=ckirohpqnjrmedgqnn.0.1592041224723.Z0FBQUFBQmU1SjhKbVd6eVV0bTl2T1FSRTlKa1hZVG4zQWl2NWdnMU5hbHB3VDBmcFNKRGpjYzllNXVLRWstcFhrTzY5UDhLQ3pleFd6VXZmdDk2SE85dFJKbGhaazZpbmVaYy1MOTV4UWNPWHVxUEd2SEZ2Zm44QnBxOW02WjhjUmJSTUk5czZ3djk; Domain=reddit.com; Max-Age=7199; Path=/; expires=Sat, 13-Jun-2020 11:40:25 GMT; secure; SameSite=None; Secure",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"Server": "snooserv"
},
"body": {
"string": ""
},
"url": "https://oauth.reddit.com/?raw_json=1"
}
}
]
}
Loading

0 comments on commit a822236

Please sign in to comment.