Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit dbb0c89

Browse files
authored
Added Test Cases (#75)
* server test case * fix ports * Fix ports again * Server tests * fixes * Add context tests * Added exchange methods * session & cookie test * handler tests + #stop * Create PredicateHandlerTests.java * cleanup & planning * Update SSEHandlerTests.java * NPE (#67) Co-authored-by: Katsute <[email protected]> * typo (#68) Co-authored-by: Katsute <[email protected]> * Update versioning * Fix #69 optional, fix remove context issue (#70) Co-authored-by: Katsute <[email protected]> * Adapt temporary handler tests * Update ThrottledHandlerTests.java * Clean up * Add tests for removal hotfix * setup * SSE tests * Cleanup 🧹 * Delete file.txt * Added multipart/form-data test * Merge latest with master (#73) * NPE (#67) Co-authored-by: Katsute <[email protected]> * typo (#68) Co-authored-by: Katsute <[email protected]> * Update versioning * Fix #69 optional, fix remove context issue (#70) Co-authored-by: Katsute <[email protected]> * Update SimpleHttpExchangeImpl.java Co-authored-by: Katsute <[email protected]> * Add files / adapter tests +UTF8 warning fix * Add walking tests * Update FileHandlerTests.java * File loading option tests 🏁 Co-authored-by: Katsute <[email protected]>
1 parent d492b67 commit dbb0c89

23 files changed

+1382
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ src/LICENSE.txt
1010
.gradle
1111
# Maven
1212
target
13+
/test/

pom.xml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
<developerConnection>scm:git:[email protected]:Ktt-Development/simplehttpserver.git</developerConnection>
1818
<tag>HEAD</tag>
1919
</scm>
20-
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
</properties>
24+
2125
<build>
2226
<plugins>
2327
<plugin>
@@ -57,7 +61,27 @@
5761
</execution>
5862
</executions>
5963
</plugin>
64+
65+
<!-- tests -->
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-surefire-plugin</artifactId>
69+
<version>3.0.0-M5</version>
70+
<configuration>
71+
<useFile>false</useFile>
72+
</configuration>
73+
</plugin>
6074
</plugins>
6175
</build>
6276

77+
<dependencies>
78+
<!-- tests -->
79+
<dependency>
80+
<groupId>junit</groupId>
81+
<artifactId>junit</artifactId>
82+
<version>4.13</version>
83+
<scope>test</scope>
84+
</dependency>
85+
</dependencies>
86+
6387
</project>

src/main/java/com/kttdevelopment/simplehttpserver/handler/FileEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public final byte[] getBytes(){
140140
else
141141
try{
142142
return adapter.getBytes(file,Files.readAllBytes(file.toPath())); // read and adapt bytes
143-
}catch(final IOException ignored){
143+
}catch(final IOException e){
144144
return null;
145145
}
146146
}
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
package handlers;
2+
3+
import com.kttdevelopment.simplehttpserver.SimpleHttpServer;
4+
import com.kttdevelopment.simplehttpserver.SimpleHttpsServer;
5+
import com.kttdevelopment.simplehttpserver.handler.*;
6+
import org.junit.*;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.net.URI;
11+
import java.net.http.*;
12+
import java.nio.file.Files;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
import java.util.concurrent.ExecutionException;
16+
17+
public class FileHandlerTests {
18+
19+
@Test
20+
public void addFileTests() throws IOException{
21+
final int port = 25001;
22+
final SimpleHttpServer server = SimpleHttpServer.create(port);
23+
final FileHandler handler = new FileHandler();
24+
final String context = "";
25+
26+
final File dir = new File("src/test/resources/file");
27+
28+
final Map<File,ByteLoadingOption> files = new HashMap<>();
29+
for(final ByteLoadingOption blop : ByteLoadingOption.values())
30+
files.put(new File(dir.getPath() + '/' + blop.name() + ".txt"),blop);
31+
32+
// initial write
33+
final String init = String.valueOf(System.currentTimeMillis());
34+
files.forEach((file, loadingOption) -> {
35+
if(!file.exists() || file.delete()){
36+
try{
37+
if(file.createNewFile())
38+
Files.write(file.toPath(), init.getBytes());
39+
else
40+
Assert.fail("Failed to create new file for testing: " + file.getPath());
41+
}catch(Exception e){
42+
Assert.fail("Failed to create new file for testing: " + file.getPath());
43+
}
44+
45+
handler.addFile(file, loadingOption);
46+
}else{
47+
Assert.fail("Failed to clear file for testing: " + file.getPath());
48+
}
49+
});
50+
51+
server.createContext(context,handler);
52+
server.start();
53+
54+
files.forEach((file, loadingOption) -> {
55+
final String url = "http://localhost:" + port + context + '/' + file.getName();
56+
HttpRequest request = HttpRequest.newBuilder()
57+
.uri(URI.create(url))
58+
.build();
59+
60+
try{
61+
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
62+
.thenApply(HttpResponse::body).get();
63+
64+
Assert.assertEquals("Client data did not match server data for " + file.getName(),init,response);
65+
}catch(InterruptedException | ExecutionException e){
66+
Assert.fail("Failed to read context of " + file.getName());
67+
}
68+
69+
// second write
70+
71+
final String after = String.valueOf(System.currentTimeMillis());
72+
try{
73+
Files.write(file.toPath(), after.getBytes());
74+
}catch(Exception e){
75+
Assert.fail("Failed to second write file " + file.getPath());
76+
}
77+
78+
try{
79+
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
80+
.thenApply(HttpResponse::body).get();
81+
82+
Assert.assertEquals("Client data did not match server data for " + file.getName(),loadingOption == ByteLoadingOption.PRELOAD ? init : after,response);
83+
}catch(InterruptedException | ExecutionException e){
84+
Assert.fail("Failed to read context " + file.getName());
85+
}
86+
});
87+
88+
server.stop();
89+
}
90+
91+
@Test
92+
public void addFilesTests() throws IOException, ExecutionException, InterruptedException{ // run adapter tests here
93+
final int port = 25002;
94+
final SimpleHttpServer server = SimpleHttpServer.create(port);
95+
final String override = "override";
96+
final String context = "", altContext = "/alt";
97+
final FileHandlerAdapter adapter = new FileHandlerAdapter() {
98+
@Override
99+
public byte[] getBytes(final File file, final byte[] bytes){
100+
return override.getBytes();
101+
}
102+
103+
@Override
104+
public String getName(final File file){
105+
return file.getName().substring(0,file.getName().lastIndexOf('.'));
106+
}
107+
};
108+
final FileHandler handler = new FileHandler(adapter);
109+
110+
final File[] files = new File[]{
111+
new File("src/test/resources/files/test.txt"),
112+
new File("src/test/resources/files/test2.txt")
113+
};
114+
115+
handler.addFiles(files);
116+
handler.addFiles(altContext,files);
117+
118+
server.createContext(context,handler);
119+
120+
server.start();
121+
122+
for(final File file : files){
123+
String url = "http://localhost:" + port + context;
124+
HttpRequest request = HttpRequest.newBuilder()
125+
.uri(URI.create(url + "/" + file.getName().substring(0,file.getName().lastIndexOf('.'))))
126+
.build();
127+
128+
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
129+
.thenApply(HttpResponse::body).get();
130+
131+
Assert.assertEquals("Adapter bytes did not match client received bytes",override,response);
132+
133+
// alt
134+
135+
String altUrl = "http://localhost:" + port + altContext;
136+
request = HttpRequest.newBuilder()
137+
.uri(URI.create(altUrl + "/" + file.getName().substring(0,file.getName().lastIndexOf('.'))))
138+
.build();
139+
140+
response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
141+
.thenApply(HttpResponse::body).get();
142+
143+
Assert.assertEquals("Adapter alt context bytes did not match client received bytes",override,response);
144+
}
145+
146+
server.stop();
147+
148+
}
149+
150+
@Test
151+
public void addDirectoryTestsNoWalk() throws IOException, InterruptedException{
152+
final int port = 25003;
153+
final SimpleHttpServer server = SimpleHttpServer.create(port);
154+
final FileHandler handler = new FileHandler();
155+
final String contextNoName = "alt";
156+
final String contextWName = "altn";
157+
final String dirNewName = "dirName";
158+
159+
final String testRealFile = "test.txt", testFileContent = "readme";
160+
final String noReadDir = "dirnoread";
161+
final String noReadFile = "innerNoRead.txt";
162+
163+
final File noWalk = new File("src/test/resources/directory/nowalk");
164+
165+
final String context = "";
166+
167+
handler.addDirectory(noWalk); // test file & directory read
168+
handler.addDirectory(contextNoName,noWalk);
169+
handler.addDirectory(noWalk,dirNewName);
170+
handler.addDirectory(contextWName,noWalk,dirNewName);
171+
172+
server.createContext(context,handler);
173+
server.start();
174+
175+
final String[] validPathsToTest = { // valid reads
176+
noWalk.getName() + '/' + testRealFile,
177+
contextNoName + '/' + noWalk.getName() + '/' + testRealFile,
178+
dirNewName + '/' + testRealFile,
179+
contextWName + '/' + dirNewName + '/' + testRealFile
180+
};
181+
182+
for(final String path : validPathsToTest){
183+
String url = "http://localhost:" + port + '/' + path;
184+
HttpRequest request = HttpRequest.newBuilder()
185+
.uri(URI.create(url))
186+
.build();
187+
188+
try{
189+
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
190+
.thenApply(HttpResponse::body).get();
191+
192+
Assert.assertNotNull("Client did not find data for " + path,response);
193+
Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response);
194+
}catch(ExecutionException e){
195+
Assert.fail("Client did not find data for " + path);
196+
}
197+
}
198+
199+
final String[] invalidPathsToTest = {
200+
noWalk.getName() + '/' + noReadDir,
201+
noWalk.getName() + '/' + noReadDir + '/' + noReadFile
202+
};
203+
204+
for(final String path : invalidPathsToTest){
205+
String url = "http://localhost:" + port + '/' + path;
206+
HttpRequest request = HttpRequest.newBuilder()
207+
.uri(URI.create(url))
208+
.build();
209+
210+
Exception exception = null;
211+
try{
212+
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
213+
.thenApply(HttpResponse::body).get();
214+
215+
Assert.assertNull("Client found data for blocked path " + path,response);
216+
}catch(ExecutionException e){
217+
exception = e;
218+
}
219+
Assert.assertNotNull("Client found data for blocked path",exception);
220+
}
221+
222+
server.stop();
223+
}
224+
225+
@Test
226+
public void addDirectoryTestsWalk() throws IOException, InterruptedException{
227+
final int port = 25004;
228+
final SimpleHttpServer server = SimpleHttpServer.create(port);
229+
final FileHandler handler = new FileHandler();
230+
231+
final String testRealFile = "test.txt", testFileContent = "readme";
232+
final String readDir = "dirnoread";
233+
final String innerFile = "innerNoRead.txt";
234+
235+
final File noWalk = new File("src/test/resources/directory/nowalk");
236+
237+
final String context = "";
238+
239+
// handler.addDirectory(noWalk,true);
240+
handler.addDirectory(noWalk, ByteLoadingOption.LIVELOAD,true); // todo: def liveload for walk param
241+
242+
server.createContext(context,handler);
243+
server.start();
244+
245+
final String[] validPathsToTest = { // valid reads
246+
noWalk.getName() + '/' + testRealFile,
247+
noWalk.getName() + '/' + readDir + '/' + innerFile
248+
};
249+
250+
for(final String path : validPathsToTest){
251+
String url = "http://localhost:" + port + '/' + path;
252+
HttpRequest request = HttpRequest.newBuilder()
253+
.uri(URI.create(url))
254+
.build();
255+
256+
try{
257+
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
258+
.thenApply(HttpResponse::body).get();
259+
260+
Assert.assertNotNull("Client did not find data for " + path,response);
261+
Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response);
262+
}catch(ExecutionException e){
263+
Assert.fail("Client did not find data for " + path);
264+
}
265+
}
266+
267+
final String[] invalidPathsToTest = {
268+
noWalk.getName() + '/' + readDir
269+
};
270+
271+
for(final String path : invalidPathsToTest){
272+
String url = "http://localhost:" + port + '/' + path;
273+
HttpRequest request = HttpRequest.newBuilder()
274+
.uri(URI.create(url))
275+
.build();
276+
277+
Exception exception = null;
278+
try{
279+
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
280+
.thenApply(HttpResponse::body).get();
281+
282+
Assert.assertNull("Client found data for blocked path " + path,response);
283+
}catch(ExecutionException e){
284+
exception = e;
285+
}
286+
Assert.assertNotNull("Client found data for blocked path",exception);
287+
}
288+
289+
server.stop();
290+
}
291+
292+
}

0 commit comments

Comments
 (0)