-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pdf_endpoints.py
More file actions
293 lines (245 loc) · 9 KB
/
Copy pathtest_pdf_endpoints.py
File metadata and controls
293 lines (245 loc) · 9 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
"""
Test script for PDF conversion endpoints
"""
import requests
import json
import os
from typing import Dict, Any
# Test configuration
BASE_URL = "http://localhost:8005"
TEST_MARKDOWN = """
# Test Document
This is a **test document** with some formatting.
## Features
- Simple list items
- *Italic text*
- `Code snippets`
### Table Example
| Column 1 | Column 2 |
|----------|----------|
| Data 1 | Data 2 |
| Data 3 | Data 4 |
### Code Block
```python
def hello_world():
print("Hello, World!")
```
> This is a blockquote
## Complex Features (should trigger pandoc)
This document contains math: $E = mc^2$
And footnotes[^1].
[^1]: This is a footnote.
"""
TEST_HTML = """
<!DOCTYPE html>
<html>
<head>
<title>Test Document</title>
</head>
<body>
<h1>Test HTML Document</h1>
<p>This is a <strong>test HTML document</strong> with some formatting.</p>
<h2>Features</h2>
<ul>
<li>Simple list items</li>
<li><em>Italic text</em></li>
<li><code>Code snippets</code></li>
</ul>
<h3>Table Example</h3>
<table>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
<tr>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</table>
<blockquote>
This is a blockquote
</blockquote>
</body>
</html>
"""
def test_pdf_conversion_endpoint():
"""Test the /convert-to-pdf endpoint"""
print("Testing /convert-to-pdf endpoint...")
# Test 1: Simple Markdown with auto priority
print("\n1. Testing simple markdown with auto priority...")
response = requests.post(f"{BASE_URL}/convert-to-pdf", json={
"content": "# Simple Test\n\nThis is a simple markdown document.",
"content_type": "markdown",
"priority": "auto"
})
if response.status_code == 200:
print("✓ Simple markdown conversion successful")
print(f" - Converter used: {response.headers.get('X-Converter-Used', 'Unknown')}")
print(f" - Processing time: {response.headers.get('X-Processing-Time', 'Unknown')}s")
print(f" - PDF size: {response.headers.get('X-PDF-Size', 'Unknown')} bytes")
print(f" - Complexity detected: {response.headers.get('X-Complexity-Detected', 'Unknown')}")
# Save PDF
with open("test_simple.pdf", "wb") as f:
f.write(response.content)
else:
print(f"✗ Simple markdown conversion failed: {response.status_code}")
print(f" Error: {response.text}")
# Test 2: Complex Markdown with auto priority
print("\n2. Testing complex markdown with auto priority...")
response = requests.post(f"{BASE_URL}/convert-to-pdf", json={
"content": TEST_MARKDOWN,
"content_type": "markdown",
"priority": "auto"
})
if response.status_code == 200:
print("✓ Complex markdown conversion successful")
print(f" - Converter used: {response.headers.get('X-Converter-Used', 'Unknown')}")
print(f" - Processing time: {response.headers.get('X-Processing-Time', 'Unknown')}s")
print(f" - PDF size: {response.headers.get('X-PDF-Size', 'Unknown')} bytes")
print(f" - Complexity detected: {response.headers.get('X-Complexity-Detected', 'Unknown')}")
# Save PDF
with open("test_complex.pdf", "wb") as f:
f.write(response.content)
else:
print(f"✗ Complex markdown conversion failed: {response.status_code}")
print(f" Error: {response.text}")
# Test 3: HTML with speed priority
print("\n3. Testing HTML with speed priority...")
response = requests.post(f"{BASE_URL}/convert-to-pdf", json={
"content": TEST_HTML,
"content_type": "html",
"priority": "speed"
})
if response.status_code == 200:
print("✓ HTML conversion successful")
print(f" - Converter used: {response.headers.get('X-Converter-Used', 'Unknown')}")
print(f" - Processing time: {response.headers.get('X-Processing-Time', 'Unknown')}s")
print(f" - PDF size: {response.headers.get('X-PDF-Size', 'Unknown')} bytes")
print(f" - Complexity detected: {response.headers.get('X-Complexity-Detected', 'Unknown')}")
# Save PDF
with open("test_html.pdf", "wb") as f:
f.write(response.content)
else:
print(f"✗ HTML conversion failed: {response.status_code}")
print(f" Error: {response.text}")
# Test 4: Markdown with quality priority
print("\n4. Testing markdown with quality priority...")
response = requests.post(f"{BASE_URL}/convert-to-pdf", json={
"content": TEST_MARKDOWN,
"content_type": "markdown",
"priority": "quality"
})
if response.status_code == 200:
print("✓ Quality markdown conversion successful")
print(f" - Converter used: {response.headers.get('X-Converter-Used', 'Unknown')}")
print(f" - Processing time: {response.headers.get('X-Processing-Time', 'Unknown')}s")
print(f" - PDF size: {response.headers.get('X-PDF-Size', 'Unknown')} bytes")
print(f" - Complexity detected: {response.headers.get('X-Complexity-Detected', 'Unknown')}")
# Save PDF
with open("test_quality.pdf", "wb") as f:
f.write(response.content)
else:
print(f"✗ Quality markdown conversion failed: {response.status_code}")
print(f" Error: {response.text}")
# Test 5: With custom CSS
print("\n5. Testing with custom CSS...")
custom_css = """
body {
font-family: 'Arial', sans-serif;
color: #333;
background-color: #f9f9f9;
}
h1 {
color: #2c3e50;
border-bottom: 2px solid #3498db;
padding-bottom: 10px;
}
"""
response = requests.post(f"{BASE_URL}/convert-to-pdf", json={
"content": "# Custom Styled Document\n\nThis document has custom CSS styling.",
"content_type": "markdown",
"priority": "auto",
"css_content": custom_css
})
if response.status_code == 200:
print("✓ Custom CSS conversion successful")
print(f" - Converter used: {response.headers.get('X-Converter-Used', 'Unknown')}")
print(f" - Processing time: {response.headers.get('X-Processing-Time', 'Unknown')}s")
print(f" - PDF size: {response.headers.get('X-PDF-Size', 'Unknown')} bytes")
# Save PDF
with open("test_custom_css.pdf", "wb") as f:
f.write(response.content)
else:
print(f"✗ Custom CSS conversion failed: {response.status_code}")
print(f" Error: {response.text}")
def test_error_handling():
"""Test error handling"""
print("\n\nTesting error handling...")
# Test 1: Empty content
print("\n1. Testing empty content...")
response = requests.post(f"{BASE_URL}/convert-to-pdf", json={
"content": "",
"content_type": "markdown",
"priority": "auto"
})
if response.status_code == 400:
print("✓ Empty content properly rejected")
else:
print(f"✗ Empty content not handled properly: {response.status_code}")
# Test 2: Invalid content type
print("\n2. Testing invalid content type...")
response = requests.post(f"{BASE_URL}/convert-to-pdf", json={
"content": "Test content",
"content_type": "invalid",
"priority": "auto"
})
if response.status_code == 422:
print("✓ Invalid content type properly rejected")
else:
print(f"✗ Invalid content type not handled properly: {response.status_code}")
def check_server_status():
"""Check if the server is running"""
try:
response = requests.get(f"{BASE_URL}/health", timeout=5)
if response.status_code == 200:
print("✓ Server is running")
return True
else:
print(f"✗ Server health check failed: {response.status_code}")
return False
except requests.exceptions.RequestException as e:
print(f"✗ Cannot connect to server: {e}")
return False
def main():
print("PDF Conversion API Test Suite")
print("=" * 50)
# Check if server is running
if not check_server_status():
print("\nPlease start the server first:")
print(" python -m uvicorn app.main:app --reload")
return
# Run tests
test_pdf_conversion_endpoint()
test_error_handling()
print("\n" + "=" * 50)
print("Test completed!")
print("\nGenerated test files:")
test_files = [
"test_simple.pdf",
"test_complex.pdf",
"test_html.pdf",
"test_quality.pdf",
"test_custom_css.pdf"
]
for file in test_files:
if os.path.exists(file):
size = os.path.getsize(file)
print(f" - {file} ({size} bytes)")
print("\nYou can open these PDF files to verify the conversion results.")
if __name__ == "__main__":
main()