@@ -39,7 +39,7 @@ public void addFileTests() throws IOException{
39
39
Files .write (file .toPath (), init .getBytes ());
40
40
else
41
41
Assert .fail ("Failed to create new file for testing: " + file .getPath ());
42
- }catch (Exception e ){
42
+ }catch (final Exception ignored ){
43
43
Assert .fail ("Failed to create new file for testing: " + file .getPath ());
44
44
}
45
45
@@ -63,7 +63,7 @@ public void addFileTests() throws IOException{
63
63
.thenApply (HttpResponse ::body ).get ();
64
64
65
65
Assert .assertEquals ("Client data did not match server data for " + file .getName (),init ,response );
66
- }catch (InterruptedException | ExecutionException e ){
66
+ }catch (final InterruptedException | ExecutionException ignored ){
67
67
Assert .fail ("Failed to read context of " + file .getName ());
68
68
}
69
69
@@ -72,7 +72,7 @@ public void addFileTests() throws IOException{
72
72
final String after = String .valueOf (System .currentTimeMillis ());
73
73
try {
74
74
Files .write (file .toPath (), after .getBytes ());
75
- }catch (Exception e ){
75
+ }catch (final Exception ignored ){
76
76
Assert .fail ("Failed to second write file " + file .getPath ());
77
77
}
78
78
@@ -81,7 +81,7 @@ public void addFileTests() throws IOException{
81
81
.thenApply (HttpResponse ::body ).get ();
82
82
83
83
Assert .assertEquals ("Client data did not match server data for " + file .getName (),loadingOption == ByteLoadingOption .PRELOAD ? init : after ,response );
84
- }catch (InterruptedException | ExecutionException e ){
84
+ }catch (final InterruptedException | ExecutionException ignored ){
85
85
Assert .fail ("Failed to read context " + file .getName ());
86
86
}
87
87
});
@@ -194,7 +194,7 @@ public void addDirectoryTestsNoWalk() throws IOException, InterruptedException{
194
194
195
195
Assert .assertNotNull ("Client did not find data for " + path ,response );
196
196
Assert .assertEquals ("Client data did not match server data for " + path ,testFileContent ,response );
197
- }catch (ExecutionException e ){
197
+ }catch (final ExecutionException ignored ){
198
198
Assert .fail ("Client did not find data for " + path );
199
199
}
200
200
}
@@ -216,7 +216,93 @@ public void addDirectoryTestsNoWalk() throws IOException, InterruptedException{
216
216
.thenApply (HttpResponse ::body ).get ();
217
217
218
218
Assert .assertNull ("Client found data for blocked path " + path ,response );
219
- }catch (ExecutionException e ){
219
+ }catch (final ExecutionException e ){
220
+ exception = e ;
221
+ }
222
+ Assert .assertNotNull ("Client found data for blocked path" ,exception );
223
+ }
224
+
225
+ server .stop ();
226
+ }
227
+
228
+ @ Test
229
+ public void addDirectoryTestsNoWalkAdapter () throws IOException , InterruptedException {
230
+ final int port = 25022 ;
231
+ final SimpleHttpServer server = SimpleHttpServer .create (port );
232
+ final FileHandlerAdapter adapter = new FileHandlerAdapter () {
233
+ @ Override
234
+ public byte [] getBytes (final File file , final byte [] bytes ){
235
+ return bytes ;
236
+ }
237
+
238
+ @ Override
239
+ public String getName (final File file ){
240
+ return file .getName ().substring (0 ,file .getName ().lastIndexOf ('.' ));
241
+ }
242
+ };
243
+ final FileHandler handler = new FileHandler (adapter );
244
+ final String contextNoName = "alt" ;
245
+ final String contextWName = "altn" ;
246
+ final String dirNewName = "dirName" ;
247
+
248
+ final String testRealFile = "test" , testFileContent = "readme" ;
249
+ final String noReadDir = "dirnoread" ;
250
+ final String noReadFile = "innerNoRead" ;
251
+
252
+ final File noWalk = new File ("src/test/resources/directory/nowalk" );
253
+
254
+ final String context = "" ;
255
+
256
+ handler .addDirectory (noWalk ); // test file & directory read
257
+ handler .addDirectory (contextNoName ,noWalk );
258
+ handler .addDirectory (noWalk ,dirNewName );
259
+ handler .addDirectory (contextWName ,noWalk ,dirNewName );
260
+
261
+ server .createContext (context ,handler );
262
+ server .start ();
263
+
264
+ final String [] validPathsToTest = { // valid reads
265
+ noWalk .getName () + '/' + testRealFile ,
266
+ contextNoName + '/' + noWalk .getName () + '/' + testRealFile ,
267
+ dirNewName + '/' + testRealFile ,
268
+ contextWName + '/' + dirNewName + '/' + testRealFile
269
+ };
270
+
271
+ for (final String path : validPathsToTest ){
272
+ String url = "http://localhost:" + port + '/' + path ;
273
+ HttpRequest request = HttpRequest .newBuilder ()
274
+ .uri (URI .create (url ))
275
+ .build ();
276
+
277
+ try {
278
+ String response = HttpClient .newHttpClient ().sendAsync (request , HttpResponse .BodyHandlers .ofString ())
279
+ .thenApply (HttpResponse ::body ).get ();
280
+
281
+ Assert .assertNotNull ("Client did not find data for " + path ,response );
282
+ Assert .assertEquals ("Client data did not match server data for " + path ,testFileContent ,response );
283
+ }catch (final ExecutionException ignored ){
284
+ Assert .fail ("Client did not find data for " + path );
285
+ }
286
+ }
287
+
288
+ final String [] invalidPathsToTest = {
289
+ noWalk .getName () + '/' + noReadDir ,
290
+ noWalk .getName () + '/' + noReadDir + '/' + noReadFile
291
+ };
292
+
293
+ for (final String path : invalidPathsToTest ){
294
+ String url = "http://localhost:" + port + '/' + path ;
295
+ HttpRequest request = HttpRequest .newBuilder ()
296
+ .uri (URI .create (url ))
297
+ .build ();
298
+
299
+ Exception exception = null ;
300
+ try {
301
+ String response = HttpClient .newHttpClient ().sendAsync (request , HttpResponse .BodyHandlers .ofString ())
302
+ .thenApply (HttpResponse ::body ).get ();
303
+
304
+ Assert .assertNull ("Client found data for blocked path " + path ,response );
305
+ }catch (final ExecutionException e ){
220
306
exception = e ;
221
307
}
222
308
Assert .assertNotNull ("Client found data for blocked path" ,exception );
@@ -261,7 +347,7 @@ public void addDirectoryTestsWalk() throws IOException, InterruptedException{
261
347
262
348
Assert .assertNotNull ("Client did not find data for " + path ,response );
263
349
Assert .assertEquals ("Client data did not match server data for " + path ,testFileContent ,response );
264
- }catch (ExecutionException e ){
350
+ }catch (final ExecutionException ignored ){
265
351
Assert .fail ("Client did not find data for " + path );
266
352
}
267
353
}
@@ -282,7 +368,7 @@ public void addDirectoryTestsWalk() throws IOException, InterruptedException{
282
368
.thenApply (HttpResponse ::body ).get ();
283
369
284
370
Assert .assertNull ("Client found data for blocked path " + path ,response );
285
- }catch (ExecutionException e ){
371
+ }catch (final ExecutionException e ){
286
372
exception = e ;
287
373
}
288
374
Assert .assertNotNull ("Client found data for blocked path" ,exception );
0 commit comments