mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-09-12 17:52:13 +02:00
Integration
This commit is contained in:
parent
0ee17334c4
commit
60ecff3051
@ -3,6 +3,7 @@ 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 net.bytebuddy.implementation.bytecode.Throw;
|
||||||
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;
|
||||||
@ -12,6 +13,8 @@ import org.springframework.http.MediaType;
|
|||||||
import org.springframework.mock.web.MockMultipartFile;
|
import org.springframework.mock.web.MockMultipartFile;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
@SpringBootTest(
|
@SpringBootTest(
|
||||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||||
properties = {
|
properties = {
|
||||||
@ -22,17 +25,16 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||||||
"system.enableAlphaFunctionality=false",
|
"system.enableAlphaFunctionality=false",
|
||||||
"system.disableSanitize=false"
|
"system.disableSanitize=false"
|
||||||
})
|
})
|
||||||
@AutoConfigureMockMvc(addFilters = false) // Skip security filters
|
@AutoConfigureMockMvc(addFilters = false) // Skip security filters for integration test
|
||||||
public class ConvertMarkdownToPdfIntegrationTest {
|
public class ConvertMarkdownToPdfIntegrationTest {
|
||||||
|
|
||||||
@Autowired private MockMvc mockMvc;
|
@Autowired private MockMvc mockMvc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration test for converting Markdown to PDF.
|
* Test case: Valid Markdown file input
|
||||||
*
|
*
|
||||||
* <p>Note: This test requires weasyprint to be installed in the system. It will automatically
|
* <p>This test verifies that a proper Markdown file is converted successfully to PDF. If
|
||||||
* skip in environments where weasyprint is not available, so it's designed to be safe to run in
|
* weasyprint is missing, this test will be skipped automatically.
|
||||||
* CI environments.
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void convertValidMarkdownToPdf_shouldReturnPdfBytes() throws Exception {
|
public void convertValidMarkdownToPdf_shouldReturnPdfBytes() throws Exception {
|
||||||
@ -50,7 +52,7 @@ public class ConvertMarkdownToPdfIntegrationTest {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load sample Markdown file from resources
|
// Load sample Markdown file from test resources
|
||||||
ClassPathResource markdownResource = new ClassPathResource("Markdown.md");
|
ClassPathResource markdownResource = new ClassPathResource("Markdown.md");
|
||||||
MockMultipartFile mockFile =
|
MockMultipartFile mockFile =
|
||||||
new MockMultipartFile(
|
new MockMultipartFile(
|
||||||
@ -59,7 +61,6 @@ public class ConvertMarkdownToPdfIntegrationTest {
|
|||||||
"text/markdown",
|
"text/markdown",
|
||||||
markdownResource.getInputStream());
|
markdownResource.getInputStream());
|
||||||
|
|
||||||
// Test the conversion endpoint
|
|
||||||
mockMvc.perform(
|
mockMvc.perform(
|
||||||
multipart("/api/v1/convert/markdown/pdf")
|
multipart("/api/v1/convert/markdown/pdf")
|
||||||
.file(mockFile)
|
.file(mockFile)
|
||||||
@ -68,4 +69,37 @@ public class ConvertMarkdownToPdfIntegrationTest {
|
|||||||
.andExpect(
|
.andExpect(
|
||||||
header().string("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE));
|
header().string("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case: Empty Markdown file
|
||||||
|
*
|
||||||
|
* <p>❌ This test will fail unless the source code explicitly checks for empty input. Source
|
||||||
|
* code should handle fileInput.isEmpty() and return HTTP 400.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void convertEmptyMarkdownFile_shouldReturnError() throws Exception {
|
||||||
|
MockMultipartFile emptyFile =
|
||||||
|
new MockMultipartFile("fileInput", "empty.md", "text/markdown", new byte[0]);
|
||||||
|
|
||||||
|
mockMvc.perform(
|
||||||
|
multipart("/api/v1/convert/markdown/pdf")
|
||||||
|
.file(emptyFile)
|
||||||
|
.contentType(MediaType.MULTIPART_FORM_DATA))
|
||||||
|
.andExpect(status().isBadRequest());// but there is throws IO Exception
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case: Missing fileInput field
|
||||||
|
*
|
||||||
|
* <p>❌ This test will fail with NullPointerException unless the controller checks fileInput !=
|
||||||
|
* null. Controller should return HTTP 400 Bad Request for missing file input.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void missingFileInput_shouldReturnError() throws Exception {
|
||||||
|
mockMvc.perform(
|
||||||
|
multipart("/api/v1/convert/markdown/pdf")
|
||||||
|
.contentType(MediaType.MULTIPART_FORM_DATA))
|
||||||
|
.andExpect(status().isBadRequest());// but there is throws IllegalArgument Exception
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,13 +77,13 @@ public class ConvertPDFToMarkdownIntegrationTest {
|
|||||||
* <p>✅ Expected behavior: The controller should check if `fileInput == null` and return HTTP
|
* <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".
|
* 400 Bad Request with a meaningful error message like "Missing file input".
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void missingFileInput_shouldReturnError() throws Exception {
|
public void missingFileInput_shouldReturnError() throws Exception {
|
||||||
mockMvc.perform(
|
mockMvc.perform(
|
||||||
multipart("/api/v1/convert/pdf/markdown")
|
multipart("/api/v1/convert/pdf/markdown")
|
||||||
.contentType(MediaType.MULTIPART_FORM_DATA))
|
.contentType(MediaType.MULTIPART_FORM_DATA))
|
||||||
.andExpect(status().isBadRequest());
|
.andExpect(status().isBadRequest());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** MIME 类型错误应失败 */
|
/** MIME 类型错误应失败 */
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user