2828 */
2929class StaticFileHandlerTest {
3030
31- private StaticFileHandler handler ;
31+ private StaticFileHandler createHandler (){
32+ return new StaticFileHandler (tempDir .toString ());
33+ }
34+
35+ private String sendRequest (String uri ) throws IOException {
36+ StaticFileHandler handler = createHandler ();
37+ ByteArrayOutputStream output = new ByteArrayOutputStream ();
38+ handler .sendGetRequest (output , uri );
39+ return output .toString ();
40+ }
41+
3242
3343 //Junit creates a temporary folder which can be filled with temporary files that gets removed after tests
3444 @ TempDir
@@ -45,60 +55,35 @@ void setUp() {
4555 void testCaching_HitOnSecondRequest () throws IOException {
4656 // Arrange
4757 Files .writeString (tempDir .resolve ("cached.html" ), "Content" );
48- StaticFileHandler handler = new StaticFileHandler (tempDir .toString ());
4958
50- // Act - Första anropet (cache miss)
51- handler . sendGetRequest ( new ByteArrayOutputStream (), "cached.html" );
59+ // Act
60+ sendRequest ( "cached.html" );
5261 int sizeAfterFirst = StaticFileHandler .getCacheStats ().entries ;
53-
54- // Act - Andra anropet (cache hit)
55- handler .sendGetRequest (new ByteArrayOutputStream (), "cached.html" );
62+ sendRequest ("cached.html" );
5663 int sizeAfterSecond = StaticFileHandler .getCacheStats ().entries ;
5764
58- // Assert - Cache ska innehålla samma entry
59- assertThat (sizeAfterFirst ).isEqualTo (1 );
60- assertThat (sizeAfterSecond ).isEqualTo (1 );
65+ // Assert
66+ assertThat (sizeAfterFirst ).isEqualTo (sizeAfterSecond ).isEqualTo (1 );
6167 }
6268
69+
6370 @ Test
6471 void testSanitization_QueryString () throws IOException {
65- // Arrange
6672 Files .writeString (tempDir .resolve ("index.html" ), "Home" );
67- StaticFileHandler handler = new StaticFileHandler (tempDir .toString ());
68- ByteArrayOutputStream output = new ByteArrayOutputStream ();
69-
70- // Act - URI med query string
71- handler .sendGetRequest (output , "index.html?foo=bar" );
72-
73- // Assert
74- assertThat (output .toString ()).contains ("HTTP/1.1 200" );
73+ assertThat (sendRequest ("index.html?foo=bar" )).contains ("HTTP/1.1 200" );
7574 }
7675
76+
7777 @ Test
7878 void testSanitization_LeadingSlash () throws IOException {
79- // Arrange
8079 Files .writeString (tempDir .resolve ("page.html" ), "Page" );
81- StaticFileHandler handler = new StaticFileHandler (tempDir .toString ());
82- ByteArrayOutputStream output = new ByteArrayOutputStream ();
83-
84- // Act
85- handler .sendGetRequest (output , "/page.html" );
86-
87- // Assert
88- assertThat (output .toString ()).contains ("HTTP/1.1 200" );
80+ assertThat (sendRequest ("/page.html" )).contains ("HTTP/1.1 200" );
8981 }
9082
83+
9184 @ Test
9285 void testSanitization_NullBytes () throws IOException {
93- // Arrange
94- StaticFileHandler handler = new StaticFileHandler (tempDir .toString ());
95- ByteArrayOutputStream output = new ByteArrayOutputStream ();
96-
97- // Act
98- handler .sendGetRequest (output , "file.html\0 ../../secret" );
99-
100- // Assert
101- assertThat (output .toString ()).contains ("HTTP/1.1 404" );
86+ assertThat (sendRequest ("file.html\0 ../../secret" )).contains ("HTTP/1.1 404" );
10287 }
10388
10489 @ Test
0 commit comments