oxidize-pdf-api 1.1.7

REST API for oxidizePdf (Community edition)
Documentation
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! Tests for the API endpoints

#[cfg(test)]
mod tests {
    use super::super::api::*;
    use axum::{
        body::{to_bytes, Body},
        http::{Request, StatusCode},
    };
    use serde_json::json;
    use tower::ServiceExt;

    #[test]
    fn test_sync_simple() {
        // Basic synchronous test to verify tests run
        assert_eq!(2 + 2, 4);
    }

    #[tokio::test]
    async fn test_async_simple() {
        // Basic async test to verify tokio runtime works
        let result = tokio::time::timeout(std::time::Duration::from_secs(1), async { 42 }).await;
        assert_eq!(result.unwrap(), 42);
    }

    #[tokio::test]
    async fn test_health_check() {
        let app = app();

        let response = tokio::time::timeout(
            std::time::Duration::from_secs(5),
            app.oneshot(
                Request::builder()
                    .uri("/api/health")
                    .method("GET")
                    .body(Body::empty())
                    .unwrap(),
            ),
        )
        .await
        .expect("Request timed out")
        .unwrap();

        assert_eq!(response.status(), StatusCode::OK);

        let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();

        assert_eq!(json["status"], "ok");
        assert_eq!(json["service"], "oxidizePdf API");
        assert!(json["version"].is_string());
    }

    #[tokio::test]
    async fn test_create_pdf_simple() {
        let app = app();

        let request_body = json!({
            "text": "Hello, World!"
        });

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/create")
                    .method("POST")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_vec(&request_body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);

        let content_type = response
            .headers()
            .get("content-type")
            .and_then(|v| v.to_str().ok());
        assert_eq!(content_type, Some("application/pdf"));

        let content_disposition = response
            .headers()
            .get("content-disposition")
            .and_then(|v| v.to_str().ok());
        assert_eq!(
            content_disposition,
            Some("attachment; filename=\"generated.pdf\"")
        );

        let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
        assert!(!body.is_empty());
        // PDF files start with %PDF
        assert!(body.starts_with(b"%PDF"));
    }

    #[tokio::test]
    async fn test_create_pdf_with_font_size() {
        let app = app();

        let request_body = json!({
            "text": "Large Text",
            "font_size": 48.0
        });

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/create")
                    .method("POST")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_vec(&request_body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);

        let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
        assert!(!body.is_empty());
        assert!(body.starts_with(b"%PDF"));
    }

