File size: 5,744 Bytes
bbc4356 | 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | """
Integration tests for proxy export formats.
Tests all 4 export formats: txt, json, csv, pac
"""
import pytest
from fastapi.testclient import TestClient
from app.main import app
import json
client = TestClient(app)
def test_export_txt_format():
"""Test TXT format export returns plain text list of proxy URLs."""
response = client.get("/api/v1/proxies/export?format=txt&limit=10")
assert response.status_code == 200
assert response.headers["content-type"] == "text/plain; charset=utf-8"
# Content should be newline-separated URLs
content = response.text
if content.strip(): # If there are proxies
lines = content.strip().split("\n")
for line in lines:
# Each line should look like a URL
assert "://" in line or ":" in line # Either full URL or IP:port
def test_export_json_format():
"""Test JSON format export returns structured JSON data."""
response = client.get("/api/v1/proxies/export?format=json&limit=10")
assert response.status_code == 200
assert "application/json" in response.headers["content-type"]
# Should be valid JSON
data = response.json()
assert isinstance(data, list)
# If there are proxies, check structure
if len(data) > 0:
proxy = data[0]
assert "url" in proxy
assert "protocol" in proxy
# Optional fields
assert "country" in proxy or True
assert "quality_score" in proxy or True
def test_export_csv_format():
"""Test CSV format export returns CSV with headers."""
response = client.get("/api/v1/proxies/export?format=csv&limit=10")
assert response.status_code == 200
assert response.headers["content-type"] == "text/csv; charset=utf-8"
assert "attachment" in response.headers.get("content-disposition", "")
# Content should have CSV headers
content = response.text
lines = content.strip().split("\n")
if len(lines) > 0:
# First line should be headers
headers = lines[0]
assert "URL" in headers
assert "Protocol" in headers
assert "Country" in headers
assert "Quality" in headers
def test_export_pac_format():
"""Test PAC format export returns proxy auto-config file."""
response = client.get("/api/v1/proxies/export?format=pac&limit=10")
assert response.status_code == 200
assert response.headers["content-type"] == "application/x-ns-proxy-autoconfig"
assert "attachment" in response.headers.get("content-disposition", "")
assert "1proxy.pac" in response.headers.get("content-disposition", "")
# Content should be a valid PAC file
content = response.text
assert "function FindProxyForURL" in content
assert "return" in content
# Should have localhost bypass rules
assert "localhost" in content.lower() or "127.0.0.1" in content
def test_export_with_filters():
"""Test export respects filter parameters."""
# Test with protocol filter
response = client.get("/api/v1/proxies/export?format=json&protocol=http&limit=5")
assert response.status_code == 200
data = response.json()
if len(data) > 0:
# All proxies should be HTTP
for proxy in data:
assert proxy.get("protocol") == "http"
def test_export_with_quality_filter():
"""Test export respects quality filter."""
response = client.get("/api/v1/proxies/export?format=json&min_quality=80&limit=5")
assert response.status_code == 200
data = response.json()
if len(data) > 0:
# All proxies should have quality >= 80
for proxy in data:
score = proxy.get("quality_score")
if score is not None:
assert score >= 80
def test_export_limit_parameter():
"""Test that limit parameter works correctly."""
# Request exactly 5 proxies
response = client.get("/api/v1/proxies/export?format=json&limit=5")
assert response.status_code == 200
data = response.json()
# Should return at most 5 proxies
assert len(data) <= 5
def test_export_invalid_format():
"""Test that invalid format returns error."""
response = client.get("/api/v1/proxies/export?format=invalid")
# Should return error or default to safe format
assert response.status_code in [200, 400]
if response.status_code == 200:
# If it returns 200, should contain error message
content = response.json()
assert "error" in content or isinstance(content, dict)
def test_export_empty_result():
"""Test export with filters that return no results."""
# Filter for a country code that likely doesn't exist
response = client.get("/api/v1/proxies/export?format=json&country_code=XX&limit=10")
assert response.status_code == 200
# Should return empty array for JSON
data = response.json()
assert isinstance(data, list)
# May or may not be empty depending on data
def test_pac_format_http_only():
"""Test PAC format only includes HTTP/HTTPS proxies."""
response = client.get("/api/v1/proxies/export?format=pac")
assert response.status_code == 200
content = response.text
# PAC file should be generated even if no HTTP proxies
assert "function FindProxyForURL" in content
# Should not include non-HTTP protocols in PROXY directives
# (PAC files don't support SOCKS5, VMess, etc.)
if "PROXY" in content:
# If there are proxy directives, they should be IP:PORT format
lines = content.split("\n")
for line in lines:
if "PROXY " in line:
# Should not contain protocol prefixes like socks5://
assert "socks5://" not in line.lower()
assert "vmess://" not in line.lower()
|