-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestProcessorTest.java
More file actions
111 lines (90 loc) · 3.54 KB
/
RequestProcessorTest.java
File metadata and controls
111 lines (90 loc) · 3.54 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
package core;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import java.net.Socket;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern;
import static core.HttpStatusCode.NOT_FOUND;
import static core.HttpStatusCode.OK;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
public class RequestProcessorTest {
Socket clientSocket;
RequestProcessor processor;
Map<Pattern, Handler> handlers;
Configuration configuration;
@Before
public void setUpStreams() {
clientSocket = mock(Socket.class);
handlers = new LinkedHashMap<>();
configuration = new Configuration();
processor = spy(new RequestProcessor(
clientSocket,
configuration,
handlers
));
}
@Test
public void testProcessError() throws Exception {
PrintStream savedOut = System.out;
OutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
when(clientSocket.getOutputStream()).thenReturn(mock(OutputStream.class));
when(clientSocket.getInputStream()).thenReturn(mock(InputStream.class));
doThrow(new RuntimeException("test")).
when(processor).process(any(OutputStream.class), any(InputStream.class));
processor.run();
assertEquals("Exception caught:\ntest\n", out.toString());
System.setOut(savedOut);
}
@Test
public void testHandlerError() throws Exception {
OutputStream out = new ByteArrayOutputStream();
InputStream in = new ByteArrayInputStream("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n".getBytes(ISO_8859_1));
doThrow(new RuntimeException("test")).
when(processor).executeHandlers(any(Request.class), any(Response.class));
processor.process(out, in);
assertEquals("HTTP/1.1 500 Internal Server Error", out.toString().split("\r\n")[0]);
}
@Test
public void testHandleNoHandlers() throws Exception {
handlers.clear();
OutputStream out = new ByteArrayOutputStream();
InputStream in = new ByteArrayInputStream("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n".getBytes(ISO_8859_1));
processor.process(out, in);
assertEquals("HTTP/1.1 404 Not Found", out.toString().split("\r\n")[0]);
}
@Test
public void testHandleMultipleHandlers() throws Exception {
handlers.clear();
handlers.put(Pattern.compile(".*"), new HandlerOK());
handlers.put(Pattern.compile("/abc/.*"), new HandlerNotFound());
OutputStream out = new ByteArrayOutputStream();
InputStream in = new ByteArrayInputStream("GET /test.html HTTP/1.1\r\nHost: www.google.com\r\n\r\n".getBytes(ISO_8859_1));
processor.process(out, in);
assertEquals("HTTP/1.1 " + OK, out.toString().split("\r\n")[0]);
assertEquals("foo", out.toString().split("\r\n\r\n")[1]);
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream("GET /abc/test.html HTTP/1.1\r\nHost: www.google.com\r\n\r\n".getBytes(ISO_8859_1));
processor.process(out, in);
assertEquals("HTTP/1.1 " + NOT_FOUND, out.toString().split("\r\n")[0]);
assertEquals("foobar", out.toString().split("\r\n\r\n")[1]);
}
public static class HandlerOK extends Handler {
@Override
public void handle(Request request, Response response) {
response.responseStatusCode = OK;
response.setBody("foo");
}
}
public static class HandlerNotFound extends Handler {
@Override
public void handle(Request request, Response response) {
response.responseStatusCode = NOT_FOUND;
response.setBody(response.getBody() + "bar");
}
}
}