File size: 4,960 Bytes
623e14e |
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 |
#!/usr/bin/env python3
"""
Test script for the Enhanced DOCX to PDF Converter API
"""
import requests
import base64
import json
import os
# API endpoint
BASE_URL = "http://localhost:8000"
def test_health():
"""Test health endpoint"""
print("Testing health endpoint...")
try:
response = requests.get(f"{BASE_URL}/health")
if response.status_code == 200:
print("✓ Health check passed")
print(f" Version: {response.json().get('version')}")
else:
print("✗ Health check failed")
except Exception as e:
print(f"✗ Health check error: {e}")
def test_convert_file(docx_path):
"""Test file conversion"""
print(f"\nTesting file conversion with {docx_path}...")
if not os.path.exists(docx_path):
print(f"✗ File {docx_path} not found")
return
try:
with open(docx_path, 'rb') as f:
files = {'file': (os.path.basename(docx_path), f, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')}
response = requests.post(f"{BASE_URL}/convert", files=files)
if response.status_code == 200:
result = response.json()
if result.get('success'):
print("✓ File conversion successful")
print(f" PDF URL: {result.get('pdf_url')}")
else:
print(f"✗ Conversion failed: {result.get('error')}")
else:
print(f"✗ Conversion failed with status {response.status_code}")
print(response.text)
except Exception as e:
print(f"✗ Conversion error: {e}")
def test_convert_base64(docx_path):
"""Test base64 conversion"""
print(f"\nTesting base64 conversion with {docx_path}...")
if not os.path.exists(docx_path):
print(f"✗ File {docx_path} not found")
return
try:
with open(docx_path, 'rb') as f:
file_content = base64.b64encode(f.read()).decode('utf-8')
data = {
'file_content': file_content,
'filename': os.path.basename(docx_path)
}
response = requests.post(f"{BASE_URL}/convert", data=data)
if response.status_code == 200:
result = response.json()
if result.get('success'):
print("✓ Base64 conversion successful")
print(f" PDF URL: {result.get('pdf_url')}")
else:
print(f"✗ Conversion failed: {result.get('error')}")
else:
print(f"✗ Conversion failed with status {response.status_code}")
print(response.text)
except Exception as e:
print(f"✗ Conversion error: {e}")
def test_batch_convert(docx_paths):
"""Test batch conversion"""
print(f"\nTesting batch conversion with {len(docx_paths)} files...")
files_data = []
for path in docx_paths:
if not os.path.exists(path):
print(f"✗ File {path} not found")
continue
try:
with open(path, 'rb') as f:
file_content = base64.b64encode(f.read()).decode('utf-8')
files_data.append({
'file_content': file_content,
'filename': os.path.basename(path)
})
except Exception as e:
print(f"✗ Error reading {path}: {e}")
if not files_data:
print("✗ No valid files to convert")
return
try:
payload = {'files': files_data}
response = requests.post(f"{BASE_URL}/convert/batch", json=payload)
if response.status_code == 200:
results = response.json()
success_count = sum(1 for r in results if r.get('success'))
print(f"✓ Batch conversion completed: {success_count}/{len(results)} successful")
for i, result in enumerate(results):
if result.get('success'):
print(f" File {i+1}: Success - {result.get('pdf_url')}")
else:
print(f" File {i+1}: Failed - {result.get('error')}")
else:
print(f"✗ Batch conversion failed with status {response.status_code}")
print(response.text)
except Exception as e:
print(f"✗ Batch conversion error: {e}")
if __name__ == "__main__":
print("Enhanced DOCX to PDF Converter API Test Script")
print("=" * 50)
# Test health endpoint
test_health()
# Test with template.docx if available
template_path = "template.docx"
if os.path.exists(template_path):
test_convert_file(template_path)
test_convert_base64(template_path)
test_batch_convert([template_path, template_path]) # Test with same file twice
else:
print(f"\nNote: {template_path} not found, skipping file tests")
print("\nTest script completed.") |