1
- """This module contains unit tests for the HTTPClient class."""
1
+ """This module contains functional tests for the HTTPClient class."""
2
2
# vim: set filetype=python ts=4 sw=4
3
3
# -*- coding: utf-8 -*-
4
4
import pytest
5
+ import requests
5
6
from requests import RequestException
6
7
from tokendito import __title__
7
8
from tokendito import __version__
8
9
from tokendito .http_client import HTTPClient
9
10
10
11
12
+ def is_httpbin_available ():
13
+ """Check if httpbin.org is available for testing."""
14
+ try :
15
+ response = requests .get ("https://httpbin.org/get" , timeout = 5 )
16
+ return response .status_code == 200
17
+ except (requests .RequestException , requests .Timeout ):
18
+ return False
19
+
20
+
21
+ httpbin_available = pytest .mark .skipif (
22
+ not is_httpbin_available (), reason = "httpbin.org is not available"
23
+ )
24
+
25
+
11
26
@pytest .fixture
12
27
def client ():
13
28
"""Fixture to create and return an HTTPClient instance."""
@@ -16,6 +31,7 @@ def client():
16
31
return client
17
32
18
33
34
+ @httpbin_available
19
35
def test_get_request (client ):
20
36
"""Test the GET request functionality of HTTPClient."""
21
37
# Make a GET request to the /get endpoint of httpbin which reflects the sent request data
@@ -27,6 +43,7 @@ def test_get_request(client):
27
43
assert json_data ["headers" ]["User-Agent" ] == f"{ __title__ } /{ __version__ } "
28
44
29
45
46
+ @httpbin_available
30
47
def test_post_request (client ):
31
48
"""Test the POST request functionality of HTTPClient."""
32
49
# Make a POST request to the /post endpoint of httpbin with sample data
@@ -38,6 +55,7 @@ def test_post_request(client):
38
55
assert json_data ["json" ] == {"key" : "value" }
39
56
40
57
58
+ @httpbin_available
41
59
def test_add_cookies (client ):
42
60
"""Test the ability to set cookies using HTTPClient."""
43
61
# Set a test cookie for the client
@@ -51,6 +69,7 @@ def test_add_cookies(client):
51
69
assert json_data ["cookies" ] == {"test_cookie" : "cookie_value" }
52
70
53
71
72
+ @httpbin_available
54
73
def test_custom_header (client ):
55
74
"""Test the ability to send custom headers using HTTPClient."""
56
75
# Make a GET request with a custom header
@@ -75,6 +94,7 @@ def test_bad_post_request(client, mocker):
75
94
client .post ("https://httpbin.org/post" , json = {"key" : "value" })
76
95
77
96
97
+ @httpbin_available
78
98
def test_reset_session (client ):
79
99
"""Test the reset method to ensure session is reset."""
80
100
# Set a test cookie for the client
0 commit comments