-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoginSystemTests.java
More file actions
64 lines (53 loc) · 2.15 KB
/
LoginSystemTests.java
File metadata and controls
64 lines (53 loc) · 2.15 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
package loginsystemtests;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import edu.nju.http.server.HttpServer;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class LoginSystemTests {
final static String NAME = "桐生一馬";
final static String PASS = "DameDane";
static HttpServer server;
static CompletableFuture<Void> future;
static String cookie = null;
@BeforeAll
static void startUp() throws IOException, InterruptedException {
server = new HttpServer();
future = CompletableFuture.runAsync(() -> server.launch(true, 10000));
}
@Test
// @Disabled
@DisplayName("Register Test")
public void register() throws IOException, InterruptedException {
var pb = new ProcessBuilder();
pb.command("curl", "-v", "--request", "POST", "http://127.0.0.1:8080/register",
"--header", "Content-Type: application/x-www-form-urlencoded",
"--data-urlencode", "name=%s&password=%s".formatted(NAME, PASS));
var p = pb.start();
p.waitFor();
assertEquals("Registered successfully", p.inputReader().readLine());
String line;
while ((line = p.errorReader().readLine()) != null) {
if (line.startsWith("< Set-Cookie:")) {
cookie = line.replaceFirst("< Set-Cookie: ", "");
break;
}
}
assertNotNull(cookie);
pb.command("curl", "-v", "--request", "GET", "http://127.0.0.1:8080/status",
"--header", "Cookie: " + cookie);
p = pb.start();
p.waitFor();
assertEquals("You've logged in as %s".formatted(NAME), p.inputReader().readLine());
}
@AfterAll
static void cleanUp() throws ExecutionException, InterruptedException {
server.shutdown();
}
}