-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathtest-http-routes.mjs
More file actions
133 lines (110 loc) · 3.58 KB
/
test-http-routes.mjs
File metadata and controls
133 lines (110 loc) · 3.58 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
#!/usr/bin/env node
/**
* Test script for HTTP routes in convex/http.ts
*
* This tests both Hono routes and traditional Convex HTTP routes
* to verify they can coexist and work properly.
*/
async function testEndpoint(name, url, options = {}) {
console.log(`\n🧪 Testing: ${name}`);
console.log(` URL: ${url}`);
try {
const response = await fetch(url, options);
const contentType = response.headers.get("content-type");
let body;
if (contentType && contentType.includes("application/json")) {
body = await response.json();
} else {
body = await response.text();
}
if (response.ok) {
console.log(` ✅ Status: ${response.status}`);
console.log(` Response:`, JSON.stringify(body, null, 2));
return { success: true, body };
} else {
console.log(` ❌ Status: ${response.status}`);
console.log(` Response:`, body);
return { success: false, status: response.status, body };
}
} catch (error) {
console.log(` ❌ Error: ${error.message}`);
return { success: false, error: error.message };
}
}
async function runTests(baseUrl) {
console.log(`\n${"=".repeat(60)}`);
console.log(`Testing HTTP Routes`);
console.log(`Base URL: ${baseUrl}`);
console.log(`${"=".repeat(60)}`);
const results = [];
// Test root endpoint
results.push(await testEndpoint("Root endpoint (Hono)", `${baseUrl}/`));
console.log(`\n${"─".repeat(60)}`);
console.log(`HONO ROUTES`);
console.log(`${"─".repeat(60)}`);
// Test Hono routes
results.push(await testEndpoint("Hono hello", `${baseUrl}/hono/hello`));
results.push(
await testEndpoint(
"Hono user with ID parameter",
`${baseUrl}/hono/user/123`,
),
);
results.push(
await testEndpoint("Hono echo (POST)", `${baseUrl}/hono/echo`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
message: "test from hono",
timestamp: Date.now(),
}),
}),
);
results.push(
await testEndpoint("Hono with Convex query", `${baseUrl}/hono/with-query`),
);
console.log(`\n${"─".repeat(60)}`);
console.log(`CONVEX HTTP ROUTES`);
console.log(`${"─".repeat(60)}`);
// Test Convex HTTP routes
results.push(await testEndpoint("Convex hello", `${baseUrl}/convex/hello`));
results.push(
await testEndpoint("Convex echo (POST)", `${baseUrl}/convex/misc`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
message: "test from convex",
timestamp: Date.now(),
}),
}),
);
// Summary
console.log(`\n${"=".repeat(60)}`);
console.log(`TEST SUMMARY`);
console.log(`${"=".repeat(60)}`);
const passed = results.filter((r) => r.success).length;
const failed = results.length - passed;
console.log(`\n✅ Passed: ${passed}`);
console.log(`❌ Failed: ${failed}`);
console.log(`📊 Total: ${results.length}`);
if (failed === 0) {
console.log(`\n🎉 All tests passed!`);
} else {
console.log(`\n⚠️ Some tests failed.`);
process.exit(1);
}
}
// Get base URL from environment or argument
const baseUrl = process.env.CONVEX_SITE_URL || process.argv[2];
if (!baseUrl) {
console.error("❌ Error: Please provide a base URL");
console.error("\nUsage:");
console.error(" node test-http-routes.mjs <base-url>");
console.error(" CONVEX_SITE_URL=<base-url> node test-http-routes.mjs");
console.error("\nExample:");
console.error(
" node test-http-routes.mjs https://happy-animal-123.convex.site",
);
process.exit(1);
}
runTests(baseUrl);