fix: resolve JSON.parse error in frontend API calls
- Add error handling for JSON encoding in all backend handlers - Add CORS support to backend server - Add proxy configuration for frontend development server - Improve error handling in frontend API calls - Build frontend to create static files for backend serving - Add comprehensive error messages and user feedback Fixes issue where frontend received malformed JSON responses from backend API.
This commit is contained in:
82
backend/test_api.sh
Executable file
82
backend/test_api.sh
Executable file
@@ -0,0 +1,82 @@
|
||||
#!/nix/store/ypgcdmzzlgnrmdcsq72c3dxz651jg9zc-bash-5.3p3/bin/bash
|
||||
|
||||
# Create test data directory
|
||||
mkdir -p test/data
|
||||
|
||||
# Start the server in the background
|
||||
./markdown-editor --data-dir test/data --port 8080 --host 127.0.0.1 > /tmp/server.log 2>&1 &
|
||||
SERVER_PID=$!
|
||||
|
||||
# Wait for server to start
|
||||
echo "Waiting for server to start..."
|
||||
sleep 3
|
||||
|
||||
# Check if server is running
|
||||
if ! ps -p $SERVER_PID > /dev/null; then
|
||||
echo "Server failed to start"
|
||||
cat /tmp/server.log
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Server is running (PID: $SERVER_PID)"
|
||||
echo "Testing API endpoints..."
|
||||
echo ""
|
||||
|
||||
# Test 1: GET /api/files (empty)
|
||||
echo "Test 1: GET /api/files (no files)"
|
||||
response=$(curl -s http://127.0.0.1:8080/api/files)
|
||||
echo "Response: $response"
|
||||
if echo "$response" | grep -q '"files"'; then
|
||||
echo "✓ Response contains 'files' key"
|
||||
else
|
||||
echo "✗ Response does not contain 'files' key"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
# Test 2: Create a file
|
||||
echo "Test 2: POST /api/files (create test.md)"
|
||||
response=$(curl -s -X POST http://127.0.0.1:8080/api/files \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"filename":"test.md","content":"# Test Content"}')
|
||||
echo "Response: $response"
|
||||
if echo "$response" | grep -q '"message"'; then
|
||||
echo "✓ Response contains 'message' key"
|
||||
else
|
||||
echo "✗ Response does not contain 'message' key"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
# Test 3: GET /api/files (with file)
|
||||
echo "Test 3: GET /api/files (with file)"
|
||||
response=$(curl -s http://127.0.0.1:8080/api/files)
|
||||
echo "Response: $response"
|
||||
if echo "$response" | grep -q '"files"'; then
|
||||
echo "✓ Response contains 'files' key"
|
||||
if echo "$response" | grep -q '"test.md"'; then
|
||||
echo "✓ Response contains 'test.md'"
|
||||
else
|
||||
echo "✗ Response does not contain 'test.md'"
|
||||
fi
|
||||
else
|
||||
echo "✗ Response does not contain 'files' key"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
# Test 4: GET /api/files/test.md
|
||||
echo "Test 4: GET /api/files/test.md"
|
||||
response=$(curl -s http://127.0.0.1:8080/api/files/test.md)
|
||||
echo "Response: $response"
|
||||
if echo "$response" | grep -q '"content"'; then
|
||||
echo "✓ Response contains 'content' key"
|
||||
else
|
||||
echo "✗ Response does not contain 'content' key"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
# Kill the server
|
||||
echo "Stopping server..."
|
||||
kill $SERVER_PID
|
||||
wait $SERVER_PID 2>/dev/null
|
||||
|
||||
echo ""
|
||||
echo "All tests completed!"
|
||||
Reference in New Issue
Block a user