forked from storytellersoftware/java-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlerTest.java
More file actions
70 lines (56 loc) · 1.72 KB
/
HandlerTest.java
File metadata and controls
70 lines (56 loc) · 1.72 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
package tests;
import static org.junit.Assert.fail;
import httpserver.HTTPException;
import httpserver.HTTPHandler;
import httpserver.HTTPRequest;
import httpserver.HTTPResponse;
import httpserver.HTTPRouter;
import org.junit.Test;
public class HandlerTest extends HTTPHandler {
public HandlerTest() throws HTTPException {
addGET("/showHeaders", "showHeaders");
addGET("/hello", "sayHello");
addGET("/hello/{String}", "sayHello");
addGET("/hello/{String}/{String}", "sayHello");
addGET("/thisAndMore/{String...}", "thisAndMore");
}
public void showHeaders(HTTPResponse resp, HTTPRequest req) {
StringBuilder b = new StringBuilder();
for (String key: req.getHeaders().keySet()) {
b.append("\n\t");
b.append(key);
b.append(":\t");
b.append(req.getHeaders().get(key));
b.append("\n");
}
resp.setBody("Headers:" + b.toString());
}
public void sayHello(HTTPResponse resp) {
resp.setBody("Hello World!");
}
public void sayHello(HTTPResponse resp, String name) {
resp.setBody("Hello " + name + "!");
}
public void sayHello(HTTPResponse resp, String first, String last) {
resp.setBody("Hello " + first + " " + last + "!");
}
public void thisAndMore(HTTPResponse resp, String... paths) {
StringBuilder b = new StringBuilder("You requested `");
for (String p: paths) {
b.append("/");
b.append(p);
}
b.append("`");
resp.setBody(b.toString());
}
@Test
public void testHandlerCreation() {
try {
HTTPRouter f = new HTTPRouter();
f.addHandler("/", new HandlerTest());
} catch (HTTPException e) {
e.printStackTrace();
fail("Couldn't create new handler...");
}
}
}