Lines 16-27
Link Here
|
16 |
*/ |
16 |
*/ |
17 |
package org.apache.catalina.valves; |
17 |
package org.apache.catalina.valves; |
18 |
|
18 |
|
|
|
19 |
import java.io.IOException; |
20 |
import java.util.Arrays; |
19 |
import java.util.Collections; |
21 |
import java.util.Collections; |
20 |
|
22 |
|
|
|
23 |
import javax.servlet.ServletException; |
21 |
import javax.servlet.http.HttpSession; |
24 |
import javax.servlet.http.HttpSession; |
22 |
|
25 |
|
23 |
import org.junit.Test; |
26 |
import org.junit.Test; |
24 |
|
27 |
import org.apache.catalina.Context; |
|
|
28 |
import org.apache.catalina.Host; |
25 |
import org.apache.catalina.Valve; |
29 |
import org.apache.catalina.Valve; |
26 |
import org.apache.catalina.connector.Request; |
30 |
import org.apache.catalina.connector.Request; |
27 |
import org.apache.catalina.connector.Response; |
31 |
import org.apache.catalina.connector.Response; |
Lines 34-39
public class TestCrawlerSessionManagerValve {
Link Here
|
34 |
public void testCrawlerIpsPositive() throws Exception { |
38 |
public void testCrawlerIpsPositive() throws Exception { |
35 |
CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve(); |
39 |
CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve(); |
36 |
valve.setCrawlerIps("216\\.58\\.206\\.174"); |
40 |
valve.setCrawlerIps("216\\.58\\.206\\.174"); |
|
|
41 |
valve.setCrawlerUserAgents(valve.getCrawlerUserAgents()); |
37 |
valve.setNext(EasyMock.createMock(Valve.class)); |
42 |
valve.setNext(EasyMock.createMock(Valve.class)); |
38 |
HttpSession session = createSessionExpectations(valve, true); |
43 |
HttpSession session = createSessionExpectations(valve, true); |
39 |
Request request = createRequestExpectations("216.58.206.174", session, true); |
44 |
Request request = createRequestExpectations("216.58.206.174", session, true); |
Lines 49-54
public class TestCrawlerSessionManagerValve {
Link Here
|
49 |
public void testCrawlerIpsNegative() throws Exception { |
54 |
public void testCrawlerIpsNegative() throws Exception { |
50 |
CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve(); |
55 |
CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve(); |
51 |
valve.setCrawlerIps("216\\.58\\.206\\.174"); |
56 |
valve.setCrawlerIps("216\\.58\\.206\\.174"); |
|
|
57 |
valve.setCrawlerUserAgents(valve.getCrawlerUserAgents()); |
52 |
valve.setNext(EasyMock.createMock(Valve.class)); |
58 |
valve.setNext(EasyMock.createMock(Valve.class)); |
53 |
HttpSession session = createSessionExpectations(valve, false); |
59 |
HttpSession session = createSessionExpectations(valve, false); |
54 |
Request request = createRequestExpectations("127.0.0.1", session, false); |
60 |
Request request = createRequestExpectations("127.0.0.1", session, false); |
Lines 60-65
public class TestCrawlerSessionManagerValve {
Link Here
|
60 |
EasyMock.verify(request, session); |
66 |
EasyMock.verify(request, session); |
61 |
} |
67 |
} |
62 |
|
68 |
|
|
|
69 |
@Test |
70 |
public void testCrawlerMultipleHostsHostAware() throws Exception { |
71 |
CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve(); |
72 |
valve.setCrawlerUserAgents(valve.getCrawlerUserAgents()); |
73 |
valve.setHostAware(true); |
74 |
valve.setContextAware(true); |
75 |
valve.setNext(EasyMock.createMock(Valve.class)); |
76 |
|
77 |
verifyCrawlingLocalhost(valve, "localhost"); |
78 |
verifyCrawlingLocalhost(valve, "example.invalid"); |
79 |
} |
80 |
|
81 |
|
82 |
private void verifyCrawlingLocalhost(CrawlerSessionManagerValve valve, String hostname) |
83 |
throws IOException, ServletException { |
84 |
HttpSession session = createSessionExpectations(valve, true); |
85 |
Request request = createRequestExpectations("127.0.0.1", session, true, hostname, "tomcatBot 1.0"); |
86 |
|
87 |
EasyMock.replay(request, session); |
88 |
|
89 |
valve.invoke(request, EasyMock.createMock(Response.class)); |
90 |
|
91 |
EasyMock.verify(request, session); |
92 |
} |
93 |
|
94 |
|
63 |
private HttpSession createSessionExpectations(CrawlerSessionManagerValve valve, boolean isBot) { |
95 |
private HttpSession createSessionExpectations(CrawlerSessionManagerValve valve, boolean isBot) { |
64 |
HttpSession session = EasyMock.createMock(HttpSession.class); |
96 |
HttpSession session = EasyMock.createMock(HttpSession.class); |
65 |
if (isBot) { |
97 |
if (isBot) { |
Lines 72-86
public class TestCrawlerSessionManagerValve {
Link Here
|
72 |
return session; |
104 |
return session; |
73 |
} |
105 |
} |
74 |
|
106 |
|
|
|
107 |
|
75 |
private Request createRequestExpectations(String ip, HttpSession session, boolean isBot) { |
108 |
private Request createRequestExpectations(String ip, HttpSession session, boolean isBot) { |
|
|
109 |
return createRequestExpectations(ip, session, isBot, "localhost", "something 1.0"); |
110 |
} |
111 |
|
112 |
private Request createRequestExpectations(String ip, HttpSession session, boolean isBot, String hostname, String userAgent) { |
76 |
Request request = EasyMock.createMock(Request.class); |
113 |
Request request = EasyMock.createMock(Request.class); |
77 |
EasyMock.expect(request.getRemoteAddr()).andReturn(ip); |
114 |
EasyMock.expect(request.getRemoteAddr()).andReturn(ip); |
|
|
115 |
EasyMock.expect(request.getHost()).andReturn(simpleHostWithName(hostname)); |
116 |
EasyMock.expect(request.getContext()).andReturn(simpleContextWithName()); |
78 |
IExpectationSetters<HttpSession> setter = EasyMock.expect(request.getSession(false)) |
117 |
IExpectationSetters<HttpSession> setter = EasyMock.expect(request.getSession(false)) |
79 |
.andReturn(null); |
118 |
.andReturn(null); |
80 |
if (isBot) { |
119 |
if (isBot) { |
81 |
setter.andReturn(session); |
120 |
setter.andReturn(session); |
82 |
} |
121 |
} |
83 |
EasyMock.expect(request.getHeaders("user-agent")).andReturn(Collections.emptyEnumeration()); |
122 |
EasyMock.expect(request.getHeaders("user-agent")).andAnswer(() -> Collections.enumeration(Arrays.asList(userAgent))); |
84 |
return request; |
123 |
return request; |
85 |
} |
124 |
} |
|
|
125 |
|
126 |
private Host simpleHostWithName(String hostname) { |
127 |
Host host = EasyMock.createMock(Host.class); |
128 |
EasyMock.expect(host.getName()).andReturn(hostname); |
129 |
EasyMock.replay(host); |
130 |
return host; |
131 |
} |
132 |
|
133 |
private Context simpleContextWithName() { |
134 |
Context context = EasyMock.createMock(Context.class); |
135 |
EasyMock.expect(context.getName()).andReturn("/examples"); |
136 |
EasyMock.replay(context); |
137 |
return context; |
138 |
} |
86 |
} |
139 |
} |