mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-09-12 17:52:13 +02:00
晚上pdf-markdown it
This commit is contained in:
parent
8b860a9149
commit
0ee17334c4
@ -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.request.MockMvcRequestBuilders.multipart;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
@ -22,14 +24,14 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||||||
"system.enableAlphaFunctionality=false",
|
"system.enableAlphaFunctionality=false",
|
||||||
"system.disableSanitize=false"
|
"system.disableSanitize=false"
|
||||||
})
|
})
|
||||||
@AutoConfigureMockMvc(addFilters = false) // 跳过安全过滤器
|
@AutoConfigureMockMvc(addFilters = false)
|
||||||
public class ConvertPDFToMarkdownIntegrationTest {
|
public class ConvertPDFToMarkdownIntegrationTest {
|
||||||
|
|
||||||
@Autowired private MockMvc mockMvc;
|
@Autowired private MockMvc mockMvc;
|
||||||
|
|
||||||
|
/** 正常 PDF 转换 Markdown */
|
||||||
@Test
|
@Test
|
||||||
public void convertValidPdfToMarkdown_shouldReturnMarkdownBytes() throws Exception {
|
public void convertValidPdfToMarkdown_shouldReturnMarkdownBytes() throws Exception {
|
||||||
// Load sample PDF file from resources
|
|
||||||
ClassPathResource pdfResource = new ClassPathResource("sample/sample.pdf");
|
ClassPathResource pdfResource = new ClassPathResource("sample/sample.pdf");
|
||||||
MockMultipartFile mockFile =
|
MockMultipartFile mockFile =
|
||||||
new MockMultipartFile(
|
new MockMultipartFile(
|
||||||
@ -44,7 +46,59 @@ public class ConvertPDFToMarkdownIntegrationTest {
|
|||||||
header().string("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE));
|
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
|
* This test verifies the behavior when an empty PDF file is uploaded.
|
||||||
// ConvertMarkdownToPdfTest.java already tests the controller logic thoroughly with mocks.
|
*
|
||||||
|
* <p>❌ 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.
|
||||||
|
*
|
||||||
|
* <p>✅ 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.
|
||||||
|
*
|
||||||
|
* <p>❌ 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.
|
||||||
|
*
|
||||||
|
* <p>✅ 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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user