    #[tokio::test]
    async fn test_create_pdf_empty_text() {
        let app = app();

        let request_body = json!({
            "text": ""
        });

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/create")
                    .method("POST")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_vec(&request_body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        // Should still succeed with empty text
        assert_eq!(response.status(), StatusCode::OK);

        let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
        assert!(body.starts_with(b"%PDF"));
    }

    #[tokio::test]
    async fn test_create_pdf_long_text() {
        let app = app();

        let long_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ".repeat(50);
        let request_body = json!({
            "text": long_text,
            "font_size": 12.0
        });

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/create")
                    .method("POST")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_vec(&request_body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(
            response.headers().get("content-type").unwrap(),
            "application/pdf"
        );
    }

    #[tokio::test]
    async fn test_create_pdf_unicode_text() {
        let app = app();

        let request_body = json!({
            "text": "Hello 世界 🌍 مرحبا",
            "font_size": 24.0
        });

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/create")
                    .method("POST")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_vec(&request_body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);

        let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
        assert!(body.starts_with(b"%PDF"));
    }

    #[tokio::test]
    async fn test_create_pdf_invalid_json() {
        let app = app();

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/create")
                    .method("POST")
                    .header("content-type", "application/json")
                    .body(Body::from("{invalid json"))
                    .unwrap(),
            )
            .await
            .unwrap();

        // Axum will return 400 for invalid JSON
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test]
    async fn test_create_pdf_missing_text_field() {
        let app = app();

        let request_body = json!({
            "font_size": 24.0
        });

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/create")
                    .method("POST")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_vec(&request_body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        // Missing required field should return 422 or 400
        assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
    }

    #[tokio::test]
    async fn test_create_pdf_negative_font_size() {
        let app = app();

        let request_body = json!({
            "text": "Test",
            "font_size": -10.0
        });

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/create")
                    .method("POST")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_vec(&request_body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        // Should handle negative font size gracefully (might succeed or return error)
        // For now, just check that we get a response
        assert!(
            response.status() == StatusCode::OK
                || response.status() == StatusCode::INTERNAL_SERVER_ERROR
        );
    }

    #[tokio::test]
    async fn test_extract_text_no_file() {
        let app = app();

        let boundary = "boundary123456";
        let body = format!(
            "--{boundary}\r\n\
             Content-Disposition: form-data; name=\"other\"\r\n\r\n\
             not a file\r\n\
             --{boundary}--\r\n"
        );

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/extract")
                    .method("POST")
                    .header(
                        "content-type",
                        format!("multipart/form-data; boundary={boundary}"),
                    )
                    .body(Body::from(body))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);

        let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert!(json["error"].is_string());
        let error_msg = json["error"].as_str().unwrap();
        assert!(
            error_msg.contains("No file provided"),
            "Error was: {}",
            error_msg
        );
    }

    #[tokio::test]
    async fn test_extract_text_invalid_pdf() {
        let app = app();

        let boundary = "boundary123456";
        let body = format!(
            "--{boundary}\r\n\
             Content-Disposition: form-data; name=\"file\"; filename=\"test.pdf\"\r\n\
             Content-Type: application/pdf\r\n\r\n\
             This is not a valid PDF file\r\n\
             --{boundary}--\r\n"
        );

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/extract")
                    .method("POST")
                    .header(
                        "content-type",
                        format!("multipart/form-data; boundary={boundary}"),
                    )
                    .body(Body::from(body))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);

        let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert!(json["error"].is_string());
        let error_msg = json["error"].as_str().unwrap();
        assert!(
            error_msg.contains("Failed to parse PDF"),
            "Error was: {}",
            error_msg
        );
    }

    #[tokio::test]
    async fn test_extract_text_valid_pdf() {
        let app = app();

        // First create a PDF using the create endpoint
        let create_body = json!({
            "text": "Test content for extraction",
            "font_size": 12.0
        });

        let create_response = app
            .clone()
            .oneshot(
                Request::builder()
                    .uri("/api/create")
                    .method("POST")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_vec(&create_body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        let pdf_bytes = to_bytes(create_response.into_body(), usize::MAX)
            .await
            .unwrap();

        // Now test extracting text from this PDF
        let boundary = "boundary123456";
        let mut body = Vec::new();
        body.extend_from_slice(format!("--{boundary}\r\n").as_bytes());
        body.extend_from_slice(
            b"Content-Disposition: form-data; name=\"file\"; filename=\"test.pdf\"\r\n",
        );
        body.extend_from_slice(b"Content-Type: application/pdf\r\n\r\n");
        body.extend_from_slice(&pdf_bytes);
        body.extend_from_slice(format!("\r\n--{boundary}--\r\n").as_bytes());

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/extract")
                    .method("POST")
                    .header(
                        "content-type",
                        format!("multipart/form-data; boundary={boundary}"),
                    )
                    .body(Body::from(body))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);

        let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert!(json["text"].is_string());
        assert!(json["pages"].is_number());
        assert_eq!(json["pages"], 1);
        // The extracted text should contain our original text
        assert!(json["text"]
            .as_str()
            .unwrap()
            .contains("Test content for extraction"));
    }

    #[tokio::test]
    async fn test_404_not_found() {
        let app = app();

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/nonexistent")
                    .method("GET")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn test_method_not_allowed() {
        let app = app();

        // Try DELETE on create endpoint
        let response = app
            .oneshot(
                Request::builder()
                    .uri("/api/create")
                    .method("DELETE")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::METHOD_NOT_ALLOWED);
    }

    #[test]
    fn test_create_pdf_request_deserialize() {
        let json = r#"{"text": "Hello", "font_size": 24.0}"#;
        let request: CreatePdfRequest = serde_json::from_str(json).unwrap();
        assert_eq!(request.text, "Hello");
        assert_eq!(request.font_size, Some(24.0));

        let json = r#"{"text": "Hello"}"#;
        let request: CreatePdfRequest = serde_json::from_str(json).unwrap();
        assert_eq!(request.text, "Hello");
        assert_eq!(request.font_size, None);
    }

    #[test]
    fn test_error_response_serialize() {
        let error = ErrorResponse {
            error: "Test error".to_string(),
        };
        let json = serde_json::to_string(&error).unwrap();
        assert_eq!(json, r#"{"error":"Test error"}"#);
    }

    #[test]
    fn test_extract_text_response_serialize() {
        let response = ExtractTextResponse {
            text: "Extracted text".to_string(),
            pages: 5,
        };
        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("\"text\":\"Extracted text\""));
        assert!(json.contains("\"pages\":5"));
    }

    #[test]
    fn test_app_error_from_pdf_error() {
        let pdf_error = oxidize_pdf::PdfError::InvalidStructure("test".to_string());
        let app_error: AppError = pdf_error.into();
        match app_error {
            AppError::Pdf(_) => {}
            _ => panic!("Expected AppError::Pdf"),
        }
    }

    #[test]
    fn test_app_error_from_io_error() {
        let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let app_error: AppError = io_error.into();
        match app_error {
            AppError::Io(_) => {}
            _ => panic!("Expected AppError::Io"),
        }
    }
}