-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainTrain.java
More file actions
135 lines (114 loc) · 4.66 KB
/
Copy pathMainTrain.java
File metadata and controls
135 lines (114 loc) · 4.66 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package test;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import test.RequestParser.RequestInfo;
public class MainTrain { // RequestParser
private static void testParseRequest() {
// Test data
String request = "GET /api/resource?id=123&name=test HTTP/1.1\n" +
"Host: example.com\n" +
"Content-Length: 5\n"+
"\n" +
"filename=\"hello_world.txt\"\n"+
"\n" +
"hello world!\n"+
"\n" ;
BufferedReader input=new BufferedReader(new InputStreamReader(new ByteArrayInputStream(request.getBytes())));
try {
RequestParser.RequestInfo requestInfo = RequestParser.parseRequest(input);
// Test HTTP command
if (!requestInfo.getHttpCommand().equals("GET")) {
System.out.println("HTTP command test failed (-5)");
}
// Test URI
if (!requestInfo.getUri().equals("/api/resource?id=123&name=test")) {
System.out.println("URI test failed (-5)");
}
// Test URI segments
String[] expectedUriSegments = {"api", "resource"};
if (!Arrays.equals(requestInfo.getUriSegments(), expectedUriSegments)) {
System.out.println("URI segments test failed (-5)");
for(String s : requestInfo.getUriSegments()){
System.out.println(s);
}
}
// Test parameters
Map<String, String> expectedParams = new HashMap<>();
expectedParams.put("id", "123");
expectedParams.put("name", "test");
expectedParams.put("filename","\"hello_world.txt\"");
if (!requestInfo.getParameters().equals(expectedParams)) {
System.out.println("Parameters test failed (-5)");
}
// Test content
byte[] expectedContent = "hello world!\n".getBytes();
if (!Arrays.equals(requestInfo.getContent(), expectedContent)) {
System.out.println("Content test failed (-5)");
}
input.close();
} catch (IOException e) {
System.out.println("Exception occurred during parsing: " + e.getMessage() + " (-5)");
}
}
public static void testServer() throws Exception {
int baseThreads = Thread.activeCount();
MyHTTPServer server = new MyHTTPServer(0, 1);
server.addServlet("GET", "/calc", new Servlet() {
@Override
public void handle(RequestInfo ri, OutputStream toClient) throws IOException {
String xVal = ri.getParameters().get("x");
String yVal = ri.getParameters().get("y");
int x = xVal == null ? 0 : Integer.parseInt(xVal);
int y = yVal == null ? 0 : Integer.parseInt(yVal);
String body = String.valueOf(x + y);
toClient.write(body.getBytes());
}
@Override
public void close() throws IOException {
}
});
server.start();
Thread.sleep(100);
if (Thread.activeCount() != baseThreads + 1) {
System.out.println("server did not start exactly one thread (-10)");
}
try (Socket socket = new Socket("localhost", server.getPort())) {
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.print("GET /calc?x=7&y=5 HTTP/1.1\r\n");
pw.print("Host: localhost\r\n");
pw.print("\r\n");
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
if (!sb.toString().contains("12")) {
System.out.println("server response mismatch (-10)");
}
}
server.close();
Thread.sleep(1200);
if (Thread.activeCount() != baseThreads) {
System.out.println("server did not close all threads (-10)");
}
}
public static void main(String[] args) {
testParseRequest(); // 40 points
try{
testServer(); // 60
}catch(Exception e){
System.out.println("your server throwed an exception (-60)");
}
System.out.println("done");
}
}