-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api.html
More file actions
267 lines (236 loc) Β· 10.2 KB
/
test-api.html
File metadata and controls
267 lines (236 loc) Β· 10.2 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DataGuard API Test</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
margin-bottom: 30px;
}
.test-section {
margin-bottom: 30px;
padding: 20px;
border: 1px solid #e0e0e0;
border-radius: 8px;
background: #fafafa;
}
.test-section h3 {
margin-top: 0;
color: #555;
}
button {
background: #007bff;
color: white;
border: none;
padding: 12px 24px;
border-radius: 6px;
cursor: pointer;
font-size: 14px;
margin: 5px;
transition: background 0.2s;
}
button:hover {
background: #0056b3;
}
button:disabled {
background: #6c757d;
cursor: not-allowed;
}
.result {
margin-top: 15px;
padding: 15px;
background: white;
border-radius: 6px;
border-left: 4px solid #28a745;
font-family: monospace;
font-size: 12px;
white-space: pre-wrap;
max-height: 300px;
overflow-y: auto;
}
.error {
border-left-color: #dc3545;
}
.info {
background: #e7f3ff;
padding: 15px;
border-radius: 6px;
margin-bottom: 20px;
border-left: 4px solid #007bff;
}
.status {
padding: 10px;
border-radius: 6px;
margin-bottom: 15px;
font-weight: bold;
}
.status.success {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status.error {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
</head>
<body>
<div class="container">
<h1>π‘οΈ DataGuard API Test</h1>
<div class="info">
<strong>Instructions:</strong><br>
1. Install the DataGuard extension in your browser<br>
2. Click the buttons below to test API requests<br>
3. The extension should intercept these requests and ask for your approval<br>
4. Check the extension popup to see the filtering results
</div>
<div class="test-section">
<h3>π§ Email Data API Tests</h3>
<p>These buttons will make requests to the mail-demo service, which should be intercepted by DataGuard:</p>
<button onclick="testAPI('subscription')">Test Subscription Emails</button>
<button onclick="testAPI('delivery')">Test Delivery Emails</button>
<button onclick="testAPI('purchase')">Test Purchase Emails</button>
<button onclick="testAPI('unread')">Test Unread Emails</button>
<button onclick="testAPI('all')">Test All Emails</button>
<div id="status"></div>
<div id="result"></div>
</div>
<div class="test-section">
<h3>π Direct API Tests (Bypass Extension)</h3>
<p>These tests directly call the API without going through the extension:</p>
<button onclick="testDirectAPI('subscription')">Direct: Subscription</button>
<button onclick="testDirectAPI('delivery')">Direct: Delivery</button>
<button onclick="testDirectAPI('purchase')">Direct: Purchase</button>
<div id="directStatus"></div>
<div id="directResult"></div>
</div>
</div>
<script>
const API_BASE = 'http://localhost:3000/api';
function showStatus(message, isError = false, targetId = 'status') {
console.log(`[DEBUG] showStatus called:`, { message, isError, targetId });
const statusEl = document.getElementById(targetId);
statusEl.innerHTML = `<div class="status ${isError ? 'error' : 'success'}">${message}</div>`;
}
function showResult(data, targetId = 'result') {
console.log(`[DEBUG] showResult called:`, { data, targetId });
const resultEl = document.getElementById(targetId);
resultEl.innerHTML = `<div class="result">${JSON.stringify(data, null, 2)}</div>`;
}
async function testAPI(type) {
console.log(`[DEBUG] testAPI called with type: ${type}`);
console.log(`[DEBUG] API_BASE: ${API_BASE}`);
showStatus(`Testing ${type} emails...`, false);
try {
const url = type === 'all' ? `${API_BASE}/emails` : `${API_BASE}/emails?type=${type}`;
console.log(`[DEBUG] Making request to URL: ${url}`);
const response = await fetch(url);
console.log(`[DEBUG] Response received:`, {
status: response.status,
statusText: response.statusText,
ok: response.ok,
headers: Object.fromEntries(response.headers.entries())
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
console.log(`[DEBUG] Response data:`, data);
console.log(`[DEBUG] Data length: ${data.length}`);
showStatus(`β
Successfully retrieved ${data.length} ${type} emails`, false);
showResult(data);
} catch (error) {
console.error(`[DEBUG] Error in testAPI:`, error);
console.error(`[DEBUG] Error stack:`, error.stack);
showStatus(`β Error: ${error.message}`, true);
showResult({ error: error.message });
}
}
async function testDirectAPI(type) {
console.log(`[DEBUG] testDirectAPI called with type: ${type}`);
console.log(`[DEBUG] API_BASE: ${API_BASE}`);
showStatus(`Testing direct API call for ${type}...`, false, 'directStatus');
try {
// Use a different approach to bypass potential extension interception
const url = `${API_BASE}/emails?type=${type}`;
console.log(`[DEBUG] Making direct request to URL: ${url}`);
const requestOptions = {
method: 'GET',
headers: {
'X-Direct-Call': 'true',
'Content-Type': 'application/json'
}
};
console.log(`[DEBUG] Request options:`, requestOptions);
// Create a new request with different headers
const response = await fetch(url, requestOptions);
console.log(`[DEBUG] Direct response received:`, {
status: response.status,
statusText: response.statusText,
ok: response.ok,
headers: Object.fromEntries(response.headers.entries())
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
console.log(`[DEBUG] Direct response data:`, data);
console.log(`[DEBUG] Direct data length: ${data.length}`);
showStatus(`β
Direct call successful: ${data.length} ${type} emails`, false, 'directStatus');
showResult(data, 'directResult');
} catch (error) {
console.error(`[DEBUG] Error in testDirectAPI:`, error);
console.error(`[DEBUG] Direct call error stack:`, error.stack);
showStatus(`β Direct call error: ${error.message}`, true, 'directStatus');
showResult({ error: error.message }, 'directResult');
}
}
// Test if the mail-demo service is running
async function checkServiceStatus() {
console.log(`[DEBUG] checkServiceStatus called`);
console.log(`[DEBUG] Checking service at: ${API_BASE}/emails`);
try {
const response = await fetch(`${API_BASE}/emails`);
console.log(`[DEBUG] Service status response:`, {
status: response.status,
statusText: response.statusText,
ok: response.ok,
headers: Object.fromEntries(response.headers.entries())
});
if (response.ok) {
console.log('β
Mail-demo service is running');
} else {
console.log('β οΈ Mail-demo service responded with error:', response.status);
}
} catch (error) {
console.error(`[DEBUG] Service check error:`, error);
console.error(`[DEBUG] Service check error stack:`, error.stack);
console.log('β Mail-demo service is not accessible:', error.message);
showStatus('β Mail-demo service is not running. Please start it first.', true);
}
}
// Check service status on page load
console.log(`[DEBUG] Page loaded, initializing...`);
console.log(`[DEBUG] API_BASE configured as: ${API_BASE}`);
checkServiceStatus();
</script>
</body>
</html>