This repository was archived by the owner on Jul 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandlerTests.java
More file actions
380 lines (300 loc) · 14.6 KB
/
Copy pathFileHandlerTests.java
File metadata and controls
380 lines (300 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package handlers;
import com.kttdevelopment.simplehttpserver.SimpleHttpServer;
import com.kttdevelopment.simplehttpserver.handler.*;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.http.*;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
public class FileHandlerTests {
@SuppressWarnings("ResultOfMethodCallIgnored")
@Test
public void addFileTests() throws IOException{
final int port = 25001;
final SimpleHttpServer server = SimpleHttpServer.create(port);
final FileHandler handler = new FileHandler();
final String context = "";
final File dir = new File("src/test/resources/file");
final Map<File,ByteLoadingOption> files = new HashMap<>();
for(final ByteLoadingOption blop : ByteLoadingOption.values())
files.put(new File(dir.getPath() + '/' + blop.name() + ".txt"),blop);
// initial write
final String init = String.valueOf(System.currentTimeMillis());
files.forEach((file, loadingOption) -> {
if(!file.exists() || file.delete()){
try{
if((file.getParentFile().mkdirs() || file.getParentFile().isDirectory()) && file.createNewFile())
Files.write(file.toPath(), init.getBytes());
else
Assert.fail("Failed to create new file for testing: " + file.getPath());
}catch(final Exception ignored){
Assert.fail("Failed to create new file for testing: " + file.getPath());
}
handler.addFile(file, loadingOption);
}else{
Assert.fail("Failed to clear file for testing: " + file.getPath());
}
});
server.createContext(context,handler);
server.start();
files.forEach((file, loadingOption) -> {
final String url = "http://localhost:" + port + context + '/' + file.getName();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
try{
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body).get();
Assert.assertEquals("Client data did not match server data for " + file.getName(),init,response);
}catch(final InterruptedException | ExecutionException ignored){
Assert.fail("Failed to read context of " + file.getName());
}
// second write
final String after = String.valueOf(System.currentTimeMillis());
try{
Files.write(file.toPath(), after.getBytes());
}catch(final Exception ignored){
Assert.fail("Failed to second write file " + file.getPath());
}
try{
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body).get();
Assert.assertEquals("Client data did not match server data for " + file.getName(),loadingOption == ByteLoadingOption.PRELOAD ? init : after,response);
}catch(final InterruptedException | ExecutionException ignored){
Assert.fail("Failed to read context " + file.getName());
}
});
files.forEach((file, loadingOption) -> file.delete());
server.stop();
}
@Test
public void addFilesTests() throws IOException, ExecutionException, InterruptedException{ // run adapter tests here
final int port = 25002;
final SimpleHttpServer server = SimpleHttpServer.create(port);
final String override = "override";
final String context = "", altContext = "/alt";
final FileHandlerAdapter adapter = new FileHandlerAdapter() {
@Override
public byte[] getBytes(final File file, final byte[] bytes){
return override.getBytes();
}
@Override
public String getName(final File file){
return file.getName().substring(0,file.getName().lastIndexOf('.'));
}
};
final FileHandler handler = new FileHandler(adapter);
final File[] files = new File[]{
new File("src/test/resources/files/test.txt"),
new File("src/test/resources/files/test2.txt")
};
handler.addFiles(files);
handler.addFiles(altContext,files);
server.createContext(context,handler);
server.start();
for(final File file : files){
String url = "http://localhost:" + port + context;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url + "/" + file.getName().substring(0,file.getName().lastIndexOf('.'))))
.build();
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body).get();
Assert.assertEquals("Adapter bytes did not match client received bytes",override,response);
// alt
String altUrl = "http://localhost:" + port + altContext;
request = HttpRequest.newBuilder()
.uri(URI.create(altUrl + "/" + file.getName().substring(0,file.getName().lastIndexOf('.'))))
.build();
response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body).get();
Assert.assertEquals("Adapter alt context bytes did not match client received bytes",override,response);
}
server.stop();
}
@Test
public void addDirectoryTestsNoWalk() throws IOException, InterruptedException{
final int port = 25003;
final SimpleHttpServer server = SimpleHttpServer.create(port);
final FileHandler handler = new FileHandler();
final String contextNoName = "alt";
final String contextWName = "altn";
final String dirNewName = "dirName";
final String testRealFile = "test.txt", testFileContent = "readme";
final String noReadDir = "dirnoread";
final String noReadFile = "innerNoRead.txt";
final File noWalk = new File("src/test/resources/directory/nowalk");
final String context = "";
handler.addDirectory(noWalk); // test file & directory read
handler.addDirectory(contextNoName,noWalk);
handler.addDirectory(noWalk,dirNewName);
handler.addDirectory(contextWName,noWalk,dirNewName);
server.createContext(context,handler);
server.start();
final String[] validPathsToTest = { // valid reads
noWalk.getName() + '/' + testRealFile,
contextNoName + '/' + noWalk.getName() + '/' + testRealFile,
dirNewName + '/' + testRealFile,
contextWName + '/' + dirNewName + '/' + testRealFile
};
for(final String path : validPathsToTest){
String url = "http://localhost:" + port + '/' + path;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
try{
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body).get();
Assert.assertNotNull("Client did not find data for " + path,response);
Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response);
}catch(final ExecutionException ignored){
Assert.fail("Client did not find data for " + path);
}
}
final String[] invalidPathsToTest = {
noWalk.getName() + '/' + noReadDir,
noWalk.getName() + '/' + noReadDir + '/' + noReadFile
};
for(final String path : invalidPathsToTest){
String url = "http://localhost:" + port + '/' + path;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
Exception exception = null;
try{
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body).get();
Assert.assertNull("Client found data for blocked path " + path,response);
}catch(final ExecutionException e){
exception = e;
}
Assert.assertNotNull("Client found data for blocked path",exception);
}
server.stop();
}
@Test
public void addDirectoryTestsNoWalkAdapter() throws IOException, InterruptedException{
final int port = 25022;
final SimpleHttpServer server = SimpleHttpServer.create(port);
final FileHandlerAdapter adapter = new FileHandlerAdapter() {
@Override
public byte[] getBytes(final File file, final byte[] bytes){
return bytes;
}
@Override
public String getName(final File file){
return file.getName().substring(0,file.getName().lastIndexOf('.'));
}
};
final FileHandler handler = new FileHandler(adapter);
final String contextNoName = "alt";
final String contextWName = "altn";
final String dirNewName = "dirName";
final String testRealFile = "test", testFileContent = "readme";
final String noReadDir = "dirnoread";
final String noReadFile = "innerNoRead";
final File noWalk = new File("src/test/resources/directory/nowalk");
final String context = "";
handler.addDirectory(noWalk); // test file & directory read
handler.addDirectory(contextNoName,noWalk);
handler.addDirectory(noWalk,dirNewName);
handler.addDirectory(contextWName,noWalk,dirNewName);
server.createContext(context,handler);
server.start();
final String[] validPathsToTest = { // valid reads
noWalk.getName() + '/' + testRealFile,
contextNoName + '/' + noWalk.getName() + '/' + testRealFile,
dirNewName + '/' + testRealFile,
contextWName + '/' + dirNewName + '/' + testRealFile
};
for(final String path : validPathsToTest){
String url = "http://localhost:" + port + '/' + path;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
try{
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body).get();
Assert.assertNotNull("Client did not find data for " + path,response);
Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response);
}catch(final ExecutionException ignored){
Assert.fail("Client did not find data for " + path);
}
}
final String[] invalidPathsToTest = {
noWalk.getName() + '/' + noReadDir,
noWalk.getName() + '/' + noReadDir + '/' + noReadFile
};
for(final String path : invalidPathsToTest){
String url = "http://localhost:" + port + '/' + path;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
Exception exception = null;
try{
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body).get();
Assert.assertNull("Client found data for blocked path " + path,response);
}catch(final ExecutionException e){
exception = e;
}
Assert.assertNotNull("Client found data for blocked path",exception);
}
server.stop();
}
@Test
public void addDirectoryTestsWalk() throws IOException, InterruptedException{
final int port = 25004;
final SimpleHttpServer server = SimpleHttpServer.create(port);
final FileHandler handler = new FileHandler();
final String testRealFile = "test.txt", testFileContent = "readme";
final String readDir = "dirnoread";
final String innerFile = "innerNoRead.txt";
final File noWalk = new File("src/test/resources/directory/nowalk");
final String context = "";
handler.addDirectory(noWalk,true);
server.createContext(context,handler);
server.start();
final String[] validPathsToTest = { // valid reads
noWalk.getName() + '/' + testRealFile,
noWalk.getName() + '/' + readDir + '/' + innerFile
};
for(final String path : validPathsToTest){
String url = "http://localhost:" + port + '/' + path;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
try{
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body).get();
Assert.assertNotNull("Client did not find data for " + path,response);
Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response);
}catch(final ExecutionException ignored){
Assert.fail("Client did not find data for " + path);
}
}
final String[] invalidPathsToTest = {
noWalk.getName() + '/' + readDir
};
for(final String path : invalidPathsToTest){
String url = "http://localhost:" + port + '/' + path;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
Exception exception = null;
try{
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body).get();
Assert.assertNull("Client found data for blocked path " + path,response);
}catch(final ExecutionException e){
exception = e;
}
Assert.assertNotNull("Client found data for blocked path",exception);
}
server.stop();
}
}