cf / test_api.py
Hiro121234's picture
Upload 12 files
1f82855 verified
#!/usr/bin/env python3
"""
Test script for Turnstile Solver API
Usage: python test_api.py <API_URL>
Example: python test_api.py https://your-space.hf.space
"""
import sys
import time
import requests
def test_api(base_url):
"""Test the Turnstile Solver API"""
print("="*60)
print("Turnstile Solver API Test")
print("="*60)
print(f"\nAPI URL: {base_url}")
# Test 1: Health check
print("\n[1/4] Testing health endpoint...")
try:
response = requests.get(f"{base_url}/health", timeout=10)
if response.status_code == 200:
print("βœ… Health check passed")
print(f" Response: {response.json()}")
else:
print(f"❌ Health check failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Health check error: {e}")
return False
# Test 2: Create solve task
print("\n[2/4] Creating solve task...")
try:
response = requests.get(
f"{base_url}/turnstile",
params={
"url": "https://createvision.ai",
"sitekey": "0x4AAAAAAB6qwG1HcvybuFht"
},
timeout=10
)
if response.status_code == 202:
task_id = response.json()["task_id"]
print(f"βœ… Task created: {task_id}")
else:
print(f"❌ Task creation failed: {response.status_code}")
print(f" Response: {response.text}")
return False
except Exception as e:
print(f"❌ Task creation error: {e}")
return False
# Test 3: Wait for solution
print("\n[3/4] Waiting for solution...")
max_attempts = 20
for attempt in range(max_attempts):
try:
time.sleep(1)
response = requests.get(
f"{base_url}/result",
params={"id": task_id},
timeout=10
)
if response.status_code == 200:
if response.text == "CAPTCHA_NOT_READY":
print(f" Attempt {attempt + 1}/{max_attempts}: Still solving...")
continue
result = response.json()
if result.get("value") == "CAPTCHA_FAIL":
print(f"❌ Solve failed: {result}")
return False
# Success!
print(f"βœ… Solved in {result['elapsed_time']} seconds!")
print(f" Token: {result['value'][:50]}...")
break
else:
print(f"❌ Result check failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Result check error: {e}")
return False
else:
print("❌ Timeout waiting for solution")
return False
# Test 4: Verify token format
print("\n[4/4] Verifying token format...")
token = result['value']
if token.startswith("0.") and len(token) > 100:
print("βœ… Token format is valid")
else:
print("⚠️ Token format looks unusual")
print("\n" + "="*60)
print("βœ… All tests passed!")
print("="*60)
print(f"\nYour API is working correctly at: {base_url}")
print("\nExample usage:")
print(f' curl "{base_url}/turnstile?url=https://example.com&sitekey=0x4AAA..."')
return True
def main():
if len(sys.argv) < 2:
print("Usage: python test_api.py <API_URL>")
print("Example: python test_api.py https://your-space.hf.space")
print("\nOr test locally:")
print(" python test_api.py http://localhost:7860")
sys.exit(1)
api_url = sys.argv[1].rstrip('/')
success = test_api(api_url)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()