-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerJunitTests.java
More file actions
257 lines (218 loc) · 9.4 KB
/
Copy pathServerJunitTests.java
File metadata and controls
257 lines (218 loc) · 9.4 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package server;// make sure to add JUnit to your project to run this class.
import org.junit.Assert;
import org.junit.Test;
/*
* 3 tests are provided for you, these WILL be part of the marking criteria for functionality tests.
* It's a good idea to make sure they pass, and add some more tests here.
* You should not modify the existing tests.
*/
public class ServerJunitTests {
private static final String FILF_ROOT = "./root";
private static final String LOG = "./root/log.txt";
private static final String HTTP_CRLF = "\r\n";
private static final String STATUS_OK = "HTTP/1.0 200 OK";
private static final String FILE_NOT_FOUND = "HTTP/1.0 404 Not Found\r\n\r\n";
private static final String FILE_NO_PREMISSION = "HTTP/1.0 403 Forbidden\r\n\r\n";
private static final String FILE_CREATED = "HTTP/1.0 201 Created\r\n\r\n";
private static final String NOT_MODIFIED = "HTTP/1.0 304 Not modified\r\n\r\n";
private static final String UKNOWN_METHOD_1_0 = "HTTP/1.0 501 Rabble Not Implemented\r\n\r\n"; //For some unknown method.
private static final String BAD_REQUEST_1_0 = "HTTP/1.0 400 Bad Request\r\n\r\n";
ConcreteLogCenter logCenter = ConcreteLogCenter.getInstance();
FileLog fileLog = new FileLog();
@Test
public void testGetRequestHttp1() {
logCenter.addObserver(fileLog);
ConcreteLogCenter.setLogMode(true);
fileLog.setLogRoot(LOG);
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "GET /index.html HTTP/1.0\r\n\r\n";
String expected;
String actual;
// Request handler needs to have a constructor with no arguments.
RequestHandler requestHandler = new RequestHandler();
expected = STATUS_OK;
actual = new String(requestHandler.processRequest(request.getBytes())).split(HTTP_CRLF)[0];
Assert.assertEquals(expected, actual);
}
@Test
public void testHeadRequestHttp1() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "HEAD /index.html HTTP/1.0\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = STATUS_OK;
actual = new String(requestHandler.processRequest(request.getBytes())).split(HTTP_CRLF)[0];
Assert.assertEquals(expected, actual);
}
// Test post2
@Test
public void testHttpPost() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "POST /index.html HTTP/1.0\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
requestHandler.setEntity("aaaaa".getBytes());
expected = STATUS_OK;
actual = new String(requestHandler.processRequest(request.getBytes())).split(HTTP_CRLF)[0];
Assert.assertEquals(expected, actual);
}
@Test
public void testInvalidRequest() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "Rabble rabble";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = BAD_REQUEST_1_0;
actual = new String(requestHandler.processRequest(request.getBytes()));
Assert.assertEquals(expected, actual);
}
// Test simple request.
@Test
public void testSimpleRequest() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "GET /index.html\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = "<!DOCTYPE html>";
actual = new String(requestHandler.processRequest(request.getBytes())).split(HTTP_CRLF)[0];
Assert.assertEquals(expected, actual);
}
// Test with custom but valid method.
@Test
public void testUnknowMethod() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "Rabble /index.html HTTP/1.0\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = UKNOWN_METHOD_1_0;
actual = new String(requestHandler.processRequest(request.getBytes()));
Assert.assertEquals(expected, actual);
}
@Test
public void getNotFound() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "GET /aaa.html HTTP/1.0\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = FILE_NOT_FOUND;
actual = new String(requestHandler.processRequest(request.getBytes()));
Assert.assertEquals(expected, actual);
}
@Test
public void headNotFound() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "HEAD /aaa.html HTTP/1.0\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = FILE_NOT_FOUND;
actual = new String(requestHandler.processRequest(request.getBytes()));
Assert.assertEquals(expected, actual);
}
@Test
public void postNotFound() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "POST /aaa.html HTTP/1.0\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
requestHandler.setEntity("aaaaa".getBytes());
expected = FILE_CREATED;
actual = new String(requestHandler.processRequest(request.getBytes()));
Assert.assertEquals(expected, actual);
}
public void getForbid() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "GET /page1.html HTTP/1.0\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = FILE_NO_PREMISSION;
actual = new String(requestHandler.processRequest(request.getBytes()));
Assert.assertEquals(expected, actual);
}
@Test
public void headForbid() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "HEAD /page1.html HTTP/1.0\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = FILE_NO_PREMISSION;
actual = new String(requestHandler.processRequest(request.getBytes()));
Assert.assertEquals(expected, actual);
}
@Test
public void postForbid() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "POST /page1.html HTTP/1.0\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
requestHandler.setEntity("aaaaa".getBytes());
expected = FILE_NO_PREMISSION;
actual = new String(requestHandler.processRequest(request.getBytes()));
Assert.assertEquals(expected, actual);
}
@Test
public void getConditional1() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "GET /index.html HTTP/1.0\r\nIf-Modified-Since: Sun Nov 6 08:49:37 2015\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = NOT_MODIFIED;
actual = new String(requestHandler.processRequest(request.getBytes()));
Assert.assertEquals(expected, actual);
}
@Test
public void getConditional2() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "GET /index.html HTTP/1.0\r\nIf-Modified-Since: Sunday, 06-Nov-15 08:49:37 GMT\r\n\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = NOT_MODIFIED;
actual = new String(requestHandler.processRequest(request.getBytes()));
Assert.assertEquals(expected, actual);
}
@Test
public void getConditional3() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "GET /index.html HTTP/1.0\r\nIf-Modified-Since: Sun, 06 Nov 2015 08:49:37 GMT\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = NOT_MODIFIED;
actual = new String(requestHandler.processRequest(request.getBytes()));
Assert.assertEquals(expected, actual);
}
@Test
public void getConditionalinTime() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "GET /index.html HTTP/1.0\r\nIf-Modified-Since: Sun, 06 Nov 1994 08:49:37 GMT\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = STATUS_OK;
actual = new String(requestHandler.processRequest(request.getBytes())).split(HTTP_CRLF)[0];
Assert.assertEquals(expected, actual);
}
@Test
public void getConditionalBadTime() {
HTTPFileServer.setRootDir(FILF_ROOT);
String request = "GET /index.html HTTP/1.0\r\nIf-Modified-Since: rabble\r\n\r\n";
String expected;
String actual;
RequestHandler requestHandler = new RequestHandler();
expected = STATUS_OK;
actual = new String(requestHandler.processRequest(request.getBytes())).split(HTTP_CRLF)[0];
Assert.assertEquals(expected, actual);
}
}