salesforce_node_pdf_api/test-api.js
2025-09-03 23:52:13 +05:30

140 lines
4.8 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* Test script for the Property Brochure PDF Generator API
* Run this after starting the server to test the API endpoints
*/
const fs = require('fs');
const path = require('path');
// Test HTML content
const testHTML = fs.readFileSync('test.html', 'utf8');
// Test the API endpoints
async function testAPI() {
console.log('🧪 Testing Property Brochure PDF Generator API...\n');
// Test 1: Root endpoint
console.log('1⃣ Testing GET / endpoint...');
try {
const response = await fetch('http://localhost:8000/');
const data = await response.json();
console.log('✅ Root endpoint working:', data.message);
console.log(' Version:', data.version);
console.log(' Status:', data.status);
} catch (error) {
console.log('❌ Root endpoint failed:', error.message);
}
// Test 2: Health endpoint
console.log('\n2⃣ Testing GET /health endpoint...');
try {
const response = await fetch('http://localhost:8000/health');
const data = await response.json();
console.log('✅ Health endpoint working:', data.status);
console.log(' Service:', data.service);
} catch (error) {
console.log('❌ Health endpoint failed:', error.message);
}
// Test 3: PDF generation
console.log('\n3⃣ Testing POST /generate-pdf endpoint...');
try {
const response = await fetch('http://localhost:8000/generate-pdf', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
input: testHTML,
output: 'test_brochure.pdf'
})
});
if (response.ok) {
const buffer = await response.arrayBuffer();
const outputPath = path.join(__dirname, 'test_brochure.pdf');
fs.writeFileSync(outputPath, Buffer.from(buffer));
console.log('✅ PDF generation successful!');
console.log(' File saved as: test_brochure.pdf');
console.log(' File size:', (buffer.byteLength / 1024).toFixed(2), 'KB');
} else {
const error = await response.json();
console.log('❌ PDF generation failed:', error.error);
}
} catch (error) {
console.log('❌ PDF generation failed:', error.message);
}
// Test 4: Error handling - empty input
console.log('\n4⃣ Testing error handling (empty input)...');
try {
const response = await fetch('http://localhost:8000/generate-pdf', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
input: '',
output: 'error_test.pdf'
})
});
if (response.status === 400) {
const error = await response.json();
console.log('✅ Error handling working:', error.error);
} else {
console.log('❌ Expected error 400, got:', response.status);
}
} catch (error) {
console.log('❌ Error handling test failed:', error.message);
}
// Test 5: Alternative format (Apex compatibility)
console.log('\n5⃣ Testing alternative format (Apex compatibility)...');
try {
const response = await fetch('http://localhost:8000/generate-pdf', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
html_content: testHTML,
filename: 'apex_test.pdf'
})
});
if (response.ok) {
const buffer = await response.arrayBuffer();
const outputPath = path.join(__dirname, 'apex_test.pdf');
fs.writeFileSync(outputPath, Buffer.from(buffer));
console.log('✅ Apex format working!');
console.log(' File saved as: apex_test.pdf');
} else {
const error = await response.json();
console.log('❌ Apex format failed:', error.error);
}
} catch (error) {
console.log('❌ Apex format test failed:', error.message);
}
console.log('\n🎉 API testing completed!');
console.log('\n📁 Generated files:');
if (fs.existsSync('test_brochure.pdf')) {
console.log(' • test_brochure.pdf');
}
if (fs.existsSync('apex_test.pdf')) {
console.log(' • apex_test.pdf');
}
}
// Check if fetch is available (Node.js 18+)
if (typeof fetch === 'undefined') {
console.log('❌ This script requires Node.js 18+ or you need to install node-fetch');
console.log(' Install with: npm install node-fetch');
process.exit(1);
}
// Run tests
testAPI().catch(console.error);