Skip to content

Commit

Permalink
Create test_0.py
Browse files Browse the repository at this point in the history
init test construct #0
  • Loading branch information
geeknik authored Sep 5, 2024
1 parent eb30753 commit d0ae746
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/test_0.py
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

0 comments on commit d0ae746

Please sign in to comment.