From 0ee17334c46029aac342fcd70f876bf18ebc50c0 Mon Sep 17 00:00:00 2001 From: Carpe-Wang Date: Sat, 19 Apr 2025 14:52:22 -0400 Subject: [PATCH] =?UTF-8?q?=E6=99=9A=E4=B8=8Apdf-markdown=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConvertPDFToMarkdownIntegrationTest.java | 64 +++++++++++++++++-- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/src/test/java/stirling/software/SPDF/utils/ConvertPDFToMarkdownIntegrationTest.java b/src/test/java/stirling/software/SPDF/utils/ConvertPDFToMarkdownIntegrationTest.java index a85f32a33..fac70a8af 100644 --- a/src/test/java/stirling/software/SPDF/utils/ConvertPDFToMarkdownIntegrationTest.java +++ b/src/test/java/stirling/software/SPDF/utils/ConvertPDFToMarkdownIntegrationTest.java @@ -3,6 +3,8 @@ package stirling.software.SPDF.utils; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import java.nio.charset.StandardCharsets; + import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -22,14 +24,14 @@ import org.springframework.test.web.servlet.MockMvc; "system.enableAlphaFunctionality=false", "system.disableSanitize=false" }) -@AutoConfigureMockMvc(addFilters = false) // 跳过安全过滤器 +@AutoConfigureMockMvc(addFilters = false) public class ConvertPDFToMarkdownIntegrationTest { @Autowired private MockMvc mockMvc; + /** 正常 PDF 转换 Markdown */ @Test public void convertValidPdfToMarkdown_shouldReturnMarkdownBytes() throws Exception { - // Load sample PDF file from resources ClassPathResource pdfResource = new ClassPathResource("sample/sample.pdf"); MockMultipartFile mockFile = new MockMultipartFile( @@ -44,7 +46,59 @@ public class ConvertPDFToMarkdownIntegrationTest { header().string("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE)); } - // The Markdown to PDF integration test is omitted because it requires weasyprint, which - // is unlikely to be available in the test environment. The unit test in - // ConvertMarkdownToPdfTest.java already tests the controller logic thoroughly with mocks. + /** + * This test verifies the behavior when an empty PDF file is uploaded. + * + *

❌ This test fails because the source code does NOT check whether the uploaded file is + * empty. It directly tries to parse it with PDFBox, which throws an IOException. + * + *

✅ Expected behavior: The controller should check `fileInput.isEmpty()` and return HTTP 400 + * Bad Request. + */ + @Test + public void convertEmptyPdfFile_shouldReturnError() throws Exception { + MockMultipartFile emptyFile = + new MockMultipartFile("fileInput", "empty.pdf", "application/pdf", new byte[0]); + + mockMvc.perform( + multipart("/api/v1/convert/pdf/markdown") + .file(emptyFile) + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andExpect(status().isBadRequest()); + } + + /** + * This test verifies the behavior when no `fileInput` field is provided in the request. + * + *

❌ This test fails because the source code does NOT check whether `fileInput` is null. It + * tries to access `fileInput.getOriginalFilename()` without null-check, causing + * NullPointerException. + * + *

✅ Expected behavior: The controller should check if `fileInput == null` and return HTTP + * 400 Bad Request with a meaningful error message like "Missing file input". + */ + @Test + public void missingFileInput_shouldReturnError() throws Exception { + mockMvc.perform( + multipart("/api/v1/convert/pdf/markdown") + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andExpect(status().isBadRequest()); + } + + /** MIME 类型错误应失败 */ + @Test + public void convertWrongMimeType_shouldReturnError() throws Exception { + MockMultipartFile wrongMimeFile = + new MockMultipartFile( + "fileInput", + "notpdf.txt", + "text/plain", + "Not a real PDF.".getBytes(StandardCharsets.UTF_8)); + + mockMvc.perform( + multipart("/api/v1/convert/pdf/markdown") + .file(wrongMimeFile) + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andExpect(status().isBadRequest()); + } }