forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestLoggerTest.java
More file actions
188 lines (165 loc) · 5.52 KB
/
RequestLoggerTest.java
File metadata and controls
188 lines (165 loc) · 5.52 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
package org.jooby;
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;
import java.time.ZoneId;
import java.util.Optional;
import org.jooby.test.MockUnit;
import org.jooby.test.MockUnit.Block;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({RequestLogger.class, System.class })
public class RequestLoggerTest {
private Block capture = unit -> {
Response rsp = unit.get(Response.class);
rsp.complete(unit.capture(Route.Complete.class));
};
private Block onComplete = unit -> {
unit.captured(Route.Complete.class).iterator().next()
.handle(unit.get(Request.class), unit.get(Response.class), Optional.empty());
};
@Test
public void basicUsage() throws Exception {
new MockUnit(Request.class, Response.class)
.expect(capture)
.expect(timestamp(1L))
.expect(ip("127.0.0.1"))
.expect(method("GET"))
.expect(path("/"))
.expect(protocol("HTTP/1.1"))
.expect(status(Status.OK))
.expect(len(345L))
.run(unit -> {
new RequestLogger()
.dateFormatter(ZoneId.of("UTC"))
.handle(unit.get(Request.class), unit.get(Response.class));
}, onComplete);
}
@Test
public void latency() throws Exception {
new MockUnit(Request.class, Response.class)
.expect(capture)
.expect(timestamp(7L))
.expect(ip("127.0.0.1"))
.expect(method("GET"))
.expect(path("/"))
.expect(protocol("HTTP/1.1"))
.expect(status(Status.OK))
.expect(len(345L))
.expect(unit -> {
unit.mockStatic(System.class);
expect(System.currentTimeMillis()).andReturn(10L);
})
.run(unit -> {
new RequestLogger()
.dateFormatter(ZoneId.of("UTC"))
.latency()
.log(line -> assertEquals(
"127.0.0.1 - - [01/Jan/1970:00:00:00 +0000] \"GET / HTTP/1.1\" 200 345 3", line))
.handle(unit.get(Request.class), unit.get(Response.class));
}, onComplete);
}
@Test
public void extended() throws Exception {
new MockUnit(Request.class, Response.class)
.expect(capture)
.expect(timestamp(7L))
.expect(ip("127.0.0.1"))
.expect(method("GET"))
.expect(path("/"))
.expect(protocol("HTTP/1.1"))
.expect(status(Status.OK))
.expect(len(345L))
.expect(referer("/referer"))
.expect(userAgent("ugent"))
.run(unit -> {
new RequestLogger()
.dateFormatter(ZoneId.of("UTC"))
.extended()
.log(line -> assertEquals(
"127.0.0.1 - - [01/Jan/1970:00:00:00 +0000] \"GET / HTTP/1.1\" 200 345 \"/referer\" \"ugent\"", line))
.handle(unit.get(Request.class), unit.get(Response.class));
}, onComplete);
}
private Block referer(final String referer) {
return unit -> {
Mutant mutant = unit.mock(Mutant.class);
expect(mutant.value("-")).andReturn(referer);
Request req = unit.get(Request.class);
expect(req.header("Referer")).andReturn(mutant);
};
}
private Block userAgent(final String userAgent) {
return unit -> {
Mutant mutant = unit.mock(Mutant.class);
expect(mutant.value("-")).andReturn(userAgent);
Request req = unit.get(Request.class);
expect(req.header("User-Agent")).andReturn(mutant);
};
}
@Test
public void customLog() throws Exception {
new MockUnit(Request.class, Response.class)
.expect(capture)
.expect(timestamp(1L))
.expect(ip("127.0.0.1"))
.expect(method("GET"))
.expect(path("/"))
.expect(protocol("HTTP/1.1"))
.expect(status(Status.OK))
.expect(len(345L))
.run(unit -> {
new RequestLogger()
.dateFormatter(ZoneId.of("UTC"))
.log(line -> assertEquals(
"127.0.0.1 - - [01/Jan/1970:00:00:00 +0000] \"GET / HTTP/1.1\" 200 345", line))
.handle(unit.get(Request.class), unit.get(Response.class));
}, onComplete);
}
private Block method(final String method) {
return unit -> {
Request req = unit.get(Request.class);
expect(req.method()).andReturn(method);
};
}
private Block path(final String path) {
return unit -> {
Request req = unit.get(Request.class);
expect(req.path()).andReturn(path);
};
}
private Block status(final Status status) {
return unit -> {
Response rsp = unit.get(Response.class);
expect(rsp.status()).andReturn(Optional.ofNullable(status));
};
}
private Block len(final Long len) {
return unit -> {
Mutant mutant = unit.mock(Mutant.class);
expect(mutant.value("-")).andReturn(len.toString());
Response rsp = unit.get(Response.class);
expect(rsp.header("Content-Length")).andReturn(mutant);
};
}
private Block protocol(final String protocol) {
return unit -> {
Request req = unit.get(Request.class);
expect(req.protocol()).andReturn(protocol);
};
}
private Block timestamp(final long ts) {
return unit -> {
Request req = unit.get(Request.class);
expect(req.timestamp()).andReturn(ts);
};
}
private Block ip(final String ip) {
return unit -> {
Request req = unit.get(Request.class);
expect(req.ip()).andReturn(ip);
};
}
}