-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init test construct #0
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |