-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_integration.py
More file actions
141 lines (115 loc) · 4.39 KB
/
Copy pathtest_integration.py
File metadata and controls
141 lines (115 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Integration test - Start proxy and send a real request
"""
import socket
import time
import subprocess
import sys
import signal
def send_test_request():
"""Send a test request to the proxy"""
print("\n[Client] Connecting to proxy at localhost:8080...")
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
sock.connect(('localhost', 8080))
print("[Client] ✓ Connected to proxy")
# Send request
print("[Client] Sending request for http://example.com...")
request = b"GET /http://example.com HTTP/1.0\r\n"
request += b"Host: localhost\r\n"
request += b"\r\n"
sock.sendall(request)
print("[Client] ✓ Request sent")
# Receive response
print("[Client] Waiting for response...")
response = b""
start_time = time.time()
while time.time() - start_time < 10:
try:
chunk = sock.recv(4096)
if not chunk:
break
response += chunk
if len(response) > 100: # Got enough to check
break
except socket.timeout:
break
sock.close()
if response:
response_text = response.decode('utf-8', errors='ignore')
lines = response_text.split('\n')
status_line = lines[0] if lines else ""
print(f"[Client] ✓ Received response: {status_line}")
print(f"[Client] Total bytes: {len(response)}")
if response_text.startswith('HTTP/'):
print("[Client] ✓ Valid HTTP response")
# Check status code
if '200' in status_line:
print("[Client] ✓ Status 200 OK")
return True
else:
print(f"[Client] ⚠ Non-200 status: {status_line}")
return True # Still valid response
else:
print("[Client] ✗ Invalid HTTP response")
return False
else:
print("[Client] ✗ No response received")
return False
except ConnectionRefusedError:
print("[Client] ✗ Connection refused - is the proxy running?")
return False
except Exception as e:
print(f"[Client] ✗ Error: {e}")
return False
def main():
print("=" * 60)
print("Integration Test - Proxy with Real Request")
print("=" * 60)
proxy_path = r"c:\Users\LMC\OneDrive - THS\Desktop\RetroBrowser_Project\src\proxy\proxy.py"
python_exe = r"C:/Users/LMC/AppData/Local/Programs/Python/Python313/python.exe"
print("\n[Test] Starting proxy server...")
# Start proxy in subprocess
proxy_process = subprocess.Popen(
[python_exe, proxy_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP # Allow clean termination
)
# Wait for proxy to start
print("[Test] Waiting 3 seconds for proxy to initialize...")
time.sleep(3)
# Check if proxy is still running
if proxy_process.poll() is not None:
stdout, stderr = proxy_process.communicate()
print(f"[Test] ✗ Proxy exited prematurely!")
print(f"[Test] stdout: {stdout.decode('utf-8', errors='ignore')[:200]}")
print(f"[Test] stderr: {stderr.decode('utf-8', errors='ignore')[:200]}")
return False
print("[Test] ✓ Proxy appears to be running")
# Send test request
success = send_test_request()
# Cleanup: Stop the proxy
print("\n[Test] Stopping proxy server...")
try:
# Send Ctrl+C signal to the process group
proxy_process.send_signal(signal.CTRL_C_EVENT)
proxy_process.wait(timeout=5)
print("[Test] ✓ Proxy stopped gracefully")
except:
proxy_process.terminate()
proxy_process.wait(timeout=2)
print("[Test] ⚠ Proxy terminated forcefully")
print("\n" + "=" * 60)
if success:
print("✓ INTEGRATION TEST PASSED!")
else:
print("✗ INTEGRATION TEST FAILED!")
print("=" * 60)
return success
if __name__ == '__main__':
success = main()
sys.exit(0 if success else 1)