Skip to content

Commit f5b2f44

Browse files
committed
kom up problem när github kör ska ha fixat det tror jag
1 parent bb0d51a commit f5b2f44

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

src/test/java/org/example/StaticFileHandlerTest.java

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@
1313
import static org.junit.jupiter.api.Assertions.*;
1414
import static org.assertj.core.api.Assertions.assertThat;
1515

16-
1716
/**
1817
* Unit test class for verifying the behavior of the StaticFileHandler class.
18+
*
1919
* This test class ensures that StaticFileHandler correctly handles GET requests
2020
* for static files, including both cases where the requested file exists and
2121
* where it does not. Temporary directories and files are utilized in tests to
2222
* ensure no actual file system dependencies during test execution.
23+
*
2324
* Key functional aspects being tested include:
2425
* - Correct response status code and content for an existing file.
2526
* - Correct response status code and fallback behavior for a missing file.
2627
*/
2728
class StaticFileHandlerTest {
2829

29-
private StaticFileHandler createHandler(){
30+
private StaticFileHandler createHandler() {
3031
return new StaticFileHandler(tempDir.toString());
3132
}
3233

@@ -37,9 +38,6 @@ private String sendRequest(String uri) throws IOException {
3738
return output.toString();
3839
}
3940

40-
41-
//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
4341
@TempDir
4442
Path tempDir;
4543

@@ -49,7 +47,6 @@ void setUp() {
4947
StaticFileHandler.clearCache();
5048
}
5149

52-
5350
@Test
5451
void testCaching_HitOnSecondRequest() throws IOException {
5552
// Arrange
@@ -65,27 +62,23 @@ void testCaching_HitOnSecondRequest() throws IOException {
6562
assertThat(sizeAfterFirst).isEqualTo(sizeAfterSecond).isEqualTo(1);
6663
}
6764

68-
6965
@Test
7066
void testSanitization_QueryString() throws IOException {
7167
Files.writeString(tempDir.resolve("index.html"), "Home");
7268
assertThat(sendRequest("index.html?foo=bar")).contains("HTTP/1.1 200");
7369
}
7470

75-
7671
@Test
7772
void testSanitization_LeadingSlash() throws IOException {
7873
Files.writeString(tempDir.resolve("page.html"), "Page");
7974
assertThat(sendRequest("/page.html")).contains("HTTP/1.1 200");
8075
}
8176

82-
8377
@Test
8478
void testSanitization_NullBytes() throws IOException {
8579
assertThat(sendRequest("file.html\0../../secret")).contains("HTTP/1.1 404");
8680
}
8781

88-
8982
@Test
9083
void testConcurrent_MultipleReads() throws InterruptedException, IOException {
9184
// Arrange
@@ -105,7 +98,7 @@ void testConcurrent_MultipleReads() throws InterruptedException, IOException {
10598
for (int j = 0; j < 50; j++) {
10699
ByteArrayOutputStream out = new ByteArrayOutputStream();
107100
handler.sendGetRequest(out, "shared.html");
108-
assertThat(out.toString()).contains("200");
101+
assertThat(out.toString()).contains("HTTP/1.1 200");
109102
}
110103
} catch (IOException e) {
111104
throw new RuntimeException(e);
@@ -119,41 +112,38 @@ void testConcurrent_MultipleReads() throws InterruptedException, IOException {
119112
});
120113
threads[i].start();
121114
}
115+
122116
// Vänta på alla trådar
123117
for (Thread t : threads) {
124118
t.join();
125119
}
120+
126121
// Assert - Check if any child thread had assertion failures
127122
if (assertionErrors[0] != null) {
128123
throw assertionErrors[0];
129-
} // Assert - Cache ska bara ha EN entry
124+
}
125+
126+
// Assert - Cache ska bara ha EN entry
130127
assertThat(StaticFileHandler.getCacheStats().entries).isEqualTo(1);
131128
}
132129

133-
134130
@Test
135131
void test_file_that_exists_should_return_200() throws IOException {
136-
//Arrange
137-
Path testFile = tempDir.resolve("test.html"); // Defines the path in the temp directory
138-
Files.writeString(testFile, "Hello Test"); // Creates a text in that file
132+
// Arrange
133+
Path testFile = tempDir.resolve("test.html");
134+
Files.writeString(testFile, "Hello Test");
139135

140-
//Using the new constructor in StaticFileHandler to reroute so the tests uses the temporary folder instead of the hardcoded www
141136
StaticFileHandler staticFileHandler = new StaticFileHandler(tempDir.toString());
142-
143-
//Using ByteArrayOutputStream instead of Outputstream during tests to capture the servers response in memory, fake stream
144137
ByteArrayOutputStream fakeOutput = new ByteArrayOutputStream();
145138

146-
//Act
147-
staticFileHandler.sendGetRequest(fakeOutput, "test.html"); //Get test.html and write the answer to fakeOutput
148-
149-
//Assert
150-
String response = fakeOutput.toString();//Converts the captured byte stream into a String for verification
151-
152-
assertTrue(response.contains("HTTP/1.1 200 OK")); // Assert the status
153-
assertTrue(response.contains("Hello Test")); //Assert the content in the file
154-
155-
assertTrue(response.contains("Content-Type: text/html; charset=UTF-8")); // Verify the correct Content-type header
139+
// Act
140+
staticFileHandler.sendGetRequest(fakeOutput, "test.html");
156141

142+
// Assert
143+
String response = fakeOutput.toString();
144+
assertTrue(response.contains("HTTP/1.1 200 OK"));
145+
assertTrue(response.contains("Hello Test"));
146+
assertTrue(response.contains("Content-Type: text/html; charset=UTF-8"));
157147
}
158148

159149
@Test
@@ -168,14 +158,13 @@ void test_file_that_does_not_exists_should_return_404() throws IOException {
168158
// Assert
169159
String response = fakeOutput.toString();
170160
assertTrue(response.contains("HTTP/1.1 404 Not Found"));
171-
172161
}
173162

174163
@Test
175164
void test_path_traversal_should_return_403() throws IOException {
176165
// Arrange
177166
Path secret = tempDir.resolve("secret.txt");
178-
Files.writeString(secret,"TOP SECRET");
167+
Files.writeString(secret, "TOP SECRET");
179168
Path webRoot = tempDir.resolve("www");
180169
Files.createDirectories(webRoot);
181170
StaticFileHandler handler = new StaticFileHandler(webRoot.toString());

0 commit comments

Comments
 (0)