Skip to content

Commit

Permalink
chore: set default timeout for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
open-dynaMIX committed Nov 7, 2023
1 parent a4ed3d9 commit d8c903c
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
import requests
from requests.auth import HTTPBasicAuth

REQUEST_TIMEOUT = 5


def get(uri, auth=None, headers=None, timeout=REQUEST_TIMEOUT):
return requests.get(uri, auth=auth, headers=headers, timeout=timeout)


def post(uri, data=None):
return requests.post(uri, data=data, timeout=REQUEST_TIMEOUT)


def get_uri(suffix, v=None, port=8080):
host = "localhost"
Expand All @@ -17,14 +27,14 @@ def get_uri(suffix, v=None, port=8080):


def get_status():
resp = requests.get(get_uri("api/status"))
resp = get(get_uri("api/status"))
assert resp.status_code == 200
return resp.json()


def is_responding(uri):
try:
requests.get(uri)
get(uri)
except requests.exceptions.ConnectionError:
return False
return True
Expand All @@ -43,7 +53,7 @@ def send(command, arg=None, arg2=None, expect=200, status=None):
for a in [arg, arg2]:
if a is not None:
api += f"/{a}"
resp = requests.post(get_uri(api))
resp = post(get_uri(api))
assert resp.status_code == expect
if status is not None:
return get_status()[status]
Expand Down Expand Up @@ -97,7 +107,7 @@ class TestsRequests:
],
)
def test_static(mpv_instance, uri, status_code, content_type):
resp = requests.get(get_uri(uri))
resp = get(get_uri(uri))
assert resp.status_code == status_code
if status_code != 200:
return
Expand All @@ -113,7 +123,7 @@ def test_static(mpv_instance, uri, status_code, content_type):
@staticmethod
@pytest.mark.parametrize("uri", ["", "/", "//", "///"])
def test_index(mpv_instance, uri):
resp = requests.get(get_uri(uri))
resp = get(get_uri(uri))
assert resp.status_code == 200

resp.headers.pop("Content-Length")
Expand Down Expand Up @@ -206,7 +216,7 @@ def test_post_wrong_args(mpv_instance, snapshot, endpoint, arg, arg2):
for a in [arg, arg2]:
if a is not None:
api += f"/{a}"
response = requests.post(get_uri(api))
response = post(get_uri(api))
assert response.status_code == 400
snapshot.assert_match(response.json())

Expand Down Expand Up @@ -408,7 +418,7 @@ def test_not_allowed_methods(mpv_instance, endpoint, method, expected):
],
)
def test_collections(self, endpoint, expected_status, mpv_instance, snapshot):
resp = requests.get(f"{get_uri(endpoint)}")
resp = get(get_uri(endpoint))

assert resp.status_code == expected_status

Expand Down Expand Up @@ -493,7 +503,7 @@ def send_loadfile(url, mode=None, expect=200):
indirect=["mpv_instance"],
)
def test_static_dir_config(mpv_instance, status_code):
resp = requests.get(get_uri("static.json"))
resp = get(get_uri("static.json"))
assert resp.status_code == status_code

if status_code == 200:
Expand Down Expand Up @@ -588,7 +598,7 @@ def test_disablers(mpv_instance, v4_works, v6_works):
)
def test_auth(htpasswd, mpv_instance, auth, status_code):
try:
resp = requests.get(get_uri("api/status"), auth=auth, timeout=0.5)
resp = get(get_uri("api/status"), auth=auth, timeout=0.5)
except requests.exceptions.ReadTimeout:
assert status_code is None
return
Expand Down Expand Up @@ -648,9 +658,7 @@ def test_logging(htpasswd, mpv_instance, use_auth, username, password, status_co
auth = None
if use_auth and username:
auth = HTTPBasicAuth(username, password)
resp = requests.get(
get_uri("api/status"), auth=auth, headers={"Referer": "https://referer"}
)
resp = get(get_uri("api/status"), auth=auth, headers={"Referer": "https://referer"})
assert resp.status_code == status_code

# example log line
Expand Down

0 comments on commit d8c903c

Please sign in to comment.