File size: 4,066 Bytes
1f82855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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()