kreuzberg 4.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (265) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yaml +534 -0
  5. data/Gemfile +9 -0
  6. data/Gemfile.lock +157 -0
  7. data/README.md +421 -0
  8. data/Rakefile +25 -0
  9. data/Steepfile +47 -0
  10. data/examples/async_patterns.rb +340 -0
  11. data/ext/kreuzberg_rb/extconf.rb +35 -0
  12. data/ext/kreuzberg_rb/native/Cargo.toml +36 -0
  13. data/ext/kreuzberg_rb/native/README.md +425 -0
  14. data/ext/kreuzberg_rb/native/build.rs +17 -0
  15. data/ext/kreuzberg_rb/native/include/ieeefp.h +11 -0
  16. data/ext/kreuzberg_rb/native/include/msvc_compat/strings.h +14 -0
  17. data/ext/kreuzberg_rb/native/include/strings.h +20 -0
  18. data/ext/kreuzberg_rb/native/include/unistd.h +47 -0
  19. data/ext/kreuzberg_rb/native/src/lib.rs +2939 -0
  20. data/extconf.rb +28 -0
  21. data/kreuzberg.gemspec +105 -0
  22. data/lib/kreuzberg/api_proxy.rb +142 -0
  23. data/lib/kreuzberg/cache_api.rb +45 -0
  24. data/lib/kreuzberg/cli.rb +55 -0
  25. data/lib/kreuzberg/cli_proxy.rb +127 -0
  26. data/lib/kreuzberg/config.rb +684 -0
  27. data/lib/kreuzberg/errors.rb +50 -0
  28. data/lib/kreuzberg/extraction_api.rb +84 -0
  29. data/lib/kreuzberg/mcp_proxy.rb +186 -0
  30. data/lib/kreuzberg/ocr_backend_protocol.rb +113 -0
  31. data/lib/kreuzberg/post_processor_protocol.rb +86 -0
  32. data/lib/kreuzberg/result.rb +216 -0
  33. data/lib/kreuzberg/setup_lib_path.rb +79 -0
  34. data/lib/kreuzberg/validator_protocol.rb +89 -0
  35. data/lib/kreuzberg/version.rb +5 -0
  36. data/lib/kreuzberg.rb +82 -0
  37. data/pkg/kreuzberg-4.0.0.rc1.gem +0 -0
  38. data/sig/kreuzberg/internal.rbs +184 -0
  39. data/sig/kreuzberg.rbs +468 -0
  40. data/spec/binding/cache_spec.rb +227 -0
  41. data/spec/binding/cli_proxy_spec.rb +87 -0
  42. data/spec/binding/cli_spec.rb +54 -0
  43. data/spec/binding/config_spec.rb +345 -0
  44. data/spec/binding/config_validation_spec.rb +283 -0
  45. data/spec/binding/error_handling_spec.rb +213 -0
  46. data/spec/binding/errors_spec.rb +66 -0
  47. data/spec/binding/plugins/ocr_backend_spec.rb +307 -0
  48. data/spec/binding/plugins/postprocessor_spec.rb +269 -0
  49. data/spec/binding/plugins/validator_spec.rb +274 -0
  50. data/spec/examples.txt +104 -0
  51. data/spec/fixtures/config.toml +39 -0
  52. data/spec/fixtures/config.yaml +42 -0
  53. data/spec/fixtures/invalid_config.toml +4 -0
  54. data/spec/smoke/package_spec.rb +178 -0
  55. data/spec/spec_helper.rb +42 -0
  56. data/vendor/kreuzberg/Cargo.toml +134 -0
  57. data/vendor/kreuzberg/README.md +175 -0
  58. data/vendor/kreuzberg/build.rs +460 -0
  59. data/vendor/kreuzberg/src/api/error.rs +81 -0
  60. data/vendor/kreuzberg/src/api/handlers.rs +199 -0
  61. data/vendor/kreuzberg/src/api/mod.rs +79 -0
  62. data/vendor/kreuzberg/src/api/server.rs +353 -0
  63. data/vendor/kreuzberg/src/api/types.rs +170 -0
  64. data/vendor/kreuzberg/src/bin/profile_extract.rs +455 -0
  65. data/vendor/kreuzberg/src/cache/mod.rs +1143 -0
  66. data/vendor/kreuzberg/src/chunking/mod.rs +677 -0
  67. data/vendor/kreuzberg/src/core/batch_mode.rs +35 -0
  68. data/vendor/kreuzberg/src/core/config.rs +1032 -0
  69. data/vendor/kreuzberg/src/core/extractor.rs +903 -0
  70. data/vendor/kreuzberg/src/core/io.rs +327 -0
  71. data/vendor/kreuzberg/src/core/mime.rs +615 -0
  72. data/vendor/kreuzberg/src/core/mod.rs +42 -0
  73. data/vendor/kreuzberg/src/core/pipeline.rs +906 -0
  74. data/vendor/kreuzberg/src/embeddings.rs +323 -0
  75. data/vendor/kreuzberg/src/error.rs +431 -0
  76. data/vendor/kreuzberg/src/extraction/archive.rs +954 -0
  77. data/vendor/kreuzberg/src/extraction/docx.rs +40 -0
  78. data/vendor/kreuzberg/src/extraction/email.rs +854 -0
  79. data/vendor/kreuzberg/src/extraction/excel.rs +688 -0
  80. data/vendor/kreuzberg/src/extraction/html.rs +553 -0
  81. data/vendor/kreuzberg/src/extraction/image.rs +368 -0
  82. data/vendor/kreuzberg/src/extraction/libreoffice.rs +564 -0
  83. data/vendor/kreuzberg/src/extraction/mod.rs +77 -0
  84. data/vendor/kreuzberg/src/extraction/office_metadata/app_properties.rs +398 -0
  85. data/vendor/kreuzberg/src/extraction/office_metadata/core_properties.rs +247 -0
  86. data/vendor/kreuzberg/src/extraction/office_metadata/custom_properties.rs +240 -0
  87. data/vendor/kreuzberg/src/extraction/office_metadata/mod.rs +128 -0
  88. data/vendor/kreuzberg/src/extraction/pandoc/batch.rs +275 -0
  89. data/vendor/kreuzberg/src/extraction/pandoc/mime_types.rs +178 -0
  90. data/vendor/kreuzberg/src/extraction/pandoc/mod.rs +491 -0
  91. data/vendor/kreuzberg/src/extraction/pandoc/server.rs +496 -0
  92. data/vendor/kreuzberg/src/extraction/pandoc/subprocess.rs +1188 -0
  93. data/vendor/kreuzberg/src/extraction/pandoc/version.rs +162 -0
  94. data/vendor/kreuzberg/src/extraction/pptx.rs +3000 -0
  95. data/vendor/kreuzberg/src/extraction/structured.rs +490 -0
  96. data/vendor/kreuzberg/src/extraction/table.rs +328 -0
  97. data/vendor/kreuzberg/src/extraction/text.rs +269 -0
  98. data/vendor/kreuzberg/src/extraction/xml.rs +333 -0
  99. data/vendor/kreuzberg/src/extractors/archive.rs +425 -0
  100. data/vendor/kreuzberg/src/extractors/docx.rs +479 -0
  101. data/vendor/kreuzberg/src/extractors/email.rs +129 -0
  102. data/vendor/kreuzberg/src/extractors/excel.rs +344 -0
  103. data/vendor/kreuzberg/src/extractors/html.rs +410 -0
  104. data/vendor/kreuzberg/src/extractors/image.rs +195 -0
  105. data/vendor/kreuzberg/src/extractors/mod.rs +268 -0
  106. data/vendor/kreuzberg/src/extractors/pandoc.rs +201 -0
  107. data/vendor/kreuzberg/src/extractors/pdf.rs +496 -0
  108. data/vendor/kreuzberg/src/extractors/pptx.rs +234 -0
  109. data/vendor/kreuzberg/src/extractors/structured.rs +126 -0
  110. data/vendor/kreuzberg/src/extractors/text.rs +242 -0
  111. data/vendor/kreuzberg/src/extractors/xml.rs +128 -0
  112. data/vendor/kreuzberg/src/image/dpi.rs +164 -0
  113. data/vendor/kreuzberg/src/image/mod.rs +6 -0
  114. data/vendor/kreuzberg/src/image/preprocessing.rs +417 -0
  115. data/vendor/kreuzberg/src/image/resize.rs +89 -0
  116. data/vendor/kreuzberg/src/keywords/config.rs +154 -0
  117. data/vendor/kreuzberg/src/keywords/mod.rs +237 -0
  118. data/vendor/kreuzberg/src/keywords/processor.rs +267 -0
  119. data/vendor/kreuzberg/src/keywords/rake.rs +294 -0
  120. data/vendor/kreuzberg/src/keywords/types.rs +68 -0
  121. data/vendor/kreuzberg/src/keywords/yake.rs +163 -0
  122. data/vendor/kreuzberg/src/language_detection/mod.rs +942 -0
  123. data/vendor/kreuzberg/src/lib.rs +102 -0
  124. data/vendor/kreuzberg/src/mcp/mod.rs +32 -0
  125. data/vendor/kreuzberg/src/mcp/server.rs +1966 -0
  126. data/vendor/kreuzberg/src/ocr/cache.rs +469 -0
  127. data/vendor/kreuzberg/src/ocr/error.rs +37 -0
  128. data/vendor/kreuzberg/src/ocr/hocr.rs +216 -0
  129. data/vendor/kreuzberg/src/ocr/mod.rs +58 -0
  130. data/vendor/kreuzberg/src/ocr/processor.rs +847 -0
  131. data/vendor/kreuzberg/src/ocr/table/mod.rs +4 -0
  132. data/vendor/kreuzberg/src/ocr/table/tsv_parser.rs +144 -0
  133. data/vendor/kreuzberg/src/ocr/tesseract_backend.rs +450 -0
  134. data/vendor/kreuzberg/src/ocr/types.rs +393 -0
  135. data/vendor/kreuzberg/src/ocr/utils.rs +47 -0
  136. data/vendor/kreuzberg/src/ocr/validation.rs +206 -0
  137. data/vendor/kreuzberg/src/pdf/error.rs +122 -0
  138. data/vendor/kreuzberg/src/pdf/images.rs +139 -0
  139. data/vendor/kreuzberg/src/pdf/metadata.rs +346 -0
  140. data/vendor/kreuzberg/src/pdf/mod.rs +50 -0
  141. data/vendor/kreuzberg/src/pdf/rendering.rs +369 -0
  142. data/vendor/kreuzberg/src/pdf/table.rs +420 -0
  143. data/vendor/kreuzberg/src/pdf/text.rs +161 -0
  144. data/vendor/kreuzberg/src/plugins/extractor.rs +1010 -0
  145. data/vendor/kreuzberg/src/plugins/mod.rs +209 -0
  146. data/vendor/kreuzberg/src/plugins/ocr.rs +629 -0
  147. data/vendor/kreuzberg/src/plugins/processor.rs +641 -0
  148. data/vendor/kreuzberg/src/plugins/registry.rs +1324 -0
  149. data/vendor/kreuzberg/src/plugins/traits.rs +258 -0
  150. data/vendor/kreuzberg/src/plugins/validator.rs +955 -0
  151. data/vendor/kreuzberg/src/stopwords/mod.rs +1470 -0
  152. data/vendor/kreuzberg/src/text/mod.rs +19 -0
  153. data/vendor/kreuzberg/src/text/quality.rs +697 -0
  154. data/vendor/kreuzberg/src/text/string_utils.rs +217 -0
  155. data/vendor/kreuzberg/src/text/token_reduction/cjk_utils.rs +164 -0
  156. data/vendor/kreuzberg/src/text/token_reduction/config.rs +100 -0
  157. data/vendor/kreuzberg/src/text/token_reduction/core.rs +796 -0
  158. data/vendor/kreuzberg/src/text/token_reduction/filters.rs +902 -0
  159. data/vendor/kreuzberg/src/text/token_reduction/mod.rs +160 -0
  160. data/vendor/kreuzberg/src/text/token_reduction/semantic.rs +619 -0
  161. data/vendor/kreuzberg/src/text/token_reduction/simd_text.rs +147 -0
  162. data/vendor/kreuzberg/src/types.rs +873 -0
  163. data/vendor/kreuzberg/src/utils/mod.rs +17 -0
  164. data/vendor/kreuzberg/src/utils/quality.rs +959 -0
  165. data/vendor/kreuzberg/src/utils/string_utils.rs +381 -0
  166. data/vendor/kreuzberg/stopwords/af_stopwords.json +53 -0
  167. data/vendor/kreuzberg/stopwords/ar_stopwords.json +482 -0
  168. data/vendor/kreuzberg/stopwords/bg_stopwords.json +261 -0
  169. data/vendor/kreuzberg/stopwords/bn_stopwords.json +400 -0
  170. data/vendor/kreuzberg/stopwords/br_stopwords.json +1205 -0
  171. data/vendor/kreuzberg/stopwords/ca_stopwords.json +280 -0
  172. data/vendor/kreuzberg/stopwords/cs_stopwords.json +425 -0
  173. data/vendor/kreuzberg/stopwords/da_stopwords.json +172 -0
  174. data/vendor/kreuzberg/stopwords/de_stopwords.json +622 -0
  175. data/vendor/kreuzberg/stopwords/el_stopwords.json +849 -0
  176. data/vendor/kreuzberg/stopwords/en_stopwords.json +1300 -0
  177. data/vendor/kreuzberg/stopwords/eo_stopwords.json +175 -0
  178. data/vendor/kreuzberg/stopwords/es_stopwords.json +734 -0
  179. data/vendor/kreuzberg/stopwords/et_stopwords.json +37 -0
  180. data/vendor/kreuzberg/stopwords/eu_stopwords.json +100 -0
  181. data/vendor/kreuzberg/stopwords/fa_stopwords.json +801 -0
  182. data/vendor/kreuzberg/stopwords/fi_stopwords.json +849 -0
  183. data/vendor/kreuzberg/stopwords/fr_stopwords.json +693 -0
  184. data/vendor/kreuzberg/stopwords/ga_stopwords.json +111 -0
  185. data/vendor/kreuzberg/stopwords/gl_stopwords.json +162 -0
  186. data/vendor/kreuzberg/stopwords/gu_stopwords.json +226 -0
  187. data/vendor/kreuzberg/stopwords/ha_stopwords.json +41 -0
  188. data/vendor/kreuzberg/stopwords/he_stopwords.json +196 -0
  189. data/vendor/kreuzberg/stopwords/hi_stopwords.json +227 -0
  190. data/vendor/kreuzberg/stopwords/hr_stopwords.json +181 -0
  191. data/vendor/kreuzberg/stopwords/hu_stopwords.json +791 -0
  192. data/vendor/kreuzberg/stopwords/hy_stopwords.json +47 -0
  193. data/vendor/kreuzberg/stopwords/id_stopwords.json +760 -0
  194. data/vendor/kreuzberg/stopwords/it_stopwords.json +634 -0
  195. data/vendor/kreuzberg/stopwords/ja_stopwords.json +136 -0
  196. data/vendor/kreuzberg/stopwords/kn_stopwords.json +84 -0
  197. data/vendor/kreuzberg/stopwords/ko_stopwords.json +681 -0
  198. data/vendor/kreuzberg/stopwords/ku_stopwords.json +64 -0
  199. data/vendor/kreuzberg/stopwords/la_stopwords.json +51 -0
  200. data/vendor/kreuzberg/stopwords/lt_stopwords.json +476 -0
  201. data/vendor/kreuzberg/stopwords/lv_stopwords.json +163 -0
  202. data/vendor/kreuzberg/stopwords/ml_stopwords.json +1 -0
  203. data/vendor/kreuzberg/stopwords/mr_stopwords.json +101 -0
  204. data/vendor/kreuzberg/stopwords/ms_stopwords.json +477 -0
  205. data/vendor/kreuzberg/stopwords/ne_stopwords.json +490 -0
  206. data/vendor/kreuzberg/stopwords/nl_stopwords.json +415 -0
  207. data/vendor/kreuzberg/stopwords/no_stopwords.json +223 -0
  208. data/vendor/kreuzberg/stopwords/pl_stopwords.json +331 -0
  209. data/vendor/kreuzberg/stopwords/pt_stopwords.json +562 -0
  210. data/vendor/kreuzberg/stopwords/ro_stopwords.json +436 -0
  211. data/vendor/kreuzberg/stopwords/ru_stopwords.json +561 -0
  212. data/vendor/kreuzberg/stopwords/si_stopwords.json +193 -0
  213. data/vendor/kreuzberg/stopwords/sk_stopwords.json +420 -0
  214. data/vendor/kreuzberg/stopwords/sl_stopwords.json +448 -0
  215. data/vendor/kreuzberg/stopwords/so_stopwords.json +32 -0
  216. data/vendor/kreuzberg/stopwords/st_stopwords.json +33 -0
  217. data/vendor/kreuzberg/stopwords/sv_stopwords.json +420 -0
  218. data/vendor/kreuzberg/stopwords/sw_stopwords.json +76 -0
  219. data/vendor/kreuzberg/stopwords/ta_stopwords.json +129 -0
  220. data/vendor/kreuzberg/stopwords/te_stopwords.json +54 -0
  221. data/vendor/kreuzberg/stopwords/th_stopwords.json +118 -0
  222. data/vendor/kreuzberg/stopwords/tl_stopwords.json +149 -0
  223. data/vendor/kreuzberg/stopwords/tr_stopwords.json +506 -0
  224. data/vendor/kreuzberg/stopwords/uk_stopwords.json +75 -0
  225. data/vendor/kreuzberg/stopwords/ur_stopwords.json +519 -0
  226. data/vendor/kreuzberg/stopwords/vi_stopwords.json +647 -0
  227. data/vendor/kreuzberg/stopwords/yo_stopwords.json +62 -0
  228. data/vendor/kreuzberg/stopwords/zh_stopwords.json +796 -0
  229. data/vendor/kreuzberg/stopwords/zu_stopwords.json +31 -0
  230. data/vendor/kreuzberg/tests/api_tests.rs +966 -0
  231. data/vendor/kreuzberg/tests/archive_integration.rs +543 -0
  232. data/vendor/kreuzberg/tests/batch_orchestration.rs +542 -0
  233. data/vendor/kreuzberg/tests/batch_processing.rs +304 -0
  234. data/vendor/kreuzberg/tests/chunking_offset_demo.rs +92 -0
  235. data/vendor/kreuzberg/tests/concurrency_stress.rs +509 -0
  236. data/vendor/kreuzberg/tests/config_features.rs +580 -0
  237. data/vendor/kreuzberg/tests/config_loading_tests.rs +439 -0
  238. data/vendor/kreuzberg/tests/core_integration.rs +493 -0
  239. data/vendor/kreuzberg/tests/csv_integration.rs +424 -0
  240. data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +124 -0
  241. data/vendor/kreuzberg/tests/email_integration.rs +325 -0
  242. data/vendor/kreuzberg/tests/error_handling.rs +393 -0
  243. data/vendor/kreuzberg/tests/format_integration.rs +159 -0
  244. data/vendor/kreuzberg/tests/helpers/mod.rs +142 -0
  245. data/vendor/kreuzberg/tests/image_integration.rs +253 -0
  246. data/vendor/kreuzberg/tests/keywords_integration.rs +479 -0
  247. data/vendor/kreuzberg/tests/keywords_quality.rs +509 -0
  248. data/vendor/kreuzberg/tests/mime_detection.rs +428 -0
  249. data/vendor/kreuzberg/tests/ocr_configuration.rs +510 -0
  250. data/vendor/kreuzberg/tests/ocr_errors.rs +676 -0
  251. data/vendor/kreuzberg/tests/ocr_quality.rs +627 -0
  252. data/vendor/kreuzberg/tests/ocr_stress.rs +469 -0
  253. data/vendor/kreuzberg/tests/pandoc_integration.rs +503 -0
  254. data/vendor/kreuzberg/tests/pdf_integration.rs +43 -0
  255. data/vendor/kreuzberg/tests/pipeline_integration.rs +1412 -0
  256. data/vendor/kreuzberg/tests/plugin_ocr_backend_test.rs +771 -0
  257. data/vendor/kreuzberg/tests/plugin_postprocessor_test.rs +561 -0
  258. data/vendor/kreuzberg/tests/plugin_system.rs +921 -0
  259. data/vendor/kreuzberg/tests/plugin_validator_test.rs +783 -0
  260. data/vendor/kreuzberg/tests/registry_integration_tests.rs +607 -0
  261. data/vendor/kreuzberg/tests/security_validation.rs +404 -0
  262. data/vendor/kreuzberg/tests/stopwords_integration_test.rs +888 -0
  263. data/vendor/kreuzberg/tests/test_fastembed.rs +609 -0
  264. data/vendor/kreuzberg/tests/xlsx_metadata_extraction_test.rs +87 -0
  265. metadata +471 -0
