Lines 22-28
Link Here
|
22 |
|
22 |
|
23 |
import javax.servlet.ServletException; |
23 |
import javax.servlet.ServletException; |
24 |
import javax.servlet.http.HttpSession; |
24 |
import javax.servlet.http.HttpSession; |
|
|
25 |
import javax.servlet.http.HttpSessionBindingListener; |
25 |
|
26 |
|
|
|
27 |
import org.apache.catalina.Manager; |
28 |
import org.apache.catalina.core.StandardContext; |
29 |
import org.apache.catalina.session.StandardManager; |
30 |
import org.apache.catalina.session.StandardSession; |
31 |
import org.hamcrest.CoreMatchers; |
26 |
import org.junit.Test; |
32 |
import org.junit.Test; |
27 |
|
33 |
|
28 |
import org.apache.catalina.Context; |
34 |
import org.apache.catalina.Context; |
Lines 33-40
Link Here
|
33 |
import org.easymock.EasyMock; |
39 |
import org.easymock.EasyMock; |
34 |
import org.easymock.IExpectationSetters; |
40 |
import org.easymock.IExpectationSetters; |
35 |
|
41 |
|
|
|
42 |
import static org.hamcrest.CoreMatchers.*; |
43 |
import static org.hamcrest.MatcherAssert.assertThat; |
44 |
|
36 |
public class TestCrawlerSessionManagerValve { |
45 |
public class TestCrawlerSessionManagerValve { |
37 |
|
46 |
|
|
|
47 |
private static final Manager TEST_MANAGER; |
48 |
|
49 |
static { |
50 |
TEST_MANAGER = new StandardManager(); |
51 |
TEST_MANAGER.setContext(new StandardContext()); |
52 |
} |
53 |
|
54 |
|
55 |
|
38 |
@Test |
56 |
@Test |
39 |
public void testCrawlerIpsPositive() throws Exception { |
57 |
public void testCrawlerIpsPositive() throws Exception { |
40 |
CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve(); |
58 |
CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve(); |
Lines 79-84
public void testCrawlerMultipleHostsHostAware() throws Exception {
Link Here
|
79 |
verifyCrawlingLocalhost(valve, "example.invalid"); |
97 |
verifyCrawlingLocalhost(valve, "example.invalid"); |
80 |
} |
98 |
} |
81 |
|
99 |
|
|
|
100 |
@Test |
101 |
public void testCrawlersSessionIdIsRemovedAfterSessionExpiry() throws IOException, ServletException { |
102 |
CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve(); |
103 |
valve.setCrawlerIps("216\\.58\\.206\\.174"); |
104 |
valve.setCrawlerUserAgents(valve.getCrawlerUserAgents()); |
105 |
valve.setNext(EasyMock.createMock(Valve.class)); |
106 |
valve.setSessionInactiveInterval(0); |
107 |
StandardSession session = new StandardSession(TEST_MANAGER); |
108 |
session.setId("id"); |
109 |
session.setValid(true); |
110 |
|
111 |
Request request = createRequestExpectations("216.58.206.174", session, true); |
112 |
|
113 |
EasyMock.replay(request); |
114 |
|
115 |
valve.invoke(request, EasyMock.createMock(Response.class)); |
116 |
|
117 |
EasyMock.verify(request); |
118 |
|
119 |
assertThat(valve.getClientIpSessionId().values(), hasItem("id")); |
120 |
|
121 |
session.expire(); |
122 |
|
123 |
assertThat(valve.getClientIpSessionId().values().size(), is(0)); |
124 |
} |
125 |
|
82 |
|
126 |
|
83 |
private void verifyCrawlingLocalhost(CrawlerSessionManagerValve valve, String hostname) |
127 |
private void verifyCrawlingLocalhost(CrawlerSessionManagerValve valve, String hostname) |
84 |
throws IOException, ServletException { |
128 |
throws IOException, ServletException { |
Lines 97-103
private HttpSession createSessionExpectations(CrawlerSessionManagerValve valve,
Link Here
|
97 |
HttpSession session = EasyMock.createMock(HttpSession.class); |
141 |
HttpSession session = EasyMock.createMock(HttpSession.class); |
98 |
if (isBot) { |
142 |
if (isBot) { |
99 |
EasyMock.expect(session.getId()).andReturn("id").times(2); |
143 |
EasyMock.expect(session.getId()).andReturn("id").times(2); |
100 |
session.setAttribute(valve.getClass().getName(), valve); |
144 |
session.setAttribute(EasyMock.eq(valve.getClass().getName()), EasyMock.anyObject(HttpSessionBindingListener.class)); |
101 |
EasyMock.expectLastCall(); |
145 |
EasyMock.expectLastCall(); |
102 |
session.setMaxInactiveInterval(60); |
146 |
session.setMaxInactiveInterval(60); |
103 |
EasyMock.expectLastCall(); |
147 |
EasyMock.expectLastCall(); |
104 |
smaller object. It helps to serialize this sessions with libraries like kryo. |
148 |
smaller object. It helps to serialize this sessions with libraries like kryo. |
105 |
-- |
|
|
106 |
.../valves/CrawlerSessionManagerValve.java | 17 +++++++++-------- |
149 |
.../valves/CrawlerSessionManagerValve.java | 17 +++++++++-------- |
107 |
.../valves/TestCrawlerSessionManagerValve.java | 1 - |
150 |
.../valves/TestCrawlerSessionManagerValve.java | 1 - |
108 |
2 files changed, 9 insertions(+), 9 deletions(-) |
151 |
2 files changed, 9 insertions(+), 9 deletions(-) |