diff --git a/tests/test_0.py b/tests/test_0.py new file mode 100644 index 0000000..62fe2de --- /dev/null +++ b/tests/test_0.py @@ -0,0 +1,52 @@ +import pytest +import socket +from unittest.mock import patch, MagicMock +from test_proxy import check_open_ports, get_ssl_info, check_http_headers, detect_proxy + +# Mock host to use in tests +MOCK_HOST = 'example.com' + +def test_check_open_ports(): + ports = [80, 443, 8080] + # Assuming the ports are closed, we expect an empty list in return + with patch('socket.create_connection', side_effect=socket.timeout): + open_ports = check_open_ports(MOCK_HOST, ports) + assert open_ports == [] + +def test_get_ssl_info(): + # Mock the SSL socket and cert data + mock_cert = { + 'subject': ((('commonName', 'example.com'),),), + 'issuer': ((('countryName', 'US'),), (('organizationName', 'DigiCert Inc'),), (('commonName', 'DigiCert SHA2 Secure Server CA'),)), + 'version': 3 + } + + with patch('ssl.create_default_context'), \ + patch('socket.create_connection', MagicMock()), \ + patch('ssl.SSLSocket.getpeercert', return_value=mock_cert): + + ssl_info = get_ssl_info(MOCK_HOST) + assert ssl_info['subject'] == ((('commonName', 'example.com'),),) + +def test_check_http_headers(): + mock_headers = { + 'Server': 'Apache', + 'Content-Type': 'text/html; charset=UTF-8' + } + + with patch('requests.head') as mock_request: + mock_response = MagicMock() + mock_response.headers = mock_headers + mock_request.return_value = mock_response + + headers = check_http_headers(f'http://{MOCK_HOST}') + assert headers == mock_headers + +def test_detect_proxy(): + # Mock detect_proxy by just checking for open ports and headers + with patch('socket.gethostbyname', return_value='93.184.216.34'), \ + patch('test_proxy.check_open_ports', return_value=[80, 443]), \ + patch('test_proxy.get_ssl_info', return_value=None), \ + patch('test_proxy.check_http_headers', return_value=None): + + detect_proxy(MOCK_HOST) # We only want to make sure the function runs without exceptions