-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestTest.java
73 lines (58 loc) · 2.06 KB
/
RequestTest.java
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
package clienttests;
import edu.nju.http.client.HttpClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import edu.nju.http.server.HttpServer;
import util.Util;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class RequestTest {
static HttpServer server;
static CompletableFuture<Void> future;
static BufferedReader br;
@BeforeEach
void setUp() throws IOException {
server = new HttpServer();
future = CompletableFuture.runAsync(() -> server.launch(true, 10000));
}
@Test
@DisplayName("GET Method test")
public void getTest() throws IOException {
var hrm = new HttpClient().get("/test");
String startLine = hrm.flatMessage().split("\r\n")[0];
assertEquals("HTTP/1.1 200 OK", startLine);
}
@Test
@DisplayName("POST Method test")
public void postTest() throws IOException {
var hrm = new HttpClient().post("/register?name=hhh&password=996", null);
String startLine = hrm.flatMessage().split("\r\n")[0];
assertEquals("HTTP/1.1 201 Created", startLine);
}
@Test
@DisplayName("Redirect Test")
public void redirectTest() throws IOException {
var hrm = new HttpClient("127.0.0.1", 8080, true)
.get("/moved");
String body = hrm.getBodyAsString();
assertEquals("You got the place!!!", body);
}
@Test
@DisplayName("304 Cache Test")
public void cacheTest() throws IOException, InterruptedException {
HttpClient client = new HttpClient();
client.get("/OS/2022/");
client = new HttpClient();
var hrm = client.get("/OS/2022/");
assert "304".equals(hrm.getStartLineAndHeaders().split(" ")[1]);
Util.testAndCompareWebPage("127.0.0.1", 8080, "/OS/2022/");
}
@AfterEach
void cleanUp() {
server.shutdown();
}
}