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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Integration tests for proxy export formats.
|
| 3 |
+
Tests all 4 export formats: txt, json, csv, pac
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import pytest
|
| 7 |
+
from fastapi.testclient import TestClient
|
| 8 |
+
from app.main import app
|
| 9 |
+
import json
|
| 10 |
+
|
| 11 |
+
client = TestClient(app)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def test_export_txt_format():
|
| 15 |
+
"""Test TXT format export returns plain text list of proxy URLs."""
|
| 16 |
+
response = client.get("/api/v1/proxies/export?format=txt&limit=10")
|
| 17 |
+
|
| 18 |
+
assert response.status_code == 200
|
| 19 |
+
assert response.headers["content-type"] == "text/plain; charset=utf-8"
|
| 20 |
+
|
| 21 |
+
# Content should be newline-separated URLs
|
| 22 |
+
content = response.text
|
| 23 |
+
if content.strip(): # If there are proxies
|
| 24 |
+
lines = content.strip().split("\n")
|
| 25 |
+
for line in lines:
|
| 26 |
+
# Each line should look like a URL
|
| 27 |
+
assert "://" in line or ":" in line # Either full URL or IP:port
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_export_json_format():
|
| 31 |
+
"""Test JSON format export returns structured JSON data."""
|
| 32 |
+
response = client.get("/api/v1/proxies/export?format=json&limit=10")
|
| 33 |
+
|
| 34 |
+
assert response.status_code == 200
|
| 35 |
+
assert "application/json" in response.headers["content-type"]
|
| 36 |
+
|
| 37 |
+
# Should be valid JSON
|
| 38 |
+
data = response.json()
|
| 39 |
+
assert isinstance(data, list)
|
| 40 |
+
|
| 41 |
+
# If there are proxies, check structure
|
| 42 |
+
if len(data) > 0:
|
| 43 |
+
proxy = data[0]
|
| 44 |
+
assert "url" in proxy
|
| 45 |
+
assert "protocol" in proxy
|
| 46 |
+
# Optional fields
|
| 47 |
+
assert "country" in proxy or True
|
| 48 |
+
assert "quality_score" in proxy or True
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def test_export_csv_format():
|
| 52 |
+
"""Test CSV format export returns CSV with headers."""
|
| 53 |
+
response = client.get("/api/v1/proxies/export?format=csv&limit=10")
|
| 54 |
+
|
| 55 |
+
assert response.status_code == 200
|
| 56 |
+
assert response.headers["content-type"] == "text/csv; charset=utf-8"
|
| 57 |
+
assert "attachment" in response.headers.get("content-disposition", "")
|
| 58 |
+
|
| 59 |
+
# Content should have CSV headers
|
| 60 |
+
content = response.text
|
| 61 |
+
lines = content.strip().split("\n")
|
| 62 |
+
|
| 63 |
+
if len(lines) > 0:
|
| 64 |
+
# First line should be headers
|
| 65 |
+
headers = lines[0]
|
| 66 |
+
assert "URL" in headers
|
| 67 |
+
assert "Protocol" in headers
|
| 68 |
+
assert "Country" in headers
|
| 69 |
+
assert "Quality" in headers
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_export_pac_format():
|
| 73 |
+
"""Test PAC format export returns proxy auto-config file."""
|
| 74 |
+
response = client.get("/api/v1/proxies/export?format=pac&limit=10")
|
| 75 |
+
|
| 76 |
+
assert response.status_code == 200
|
| 77 |
+
assert response.headers["content-type"] == "application/x-ns-proxy-autoconfig"
|
| 78 |
+
assert "attachment" in response.headers.get("content-disposition", "")
|
| 79 |
+
assert "1proxy.pac" in response.headers.get("content-disposition", "")
|
| 80 |
+
|
| 81 |
+
# Content should be a valid PAC file
|
| 82 |
+
content = response.text
|
| 83 |
+
assert "function FindProxyForURL" in content
|
| 84 |
+
assert "return" in content
|
| 85 |
+
|
| 86 |
+
# Should have localhost bypass rules
|
| 87 |
+
assert "localhost" in content.lower() or "127.0.0.1" in content
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def test_export_with_filters():
|
| 91 |
+
"""Test export respects filter parameters."""
|
| 92 |
+
# Test with protocol filter
|
| 93 |
+
response = client.get("/api/v1/proxies/export?format=json&protocol=http&limit=5")
|
| 94 |
+
assert response.status_code == 200
|
| 95 |
+
|
| 96 |
+
data = response.json()
|
| 97 |
+
if len(data) > 0:
|
| 98 |
+
# All proxies should be HTTP
|
| 99 |
+
for proxy in data:
|
| 100 |
+
assert proxy.get("protocol") == "http"
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def test_export_with_quality_filter():
|
| 104 |
+
"""Test export respects quality filter."""
|
| 105 |
+
response = client.get("/api/v1/proxies/export?format=json&min_quality=80&limit=5")
|
| 106 |
+
assert response.status_code == 200
|
| 107 |
+
|
| 108 |
+
data = response.json()
|
| 109 |
+
if len(data) > 0:
|
| 110 |
+
# All proxies should have quality >= 80
|
| 111 |
+
for proxy in data:
|
| 112 |
+
score = proxy.get("quality_score")
|
| 113 |
+
if score is not None:
|
| 114 |
+
assert score >= 80
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def test_export_limit_parameter():
|
| 118 |
+
"""Test that limit parameter works correctly."""
|
| 119 |
+
# Request exactly 5 proxies
|
| 120 |
+
response = client.get("/api/v1/proxies/export?format=json&limit=5")
|
| 121 |
+
assert response.status_code == 200
|
| 122 |
+
|
| 123 |
+
data = response.json()
|
| 124 |
+
# Should return at most 5 proxies
|
| 125 |
+
assert len(data) <= 5
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def test_export_invalid_format():
|
| 129 |
+
"""Test that invalid format returns error."""
|
| 130 |
+
response = client.get("/api/v1/proxies/export?format=invalid")
|
| 131 |
+
|
| 132 |
+
# Should return error or default to safe format
|
| 133 |
+
assert response.status_code in [200, 400]
|
| 134 |
+
|
| 135 |
+
if response.status_code == 200:
|
| 136 |
+
# If it returns 200, should contain error message
|
| 137 |
+
content = response.json()
|
| 138 |
+
assert "error" in content or isinstance(content, dict)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def test_export_empty_result():
|
| 142 |
+
"""Test export with filters that return no results."""
|
| 143 |
+
# Filter for a country code that likely doesn't exist
|
| 144 |
+
response = client.get("/api/v1/proxies/export?format=json&country_code=XX&limit=10")
|
| 145 |
+
assert response.status_code == 200
|
| 146 |
+
|
| 147 |
+
# Should return empty array for JSON
|
| 148 |
+
data = response.json()
|
| 149 |
+
assert isinstance(data, list)
|
| 150 |
+
# May or may not be empty depending on data
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def test_pac_format_http_only():
|
| 154 |
+
"""Test PAC format only includes HTTP/HTTPS proxies."""
|
| 155 |
+
response = client.get("/api/v1/proxies/export?format=pac")
|
| 156 |
+
assert response.status_code == 200
|
| 157 |
+
|
| 158 |
+
content = response.text
|
| 159 |
+
|
| 160 |
+
# PAC file should be generated even if no HTTP proxies
|
| 161 |
+
assert "function FindProxyForURL" in content
|
| 162 |
+
|
| 163 |
+
# Should not include non-HTTP protocols in PROXY directives
|
| 164 |
+
# (PAC files don't support SOCKS5, VMess, etc.)
|
| 165 |
+
if "PROXY" in content:
|
| 166 |
+
# If there are proxy directives, they should be IP:PORT format
|
| 167 |
+
lines = content.split("\n")
|
| 168 |
+
for line in lines:
|
| 169 |
+
if "PROXY " in line:
|
| 170 |
+
# Should not contain protocol prefixes like socks5://
|
| 171 |
+
assert "socks5://" not in line.lower()
|
| 172 |
+
assert "vmess://" not in line.lower()
|