|
| 1 | +package org.example; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.junit.jupiter.api.io.TempDir; |
| 5 | + |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +/** |
| 13 | + * Unit test class for verifying the behavior of the StaticFileHandler class. |
| 14 | + * |
| 15 | + * This test class ensures that StaticFileHandler correctly handles GET requests |
| 16 | + * for static files, including both cases where the requested file exists and |
| 17 | + * where it does not. Temporary directories and files are utilized in tests to |
| 18 | + * ensure no actual file system dependencies during test execution. |
| 19 | + * |
| 20 | + * Key functional aspects being tested include: |
| 21 | + * - Correct response status code and content for an existing file. |
| 22 | + * - Correct response status code and fallback behavior for a missing file. |
| 23 | + */ |
| 24 | +class StaticFileHandlerTest { |
| 25 | + |
| 26 | + //Junit creates a temporary folder which can be filled with temporary files that gets removed after tests |
| 27 | + @TempDir |
| 28 | + Path tempDir; |
| 29 | + |
| 30 | + |
| 31 | + @Test |
| 32 | + void test_file_that_exists_should_return_200() throws IOException { |
| 33 | + //Arrange |
| 34 | + Path testFile = tempDir.resolve("test.html"); // Defines the path in the temp directory |
| 35 | + Files.writeString(testFile, "Hello Test"); // Creates a text in that file |
| 36 | + |
| 37 | + //Using the new constructor in StaticFileHandler to reroute so the tests uses the temporary folder instead of the hardcoded www |
| 38 | + StaticFileHandler staticFileHandler = new StaticFileHandler(tempDir.toString()); |
| 39 | + |
| 40 | + //Using ByteArrayOutputStream instead of Outputstream during tests to capture the servers response in memory, fake stream |
| 41 | + ByteArrayOutputStream fakeOutput = new ByteArrayOutputStream(); |
| 42 | + |
| 43 | + //Act |
| 44 | + staticFileHandler.sendGetRequest(fakeOutput, "test.html"); //Get test.html and write the answer to fakeOutput |
| 45 | + |
| 46 | + //Assert |
| 47 | + String response = fakeOutput.toString();//Converts the captured byte stream into a String for verification |
| 48 | + |
| 49 | + assertTrue(response.contains("HTTP/1.1 200 OK")); // Assert the status |
| 50 | + assertTrue(response.contains("Hello Test")); //Assert the content in the file |
| 51 | + |
| 52 | + assertTrue(response.contains("Content-Type: text/html; charset=utf-8")); // Verify the correct Content-type header |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void test_file_that_does_not_exists_should_return_404() throws IOException { |
| 58 | + //Arrange |
| 59 | + // Pre-create the mandatory error page in the temp directory to prevent NoSuchFileException |
| 60 | + Path testFile = tempDir.resolve("pageNotFound.html"); |
| 61 | + Files.writeString(testFile, "Fallback page"); |
| 62 | + |
| 63 | + //Using the new constructor in StaticFileHandler to reroute so the tests uses the temporary folder instead of the hardcoded www |
| 64 | + StaticFileHandler staticFileHandler = new StaticFileHandler(tempDir.toString()); |
| 65 | + |
| 66 | + //Using ByteArrayOutputStream instead of Outputstream during tests to capture the servers response in memory, fake stream |
| 67 | + ByteArrayOutputStream fakeOutput = new ByteArrayOutputStream(); |
| 68 | + |
| 69 | + //Act |
| 70 | + staticFileHandler.sendGetRequest(fakeOutput, "notExistingFile.html"); // Request a file that clearly doesn't exist to trigger the 404 logic |
| 71 | + |
| 72 | + //Assert |
| 73 | + String response = fakeOutput.toString();//Converts the captured byte stream into a String for verification |
| 74 | + |
| 75 | + assertTrue(response.contains("HTTP/1.1 404 Not Found")); // Assert the status |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments