File size: 3,604 Bytes
510ced6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API Test</title>
    <style>
        body { font-family: Arial; padding: 20px; background: #1a1a1a; color: #fff; }
        button { padding: 10px 20px; margin: 10px; background: #c8f04e; border: none; cursor: pointer; }
        pre { background: #2a2a2a; padding: 15px; border-radius: 8px; overflow-x: auto; }
        .success { color: #4ade80; }
        .error { color: #ff4d4d; }
    </style>
</head>
<body>
    <h1>πŸ§ͺ API Connection Test</h1>
    
    <div>
        <h3>Current URL Info:</h3>
        <pre id="urlInfo"></pre>
    </div>

    <button onclick="testHealth()">Test Health Endpoint</button>
    <button onclick="testRegister()">Test Register</button>
    <button onclick="testLogin()">Test Login</button>
    
    <div>
        <h3>Results:</h3>
        <pre id="results"></pre>
    </div>

    <script>
        const API = window.location.origin + '/api';
        
        document.getElementById('urlInfo').textContent = 
            'Origin: ' + window.location.origin + '\n' +
            'API URL: ' + API + '\n' +
            'Full URL: ' + window.location.href;

        function log(msg, isError = false) {
            const el = document.getElementById('results');
            const className = isError ? 'error' : 'success';
            el.innerHTML += `<div class="${className}">${new Date().toLocaleTimeString()}: ${msg}</div>`;
        }

        async function testHealth() {
            log('Testing /health endpoint...');
            try {
                const res = await fetch(API.replace('/api', '/health'));
                const data = await res.json();
                log('βœ… Health check: ' + JSON.stringify(data, null, 2));
            } catch (err) {
                log('❌ Health check failed: ' + err.message, true);
            }
        }

        async function testRegister() {
            log('Testing /api/users/register...');
            const testEmail = 'test' + Date.now() + '@test.com';
            try {
                const res = await fetch(API + '/users/register', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ 
                        email: testEmail, 
                        password: 'test123456' 
                    })
                });
                const data = await res.json();
                log('βœ… Register response: ' + JSON.stringify(data, null, 2));
            } catch (err) {
                log('❌ Register failed: ' + err.message, true);
            }
        }

        async function testLogin() {
            log('Testing /api/users/login...');
            try {
                const res = await fetch(API + '/users/login', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ 
                        email: 'admin@moharek.com', 
                        password: 'admin123' 
                    })
                });
                const data = await res.json();
                log('βœ… Login response: ' + JSON.stringify(data, null, 2));
            } catch (err) {
                log('❌ Login failed: ' + err.message, true);
            }
        }

        // Auto-test on load
        setTimeout(() => {
            log('πŸš€ Starting automatic tests...');
            testHealth();
        }, 500);
    </script>
</body>
</html>