@@ -0,0 +1,966 @@
1
+ //! Integration tests for the API module.
2
+
3
+ #![cfg(feature = "api")]
4
+
5
+ use axum::{
6
+ body::Body,
7
+ http::{Request, StatusCode},
8
+ };
9
+ use serde_json::json;
10
+ use tower::ServiceExt;
11
+
12
+ use kreuzberg::{
13
+ ExtractionConfig,
14
+ api::{HealthResponse, InfoResponse, create_router},
15
+ };
16
+
17
+ /// Test the health check endpoint.
18
+ #[tokio::test]
19
+ async fn test_health_endpoint() {
20
+ let app = create_router(ExtractionConfig::default());
21
+
22
+ let response = app
23
+ .oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap())
24
+ .await
25
+ .unwrap();
26
+
27
+ assert_eq!(response.status(), StatusCode::OK);
28
+
29
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
30
+ let health: HealthResponse = serde_json::from_slice(&body).unwrap();
31
+
32
+ assert_eq!(health.status, "healthy");
33
+ assert!(!health.version.is_empty());
34
+ }
35
+
36
+ /// Test the info endpoint.
37
+ #[tokio::test]
38
+ async fn test_info_endpoint() {
39
+ let app = create_router(ExtractionConfig::default());
40
+
41
+ let response = app
42
+ .oneshot(Request::builder().uri("/info").body(Body::empty()).unwrap())
43
+ .await
44
+ .unwrap();
45
+
46
+ assert_eq!(response.status(), StatusCode::OK);
47
+
48
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
49
+ let info: InfoResponse = serde_json::from_slice(&body).unwrap();
50
+
51
+ assert!(!info.version.is_empty());
52
+ assert!(info.rust_backend);
53
+ }
54
+
55
+ /// Test extract endpoint with no files returns 400.
56
+ #[tokio::test]
57
+ async fn test_extract_no_files() {
58
+ let app = create_router(ExtractionConfig::default());
59
+
60
+ let boundary = "----boundary";
61
+ let body_content = format!("--{}--\r\n", boundary);
62
+
63
+ let response = app
64
+ .oneshot(
65
+ Request::builder()
66
+ .method("POST")
67
+ .uri("/extract")
68
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
69
+ .body(Body::from(body_content))
70
+ .unwrap(),
71
+ )
72
+ .await
73
+ .unwrap();
74
+
75
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
76
+ }
77
+
78
+ /// Test extract endpoint with a simple text file.
79
+ #[tokio::test]
80
+ async fn test_extract_text_file() {
81
+ let app = create_router(ExtractionConfig::default());
82
+
83
+ let boundary = "----boundary";
84
+ let file_content = "Hello, world!";
85
+
86
+ let body_content = format!(
87
+ "--{}\r\n\
88
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
89
+ Content-Type: text/plain\r\n\
90
+ \r\n\
91
+ {}\r\n\
92
+ --{}--\r\n",
93
+ boundary, file_content, boundary
94
+ );
95
+
96
+ let response = app
97
+ .oneshot(
98
+ Request::builder()
99
+ .method("POST")
100
+ .uri("/extract")
101
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
102
+ .body(Body::from(body_content))
103
+ .unwrap(),
104
+ )
105
+ .await
106
+ .unwrap();
107
+
108
+ assert_eq!(response.status(), StatusCode::OK);
109
+
110
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
111
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
112
+
113
+ assert_eq!(results.len(), 1);
114
+ assert_eq!(results[0]["mime_type"], "text/plain");
115
+ assert!(results[0]["content"].as_str().unwrap().contains("Hello, world!"));
116
+
117
+ assert!(
118
+ results[0]["chunks"].is_null(),
119
+ "Chunks should be null without chunking config"
120
+ );
121
+ assert!(
122
+ results[0]["detected_languages"].is_null(),
123
+ "Language detection not enabled"
124
+ );
125
+ assert!(results[0]["tables"].is_array(), "Tables field should be present");
126
+ assert!(results[0]["metadata"].is_object(), "Metadata field should be present");
127
+ }
128
+
129
+ /// Test extract endpoint with JSON config.
130
+ #[tokio::test]
131
+ async fn test_extract_with_config() {
132
+ let app = create_router(ExtractionConfig::default());
133
+
134
+ let boundary = "----boundary";
135
+ let file_content = "Hello, world!";
136
+ let config = json!({
137
+ "force_ocr": false
138
+ });
139
+
140
+ let body_content = format!(
141
+ "--{}\r\n\
142
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
143
+ Content-Type: text/plain\r\n\
144
+ \r\n\
145
+ {}\r\n\
146
+ --{}\r\n\
147
+ Content-Disposition: form-data; name=\"config\"\r\n\
148
+ \r\n\
149
+ {}\r\n\
150
+ --{}--\r\n",
151
+ boundary, file_content, boundary, config, boundary
152
+ );
153
+
154
+ let response = app
155
+ .oneshot(
156
+ Request::builder()
157
+ .method("POST")
158
+ .uri("/extract")
159
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
160
+ .body(Body::from(body_content))
161
+ .unwrap(),
162
+ )
163
+ .await
164
+ .unwrap();
165
+
166
+ assert_eq!(response.status(), StatusCode::OK);
167
+
168
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
169
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
170
+
171
+ assert_eq!(results.len(), 1);
172
+ assert_eq!(results[0]["mime_type"], "text/plain");
173
+ assert!(results[0]["content"].as_str().unwrap().contains("Hello, world!"));
174
+
175
+ assert!(
176
+ results[0]["chunks"].is_null(),
177
+ "Chunks should be null without chunking config"
178
+ );
179
+ assert!(
180
+ results[0]["detected_languages"].is_null(),
181
+ "Language detection not enabled"
182
+ );
183
+ assert!(results[0]["tables"].is_array(), "Tables field should be present");
184
+ assert!(results[0]["metadata"].is_object(), "Metadata field should be present");
185
+ }
186
+
187
+ /// Test extract endpoint with invalid config returns 400.
188
+ #[tokio::test]
189
+ async fn test_extract_invalid_config() {
190
+ let app = create_router(ExtractionConfig::default());
191
+
192
+ let boundary = "----boundary";
193
+ let file_content = "Hello, world!";
194
+ let invalid_config = "not valid json";
195
+
196
+ let body_content = format!(
197
+ "--{}\r\n\
198
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
199
+ Content-Type: text/plain\r\n\
200
+ \r\n\
201
+ {}\r\n\
202
+ --{}\r\n\
203
+ Content-Disposition: form-data; name=\"config\"\r\n\
204
+ \r\n\
205
+ {}\r\n\
206
+ --{}--\r\n",
207
+ boundary, file_content, boundary, invalid_config, boundary
208
+ );
209
+
210
+ let response = app
211
+ .oneshot(
212
+ Request::builder()
213
+ .method("POST")
214
+ .uri("/extract")
215
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
216
+ .body(Body::from(body_content))
217
+ .unwrap(),
218
+ )
219
+ .await
220
+ .unwrap();
221
+
222
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
223
+ }
224
+
225
+ /// Test extract endpoint with multiple files.
226
+ #[tokio::test]
227
+ async fn test_extract_multiple_files() {
228
+ let app = create_router(ExtractionConfig::default());
229
+
230
+ let boundary = "----boundary";
231
+ let file1_content = "First file content";
232
+ let file2_content = "Second file content";
233
+
234
+ let body_content = format!(
235
+ "--{}\r\n\
236
+ Content-Disposition: form-data; name=\"files\"; filename=\"test1.txt\"\r\n\
237
+ Content-Type: text/plain\r\n\
238
+ \r\n\
239
+ {}\r\n\
240
+ --{}\r\n\
241
+ Content-Disposition: form-data; name=\"files\"; filename=\"test2.txt\"\r\n\
242
+ Content-Type: text/plain\r\n\
243
+ \r\n\
244
+ {}\r\n\
245
+ --{}--\r\n",
246
+ boundary, file1_content, boundary, file2_content, boundary
247
+ );
248
+
249
+ let response = app
250
+ .oneshot(
251
+ Request::builder()
252
+ .method("POST")
253
+ .uri("/extract")
254
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
255
+ .body(Body::from(body_content))
256
+ .unwrap(),
257
+ )
258
+ .await
259
+ .unwrap();
260
+
261
+ assert_eq!(response.status(), StatusCode::OK);
262
+
263
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
264
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
265
+
266
+ assert_eq!(results.len(), 2);
267
+ assert!(results[0]["content"].as_str().unwrap().contains("First file"));
268
+ assert!(results[1]["content"].as_str().unwrap().contains("Second file"));
269
+
270
+ for result in &results {
271
+ assert!(
272
+ result["chunks"].is_null(),
273
+ "Chunks should be null without chunking config"
274
+ );
275
+ assert!(result["detected_languages"].is_null(), "Language detection not enabled");
276
+ assert!(result["tables"].is_array(), "Tables field should be present");
277
+ assert!(result["metadata"].is_object(), "Metadata field should be present");
278
+ assert_eq!(result["mime_type"], "text/plain");
279
+ }
280
+ }
281
+
282
+ /// Test extract endpoint with markdown content.
283
+ #[tokio::test]
284
+ async fn test_extract_markdown_file() {
285
+ let app = create_router(ExtractionConfig::default());
286
+
287
+ let boundary = "----boundary";
288
+ let file_content = "# Heading\n\nSome **bold** text.";
289
+
290
+ let body_content = format!(
291
+ "--{}\r\n\
292
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.md\"\r\n\
293
+ Content-Type: text/markdown\r\n\
294
+ \r\n\
295
+ {}\r\n\
296
+ --{}--\r\n",
297
+ boundary, file_content, boundary
298
+ );
299
+
300
+ let response = app
301
+ .oneshot(
302
+ Request::builder()
303
+ .method("POST")
304
+ .uri("/extract")
305
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
306
+ .body(Body::from(body_content))
307
+ .unwrap(),
308
+ )
309
+ .await
310
+ .unwrap();
311
+
312
+ assert_eq!(response.status(), StatusCode::OK);
313
+
314
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
315
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
316
+
317
+ assert_eq!(results.len(), 1);
318
+ assert_eq!(results[0]["mime_type"], "text/markdown");
319
+ assert!(results[0]["content"].as_str().unwrap().contains("Heading"));
320
+ }
321
+
322
+ /// Test extract endpoint with JSON content.
323
+ #[tokio::test]
324
+ async fn test_extract_json_file() {
325
+ let app = create_router(ExtractionConfig::default());
326
+
327
+ let boundary = "----boundary";
328
+ let file_content = r#"{"key": "value", "number": 42}"#;
329
+
330
+ let body_content = format!(
331
+ "--{}\r\n\
332
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.json\"\r\n\
333
+ Content-Type: application/json\r\n\
334
+ \r\n\
335
+ {}\r\n\
336
+ --{}--\r\n",
337
+ boundary, file_content, boundary
338
+ );
339
+
340
+ let response = app
341
+ .oneshot(
342
+ Request::builder()
343
+ .method("POST")
344
+ .uri("/extract")
345
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
346
+ .body(Body::from(body_content))
347
+ .unwrap(),
348
+ )
349
+ .await
350
+ .unwrap();
351
+
352
+ assert_eq!(response.status(), StatusCode::OK);
353
+
354
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
355
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
356
+
357
+ assert_eq!(results.len(), 1);
358
+ assert_eq!(results[0]["mime_type"], "application/json");
359
+ }
360
+
361
+ /// Test extract endpoint with XML content.
362
+ #[tokio::test]
363
+ #[cfg(feature = "xml")]
364
+ async fn test_extract_xml_file() {
365
+ let app = create_router(ExtractionConfig::default());
366
+
367
+ let boundary = "----boundary";
368
+ let file_content = r#"<?xml version="1.0"?><root><item>test</item></root>"#;
369
+
370
+ let body_content = format!(
371
+ "--{}\r\n\
372
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.xml\"\r\n\
373
+ Content-Type: application/xml\r\n\
374
+ \r\n\
375
+ {}\r\n\
376
+ --{}--\r\n",
377
+ boundary, file_content, boundary
378
+ );
379
+
380
+ let response = app
381
+ .oneshot(
382
+ Request::builder()
383
+ .method("POST")
384
+ .uri("/extract")
385
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
386
+ .body(Body::from(body_content))
387
+ .unwrap(),
388
+ )
389
+ .await
390
+ .unwrap();
391
+
392
+ assert_eq!(response.status(), StatusCode::OK);
393
+
394
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
395
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
396
+
397
+ assert_eq!(results.len(), 1);
398
+ assert_eq!(results[0]["mime_type"], "application/xml");
399
+ assert!(results[0]["content"].as_str().unwrap().contains("test"));
400
+ }
401
+
402
+ /// Test extract endpoint with HTML content.
403
+ #[tokio::test]
404
+ #[cfg(feature = "html")]
405
+ async fn test_extract_html_file() {
406
+ let app = create_router(ExtractionConfig::default());
407
+
408
+ let boundary = "----boundary";
409
+ let file_content = r#"<html><body><h1>Title</h1><p>Content</p></body></html>"#;
410
+
411
+ let body_content = format!(
412
+ "--{}\r\n\
413
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.html\"\r\n\
414
+ Content-Type: text/html\r\n\
415
+ \r\n\
416
+ {}\r\n\
417
+ --{}--\r\n",
418
+ boundary, file_content, boundary
419
+ );
420
+
421
+ let response = app
422
+ .oneshot(
423
+ Request::builder()
424
+ .method("POST")
425
+ .uri("/extract")
426
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
427
+ .body(Body::from(body_content))
428
+ .unwrap(),
429
+ )
430
+ .await
431
+ .unwrap();
432
+
433
+ assert_eq!(response.status(), StatusCode::OK);
434
+
435
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
436
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
437
+
438
+ assert_eq!(results.len(), 1);
439
+ assert_eq!(results[0]["mime_type"], "text/html");
440
+ assert!(results[0]["content"].as_str().unwrap().contains("Title"));
441
+ }
442
+
443
+ /// Test extract endpoint with missing Content-Type header.
444
+ #[tokio::test]
445
+ async fn test_extract_missing_content_type() {
446
+ let app = create_router(ExtractionConfig::default());
447
+
448
+ let response = app
449
+ .oneshot(
450
+ Request::builder()
451
+ .method("POST")
452
+ .uri("/extract")
453
+ .body(Body::from("some data"))
454
+ .unwrap(),
455
+ )
456
+ .await
457
+ .unwrap();
458
+
459
+ assert!(response.status() == StatusCode::BAD_REQUEST || response.status() == StatusCode::UNSUPPORTED_MEDIA_TYPE);
460
+ }
461
+
462
+ /// Test extract endpoint with empty file.
463
+ #[tokio::test]
464
+ async fn test_extract_empty_file() {
465
+ let app = create_router(ExtractionConfig::default());
466
+
467
+ let boundary = "----boundary";
468
+ let file_content = "";
469
+
470
+ let body_content = format!(
471
+ "--{}\r\n\
472
+ Content-Disposition: form-data; name=\"files\"; filename=\"empty.txt\"\r\n\
473
+ Content-Type: text/plain\r\n\
474
+ \r\n\
475
+ {}\r\n\
476
+ --{}--\r\n",
477
+ boundary, file_content, boundary
478
+ );
479
+
480
+ let response = app
481
+ .oneshot(
482
+ Request::builder()
483
+ .method("POST")
484
+ .uri("/extract")
485
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
486
+ .body(Body::from(body_content))
487
+ .unwrap(),
488
+ )
489
+ .await
490
+ .unwrap();
491
+
492
+ assert_eq!(response.status(), StatusCode::OK);
493
+
494
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
495
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
496
+
497
+ assert_eq!(results.len(), 1);
498
+ assert_eq!(results[0]["mime_type"], "text/plain");
499
+ }
500
+
501
+ /// Test extract endpoint with unsupported MIME type.
502
+ #[tokio::test]
503
+ async fn test_extract_unsupported_mime_type() {
504
+ let app = create_router(ExtractionConfig::default());
505
+
506
+ let boundary = "----boundary";
507
+ let file_content = "Binary data";
508
+
509
+ let body_content = format!(
510
+ "--{}\r\n\
511
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.bin\"\r\n\
512
+ Content-Type: application/x-unknown-binary\r\n\
513
+ \r\n\
514
+ {}\r\n\
515
+ --{}--\r\n",
516
+ boundary, file_content, boundary
517
+ );
518
+
519
+ let response = app
520
+ .oneshot(
521
+ Request::builder()
522
+ .method("POST")
523
+ .uri("/extract")
524
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
525
+ .body(Body::from(body_content))
526
+ .unwrap(),
527
+ )
528
+ .await
529
+ .unwrap();
530
+
531
+ assert!(
532
+ response.status() == StatusCode::UNPROCESSABLE_ENTITY || response.status() == StatusCode::INTERNAL_SERVER_ERROR
533
+ );
534
+ }
535
+
536
+ /// Test extract endpoint without filename in multipart field.
537
+ #[tokio::test]
538
+ async fn test_extract_without_filename() {
539
+ let app = create_router(ExtractionConfig::default());
540
+
541
+ let boundary = "----boundary";
542
+ let file_content = "Test content";
543
+
544
+ let body_content = format!(
545
+ "--{}\r\n\
546
+ Content-Disposition: form-data; name=\"files\"\r\n\
547
+ Content-Type: text/plain\r\n\
548
+ \r\n\
549
+ {}\r\n\
550
+ --{}--\r\n",
551
+ boundary, file_content, boundary
552
+ );
553
+
554
+ let response = app
555
+ .oneshot(
556
+ Request::builder()
557
+ .method("POST")
558
+ .uri("/extract")
559
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
560
+ .body(Body::from(body_content))
561
+ .unwrap(),
562
+ )
563
+ .await
564
+ .unwrap();
565
+
566
+ assert_eq!(response.status(), StatusCode::OK);
567
+ }
568
+
569
+ /// Test extract endpoint with malformed multipart data.
570
+ #[tokio::test]
571
+ async fn test_extract_malformed_multipart() {
572
+ let app = create_router(ExtractionConfig::default());
573
+
574
+ let boundary = "----boundary";
575
+ let body_content = format!("--{}\r\nmalformed data\r\n", boundary);
576
+
577
+ let response = app
578
+ .oneshot(
579
+ Request::builder()
580
+ .method("POST")
581
+ .uri("/extract")
582
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
583
+ .body(Body::from(body_content))
584
+ .unwrap(),
585
+ )
586
+ .await
587
+ .unwrap();
588
+
589
+ assert!(response.status().is_client_error() || response.status().is_server_error());
590
+ }
591
+
592
+ /// Test CORS headers are present.
593
+ #[tokio::test]
594
+ async fn test_cors_headers() {
595
+ let app = create_router(ExtractionConfig::default());
596
+
597
+ let response = app
598
+ .oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap())
599
+ .await
600
+ .unwrap();
601
+
602
+ assert_eq!(response.status(), StatusCode::OK);
603
+
604
+ let headers = response.headers();
605
+ assert!(headers.contains_key("access-control-allow-origin") || headers.contains_key("Access-Control-Allow-Origin"));
606
+ }
607
+
608
+ /// Test OPTIONS preflight request for CORS.
609
+ #[tokio::test]
610
+ async fn test_cors_preflight() {
611
+ let app = create_router(ExtractionConfig::default());
612
+
613
+ let response = app
614
+ .oneshot(
615
+ Request::builder()
616
+ .method("OPTIONS")
617
+ .uri("/extract")
618
+ .header("origin", "http://example.com")
619
+ .header("access-control-request-method", "POST")
620
+ .body(Body::empty())
621
+ .unwrap(),
622
+ )
623
+ .await
624
+ .unwrap();
625
+
626
+ assert!(response.status().is_success() || response.status() == StatusCode::NO_CONTENT);
627
+ }
628
+
629
+ /// Test error response format for validation errors.
630
+ #[tokio::test]
631
+ async fn test_error_response_format_validation() {
632
+ let app = create_router(ExtractionConfig::default());
633
+
634
+ let boundary = "----boundary";
635
+ let body_content = format!("--{}--\r\n", boundary);
636
+
637
+ let response = app
638
+ .oneshot(
639
+ Request::builder()
640
+ .method("POST")
641
+ .uri("/extract")
642
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
643
+ .body(Body::from(body_content))
644
+ .unwrap(),
645
+ )
646
+ .await
647
+ .unwrap();
648
+
649
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
650
+
651
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
652
+ let error: serde_json::Value = serde_json::from_slice(&body).unwrap();
653
+
654
+ assert!(error["error_type"].is_string());
655
+ assert!(error["message"].is_string());
656
+ assert_eq!(error["status_code"], 400);
657
+ assert_eq!(error["error_type"], "ValidationError");
658
+ }
659
+
660
+ /// Test error response format for parsing errors.
661
+ #[tokio::test]
662
+ async fn test_error_response_format_parsing() {
663
+ let app = create_router(ExtractionConfig::default());
664
+
665
+ let boundary = "----boundary";
666
+ let invalid_config = "not valid json";
667
+
668
+ let body_content = format!(
669
+ "--{}\r\n\
670
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
671
+ Content-Type: text/plain\r\n\
672
+ \r\n\
673
+ content\r\n\
674
+ --{}\r\n\
675
+ Content-Disposition: form-data; name=\"config\"\r\n\
676
+ \r\n\
677
+ {}\r\n\
678
+ --{}--\r\n",
679
+ boundary, boundary, invalid_config, boundary
680
+ );
681
+
682
+ let response = app
683
+ .oneshot(
684
+ Request::builder()
685
+ .method("POST")
686
+ .uri("/extract")
687
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
688
+ .body(Body::from(body_content))
689
+ .unwrap(),
690
+ )
691
+ .await
692
+ .unwrap();
693
+
694
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
695
+
696
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
697
+ let error: serde_json::Value = serde_json::from_slice(&body).unwrap();
698
+
699
+ assert_eq!(error["error_type"], "ValidationError");
700
+ assert!(error["message"].as_str().unwrap().contains("configuration"));
701
+ }
702
+
703
+ /// Test 404 error for non-existent endpoint.
704
+ #[tokio::test]
705
+ async fn test_not_found_endpoint() {
706
+ let app = create_router(ExtractionConfig::default());
707
+
708
+ let response = app
709
+ .oneshot(Request::builder().uri("/nonexistent").body(Body::empty()).unwrap())
710
+ .await
711
+ .unwrap();
712
+
713
+ assert_eq!(response.status(), StatusCode::NOT_FOUND);
714
+ }
715
+
716
+ /// Test extract endpoint with very large text content.
717
+ #[tokio::test]
718
+ async fn test_extract_large_file() {
719
+ let app = create_router(ExtractionConfig::default());
720
+
721
+ let boundary = "----boundary";
722
+ let large_content = "A".repeat(1024 * 1024);
723
+
724
+ let body_content = format!(
725
+ "--{}\r\n\
726
+ Content-Disposition: form-data; name=\"files\"; filename=\"large.txt\"\r\n\
727
+ Content-Type: text/plain\r\n\
728
+ \r\n\
729
+ {}\r\n\
730
+ --{}--\r\n",
731
+ boundary, large_content, boundary
732
+ );
733
+
734
+ let response = app
735
+ .oneshot(
736
+ Request::builder()
737
+ .method("POST")
738
+ .uri("/extract")
739
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
740
+ .body(Body::from(body_content))
741
+ .unwrap(),
742
+ )
743
+ .await
744
+ .unwrap();
745
+
746
+ assert_eq!(response.status(), StatusCode::OK);
747
+
748
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
749
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
750
+
751
+ assert_eq!(results.len(), 1);
752
+ assert_eq!(results[0]["mime_type"], "text/plain");
753
+ }
754
+
755
+ /// Test concurrent requests to extract endpoint.
756
+ #[tokio::test]
757
+ async fn test_concurrent_requests() {
758
+ use tower::ServiceExt;
759
+
760
+ let app = create_router(ExtractionConfig::default());
761
+
762
+ let boundary = "----boundary";
763
+ let file_content = "Concurrent test";
764
+
765
+ let body_content = format!(
766
+ "--{}\r\n\
767
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
768
+ Content-Type: text/plain\r\n\
769
+ \r\n\
770
+ {}\r\n\
771
+ --{}--\r\n",
772
+ boundary, file_content, boundary
773
+ );
774
+
775
+ let mut handles = vec![];
776
+
777
+ for _ in 0..5 {
778
+ let app_clone = app.clone();
779
+ let body_clone = body_content.clone();
780
+
781
+ let handle = tokio::spawn(async move {
782
+ app_clone
783
+ .oneshot(
784
+ Request::builder()
785
+ .method("POST")
786
+ .uri("/extract")
787
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
788
+ .body(Body::from(body_clone))
789
+ .unwrap(),
790
+ )
791
+ .await
792
+ });
793
+
794
+ handles.push(handle);
795
+ }
796
+
797
+ for handle in handles {
798
+ let response = handle.await.unwrap().unwrap();
799
+ assert_eq!(response.status(), StatusCode::OK);
800
+ }
801
+ }
802
+
803
+ /// Test cache stats endpoint.
804
+ #[tokio::test]
805
+ async fn test_cache_stats_endpoint() {
806
+ let app = create_router(ExtractionConfig::default());
807
+
808
+ let response = app
809
+ .oneshot(Request::builder().uri("/cache/stats").body(Body::empty()).unwrap())
810
+ .await
811
+ .unwrap();
812
+
813
+ assert_eq!(response.status(), StatusCode::OK);
814
+
815
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
816
+ let stats: serde_json::Value = serde_json::from_slice(&body).unwrap();
817
+
818
+ assert!(stats["directory"].is_string());
819
+ assert!(stats["total_files"].is_number());
820
+ assert!(stats["total_size_mb"].is_number());
821
+ }
822
+
823
+ /// Test cache clear endpoint.
824
+ #[tokio::test]
825
+ async fn test_cache_clear_endpoint() {
826
+ let app = create_router(ExtractionConfig::default());
827
+
828
+ let response = app
829
+ .oneshot(
830
+ Request::builder()
831
+ .method("DELETE")
832
+ .uri("/cache/clear")
833
+ .body(Body::empty())
834
+ .unwrap(),
835
+ )
836
+ .await
837
+ .unwrap();
838
+
839
+ assert_eq!(response.status(), StatusCode::OK);
840
+
841
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
842
+ let clear_result: serde_json::Value = serde_json::from_slice(&body).unwrap();
843
+
844
+ assert!(clear_result["directory"].is_string());
845
+ assert!(clear_result["removed_files"].is_number());
846
+ assert!(clear_result["freed_mb"].is_number());
847
+ }
848
+
849
+ /// Test extract endpoint with mixed content types.
850
+ #[tokio::test]
851
+ async fn test_extract_mixed_content_types() {
852
+ let app = create_router(ExtractionConfig::default());
853
+
854
+ let boundary = "----boundary";
855
+ let text_content = "Text file";
856
+ let json_content = r#"{"test": "data"}"#;
857
+
858
+ let body_content = format!(
859
+ "--{}\r\n\
860
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
861
+ Content-Type: text/plain\r\n\
862
+ \r\n\
863
+ {}\r\n\
864
+ --{}\r\n\
865
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.json\"\r\n\
866
+ Content-Type: application/json\r\n\
867
+ \r\n\
868
+ {}\r\n\
869
+ --{}--\r\n",
870
+ boundary, text_content, boundary, json_content, boundary
871
+ );
872
+
873
+ let response = app
874
+ .oneshot(
875
+ Request::builder()
876
+ .method("POST")
877
+ .uri("/extract")
878
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
879
+ .body(Body::from(body_content))
880
+ .unwrap(),
881
+ )
882
+ .await
883
+ .unwrap();
884
+
885
+ assert_eq!(response.status(), StatusCode::OK);
886
+
887
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
888
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
889
+
890
+ assert_eq!(results.len(), 2);
891
+ assert_eq!(results[0]["mime_type"], "text/plain");
892
+ assert_eq!(results[1]["mime_type"], "application/json");
893
+ }
894
+
895
+ /// Test extract endpoint with unknown field in multipart.
896
+ #[tokio::test]
897
+ async fn test_extract_unknown_multipart_field() {
898
+ let app = create_router(ExtractionConfig::default());
899
+
900
+ let boundary = "----boundary";
901
+ let file_content = "Test content";
902
+
903
+ let body_content = format!(
904
+ "--{}\r\n\
905
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
906
+ Content-Type: text/plain\r\n\
907
+ \r\n\
908
+ {}\r\n\
909
+ --{}\r\n\
910
+ Content-Disposition: form-data; name=\"unknown_field\"\r\n\
911
+ \r\n\
912
+ some value\r\n\
913
+ --{}--\r\n",
914
+ boundary, file_content, boundary, boundary
915
+ );
916
+
917
+ let response = app
918
+ .oneshot(
919
+ Request::builder()
920
+ .method("POST")
921
+ .uri("/extract")
922
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
923
+ .body(Body::from(body_content))
924
+ .unwrap(),
925
+ )
926
+ .await
927
+ .unwrap();
928
+
929
+ assert_eq!(response.status(), StatusCode::OK);
930
+ }
931
+
932
+ /// Test extract endpoint with default MIME type (application/octet-stream).
933
+ #[tokio::test]
934
+ async fn test_extract_default_mime_type() {
935
+ let app = create_router(ExtractionConfig::default());
936
+
937
+ let boundary = "----boundary";
938
+ let file_content = "Test content";
939
+
940
+ let body_content = format!(
941
+ "--{}\r\n\
942
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
943
+ \r\n\
944
+ {}\r\n\
945
+ --{}--\r\n",
946
+ boundary, file_content, boundary
947
+ );
948
+
949
+ let response = app
950
+ .oneshot(
951
+ Request::builder()
952
+ .method("POST")
953
+ .uri("/extract")
954
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
955
+ .body(Body::from(body_content))
956
+ .unwrap(),
957
+ )
958
+ .await
959
+ .unwrap();
960
+
961
+ assert!(
962
+ response.status() == StatusCode::OK
963
+ || response.status() == StatusCode::UNPROCESSABLE_ENTITY
964
+ || response.status() == StatusCode::INTERNAL_SERVER_ERROR
965
+ );
966
+ }