-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppTest.java
More file actions
58 lines (45 loc) · 1.53 KB
/
Copy pathAppTest.java
File metadata and controls
58 lines (45 loc) · 1.53 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
package com.example;
import static org.junit.Assert.assertEquals;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import io.github.ghosthack.turismo.Turismo;
public class AppTest {
@BeforeClass
public static void startServer() {
App.routes();
Turismo.start(0); // random free port
}
@AfterClass
public static void stopServer() {
Turismo.reset();
}
@Test
public void hello() throws Exception {
assertEquals("Hello World!", get("/hello"));
}
@Test
public void pathParam() throws Exception {
assertEquals("User 42", get("/users/42"));
}
@Test
public void searchEscapesQuery() throws Exception {
String q = URLEncoder.encode("<script>alert(1)</script>", StandardCharsets.UTF_8);
assertEquals("<p>Your search query was: <script>alert(1)</script></p>",
get("/search?q=" + q));
}
@Test
public void json() throws Exception {
assertEquals("{\"id\":\"7\"}", get("/api/users/7"));
}
private static String get(String path) throws Exception {
HttpURLConnection conn = (HttpURLConnection) URI
.create("http://localhost:" + Turismo.port() + path).toURL().openConnection();
assertEquals(200, conn.getResponseCode());
return new String(conn.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
}
}