@@ -39,6 +39,7 @@ private String sendRequest(String uri) throws IOException {
3939
4040
4141 //Junit creates a temporary folder which can be filled with temporary files that gets removed after tests
42+ // Junit creates a temporary folder which can be filled with temporary files that gets removed after tests
4243 @ TempDir
4344 Path tempDir ;
4445
@@ -148,7 +149,7 @@ void test_file_that_exists_should_return_200() throws IOException {
148149 //Assert
149150 String response = fakeOutput .toString ();//Converts the captured byte stream into a String for verification
150151
151- assertTrue (response .contains ("HTTP/1.1 " + SC_OK + " OK" )); // Assert the status
152+ assertTrue (response .contains ("HTTP/1.1 200 OK" )); // Assert the status
152153 assertTrue (response .contains ("Hello Test" )); //Assert the content in the file
153154
154155 assertTrue (response .contains ("Content-Type: text/html; charset=UTF-8" )); // Verify the correct Content-type header
@@ -157,24 +158,16 @@ void test_file_that_exists_should_return_200() throws IOException {
157158
158159 @ Test
159160 void test_file_that_does_not_exists_should_return_404 () throws IOException {
160- //Arrange
161- // Pre-create the mandatory error page in the temp directory to prevent NoSuchFileException
162- Path testFile = tempDir .resolve ("pageNotFound.html" );
163- Files .writeString (testFile , "Fallback page" );
164-
165- //Using the new constructor in StaticFileHandler to reroute so the tests uses the temporary folder instead of the hardcoded www
161+ // Arrange
166162 StaticFileHandler staticFileHandler = new StaticFileHandler (tempDir .toString ());
167-
168- //Using ByteArrayOutputStream instead of Outputstream during tests to capture the servers response in memory, fake stream
169163 ByteArrayOutputStream fakeOutput = new ByteArrayOutputStream ();
170164
171- //Act
172- staticFileHandler .sendGetRequest (fakeOutput , "notExistingFile.html" ); // Request a file that clearly doesn't exist to trigger the 404 logic
173-
174- //Assert
175- String response = fakeOutput .toString ();//Converts the captured byte stream into a String for verification
165+ // Act
166+ staticFileHandler .sendGetRequest (fakeOutput , "notExistingFile.html" );
176167
177- assertTrue (response .contains ("HTTP/1.1 " + SC_NOT_FOUND + " Not Found" )); // Assert the status
168+ // Assert
169+ String response = fakeOutput .toString ();
170+ assertTrue (response .contains ("HTTP/1.1 404 Not Found" ));
178171
179172 }
180173
@@ -194,7 +187,7 @@ void test_path_traversal_should_return_403() throws IOException {
194187 // Assert
195188 String response = fakeOutput .toString ();
196189 assertFalse (response .contains ("TOP SECRET" ));
197- assertTrue (response .contains ("HTTP/1.1 " + SC_FORBIDDEN + " Forbidden" ));
190+ assertTrue (response .contains ("HTTP/1.1 403 Forbidden" ));
198191 }
199192
200193 @ ParameterizedTest
@@ -215,7 +208,7 @@ void sanitized_uris_should_return_200(String uri) throws IOException {
215208 handler .sendGetRequest (out , uri );
216209
217210 // Assert
218- assertTrue (out .toString ().contains ("HTTP/1.1 " + SC_OK + " OK" ));
211+ assertTrue (out .toString ().contains ("HTTP/1.1 200 OK" ));
219212 }
220213
221214 @ Test
@@ -231,7 +224,7 @@ void null_byte_injection_should_not_return_200() throws IOException {
231224
232225 // Assert
233226 String response = out .toString ();
234- assertFalse (response .contains ("HTTP/1.1 " + SC_OK + " OK" ));
235- assertTrue (response .contains ("HTTP/1.1 " + SC_NOT_FOUND + " Not Found" ));
227+ assertFalse (response .contains ("HTTP/1.1 200 OK" ));
228+ assertTrue (response .contains ("HTTP/1.1 404 Not Found" ));
236229 }
237230}
0 commit comments