Improve the debug mode, fixes #4

pull/9/head
Thomas Sileo 2018-06-24 11:33:14 +02:00
rodzic 3f59845b8a
commit c2524dccb2
3 zmienionych plików z 18 dodań i 11 usunięć

Wyświetl plik

@ -8,13 +8,20 @@ import requests
from .__version__ import __version__
from .errors import ActivityNotFoundError
from .errors import RemoteActivityGoneError
from .urlutils import check_url
from .urlutils import check_url as check_url
if typing.TYPE_CHECKING:
from little_boxes import activitypub as ap # noqa: type checking
class Backend(abc.ABC):
def debug_mode(self) -> bool:
"""Should be overidded to return `True` in order to enable the debug mode."""
return False
def check_url(self, url: str) -> None:
check_url(url, debug=self.debug_mode())
def user_agent(self) -> str:
return (
f"{requests.utils.default_user_agent()} (Little Boxes/{__version__};"
@ -26,7 +33,7 @@ class Backend(abc.ABC):
return binascii.hexlify(os.urandom(8)).decode("utf-8")
def fetch_json(self, url: str, **kwargs):
check_url(url)
self.check_url(url)
resp = requests.get(
url,
headers={"User-Agent": self.user_agent(), "Accept": "application/json"},
@ -53,7 +60,7 @@ class Backend(abc.ABC):
pass # pragma: no cover
def fetch_iri(self, iri: str, **kwargs) -> "ap.ObjectType": # pragma: no cover
check_url(iri)
self.check_url(iri)
resp = requests.get(
iri,
headers={

Wyświetl plik

@ -1,11 +1,9 @@
import ipaddress
import logging
import os
import socket
from typing import Dict
from urllib.parse import urlparse
from . import strtobool
from .errors import Error
logger = logging.getLogger(__name__)
@ -18,14 +16,13 @@ class InvalidURLError(Error):
pass
def is_url_valid(url: str) -> bool:
def is_url_valid(url: str, debug: bool = False) -> bool:
parsed = urlparse(url)
if parsed.scheme not in ["http", "https"]:
return False
# XXX in debug mode, we want to allow requests to localhost to test the federation with local instances
debug_mode = strtobool(os.getenv("MICROBLOGPUB_DEBUG", "false"))
if debug_mode: # pragma: no cover
if debug: # pragma: no cover
return True
if parsed.hostname in ["localhost"]:
@ -56,9 +53,9 @@ def is_url_valid(url: str) -> bool:
return True
def check_url(url: str) -> None:
logger.debug(f"check_url {url}")
if not is_url_valid(url):
def check_url(url: str, debug: bool = False) -> None:
logger.debug(f"check_url {url} debug={debug}")
if not is_url_valid(url, debug=debug):
raise InvalidURLError(f'"{url}" is invalid')
return None

Wyświetl plik

@ -65,6 +65,9 @@ class InMemBackend(Backend):
return calls
def debug_mode(self) -> bool:
return True
def setup_actor(self, name, pusername):
"""Create a new actor in this backend."""
p = ap.Person(