| """ |
| 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 = response.text |
| if content.strip(): |
| lines = content.strip().split("\n") |
| for line in lines: |
| |
| assert "://" in line or ":" in line |
|
|
|
|
| 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"] |
|
|
| |
| data = response.json() |
| assert isinstance(data, list) |
|
|
| |
| if len(data) > 0: |
| proxy = data[0] |
| assert "url" in proxy |
| assert "protocol" in proxy |
| |
| 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 = response.text |
| lines = content.strip().split("\n") |
|
|
| if len(lines) > 0: |
| |
| 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 = response.text |
| assert "function FindProxyForURL" in content |
| assert "return" in content |
|
|
| |
| assert "localhost" in content.lower() or "127.0.0.1" in content |
|
|
|
|
| def test_export_with_filters(): |
| """Test export respects filter parameters.""" |
| |
| 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: |
| |
| 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: |
| |
| 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.""" |
| |
| response = client.get("/api/v1/proxies/export?format=json&limit=5") |
| assert response.status_code == 200 |
|
|
| data = response.json() |
| |
| assert len(data) <= 5 |
|
|
|
|
| def test_export_invalid_format(): |
| """Test that invalid format returns error.""" |
| response = client.get("/api/v1/proxies/export?format=invalid") |
|
|
| |
| assert response.status_code in [200, 400] |
|
|
| if response.status_code == 200: |
| |
| content = response.json() |
| assert "error" in content or isinstance(content, dict) |
|
|
|
|
| def test_export_empty_result(): |
| """Test export with filters that return no results.""" |
| |
| response = client.get("/api/v1/proxies/export?format=json&country_code=XX&limit=10") |
| assert response.status_code == 200 |
|
|
| |
| data = response.json() |
| assert isinstance(data, list) |
| |
|
|
|
|
| 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 |
|
|
| |
| assert "function FindProxyForURL" in content |
|
|
| |
| |
| if "PROXY" in content: |
| |
| lines = content.split("\n") |
| for line in lines: |
| if "PROXY " in line: |
| |
| assert "socks5://" not in line.lower() |
| assert "vmess://" not in line.lower() |
|
|