Search in sources :

Example 71 with Request

use of org.apache.catalina.connector.Request in project tomcat by apache.

the class TestCrawlerSessionManagerValve method verifyCrawlingContext.

private void verifyCrawlingContext(CrawlerSessionManagerValve valve, String contextPath) throws IOException, ServletException {
    HttpSession session = createSessionExpectations(valve, true);
    Request request = createRequestExpectations("127.0.0.1", session, true, "localhost", contextPath, "tomcatBot 1.0");
    EasyMock.replay(request, session);
    valve.invoke(request, EasyMock.createMock(Response.class));
    EasyMock.verify(request, session);
}
Also used : Response(org.apache.catalina.connector.Response) HttpSession(jakarta.servlet.http.HttpSession) Request(org.apache.catalina.connector.Request)

Example 72 with Request

use of org.apache.catalina.connector.Request in project tomcat by apache.

the class TestCrawlerSessionManagerValve method testCrawlerIpsPositive.

@Test
public void testCrawlerIpsPositive() throws Exception {
    CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve();
    valve.setCrawlerIps("216\\.58\\.206\\.174");
    valve.setCrawlerUserAgents(valve.getCrawlerUserAgents());
    valve.setNext(EasyMock.createMock(Valve.class));
    HttpSession session = createSessionExpectations(valve, true);
    Request request = createRequestExpectations("216.58.206.174", session, true);
    EasyMock.replay(request, session);
    valve.invoke(request, EasyMock.createMock(Response.class));
    EasyMock.verify(request, session);
}
Also used : Response(org.apache.catalina.connector.Response) HttpSession(jakarta.servlet.http.HttpSession) Request(org.apache.catalina.connector.Request) Valve(org.apache.catalina.Valve) Test(org.junit.Test)

Example 73 with Request

use of org.apache.catalina.connector.Request in project tomcat by apache.

the class TestCrawlerSessionManagerValve method verifyCrawlingLocalhost.

private void verifyCrawlingLocalhost(CrawlerSessionManagerValve valve, String hostname) throws IOException, ServletException {
    HttpSession session = createSessionExpectations(valve, true);
    Request request = createRequestExpectations("127.0.0.1", session, true, hostname, "/examples", "tomcatBot 1.0");
    EasyMock.replay(request, session);
    valve.invoke(request, EasyMock.createMock(Response.class));
    EasyMock.verify(request, session);
}
Also used : Response(org.apache.catalina.connector.Response) HttpSession(jakarta.servlet.http.HttpSession) Request(org.apache.catalina.connector.Request)

Example 74 with Request

use of org.apache.catalina.connector.Request in project tomcat by apache.

the class TestCrawlerSessionManagerValve method testCrawlersSessionIdIsRemovedAfterSessionExpiry.

@Test
public void testCrawlersSessionIdIsRemovedAfterSessionExpiry() throws IOException, ServletException {
    CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve();
    valve.setCrawlerIps("216\\.58\\.206\\.174");
    valve.setCrawlerUserAgents(valve.getCrawlerUserAgents());
    valve.setNext(EasyMock.createMock(Valve.class));
    valve.setSessionInactiveInterval(0);
    StandardSession session = new StandardSession(TEST_MANAGER);
    session.setId("id");
    session.setValid(true);
    Request request = createRequestExpectations("216.58.206.174", session, true);
    EasyMock.replay(request);
    valve.invoke(request, EasyMock.createMock(Response.class));
    EasyMock.verify(request);
    MatcherAssert.assertThat(valve.getClientIpSessionId().values(), CoreMatchers.hasItem("id"));
    session.expire();
    Assert.assertEquals(0, valve.getClientIpSessionId().values().size());
}
Also used : Response(org.apache.catalina.connector.Response) StandardSession(org.apache.catalina.session.StandardSession) Request(org.apache.catalina.connector.Request) Valve(org.apache.catalina.Valve) Test(org.junit.Test)

Example 75 with Request

use of org.apache.catalina.connector.Request in project tomcat by apache.

the class TestRemoteIpValve method testInvokeNotAllowedRemoteAddr.

@Test
public void testInvokeNotAllowedRemoteAddr() throws Exception {
    // PREPARE
    RemoteIpValve remoteIpValve = new RemoteIpValve();
    remoteIpValve.setInternalProxies("192\\.168\\.0\\.10|192\\.168\\.0\\.11");
    remoteIpValve.setTrustedProxies("proxy1|proxy2|proxy3");
    remoteIpValve.setRemoteIpHeader("x-forwarded-for");
    remoteIpValve.setProxiesHeader("x-forwarded-by");
    RemoteAddrAndHostTrackerValve remoteAddrAndHostTrackerValve = new RemoteAddrAndHostTrackerValve();
    remoteIpValve.setNext(remoteAddrAndHostTrackerValve);
    Request request = new MockRequest();
    request.setCoyoteRequest(new org.apache.coyote.Request());
    request.setRemoteAddr("not-allowed-internal-proxy");
    request.setRemoteHost("not-allowed-internal-proxy-host");
    request.getCoyoteRequest().getMimeHeaders().addValue("x-forwarded-for").setString("140.211.11.130, proxy1, proxy2");
    // TEST
    remoteIpValve.invoke(request, null);
    // VERIFY
    String actualXForwardedFor = request.getHeader("x-forwarded-for");
    Assert.assertEquals("x-forwarded-for must be unchanged", "140.211.11.130, proxy1, proxy2", actualXForwardedFor);
    String actualXForwardedBy = request.getHeader("x-forwarded-by");
    Assert.assertNull("x-forwarded-by must be null", actualXForwardedBy);
    String actualRemoteAddr = remoteAddrAndHostTrackerValve.getRemoteAddr();
    Assert.assertEquals("remoteAddr", "not-allowed-internal-proxy", actualRemoteAddr);
    String actualRemoteHost = remoteAddrAndHostTrackerValve.getRemoteHost();
    Assert.assertEquals("remoteHost", "not-allowed-internal-proxy-host", actualRemoteHost);
    String actualPostInvokeRemoteAddr = request.getRemoteAddr();
    Assert.assertEquals("postInvoke remoteAddr", "not-allowed-internal-proxy", actualPostInvokeRemoteAddr);
    String actualPostInvokeRemoteHost = request.getRemoteHost();
    Assert.assertEquals("postInvoke remoteAddr", "not-allowed-internal-proxy-host", actualPostInvokeRemoteHost);
}
Also used : Request(org.apache.catalina.connector.Request) Test(org.junit.Test)

Aggregations

Request (org.apache.catalina.connector.Request)80 Test (org.junit.Test)44 Response (org.apache.catalina.connector.Response)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 IOException (java.io.IOException)9 HttpSession (javax.servlet.http.HttpSession)9 Context (org.apache.catalina.Context)9 ServletRequest (javax.servlet.ServletRequest)8 Valve (org.apache.catalina.Valve)7 RequestFacade (org.apache.catalina.connector.RequestFacade)7 TesterContext (org.apache.tomcat.unittest.TesterContext)7 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)5 HttpSession (jakarta.servlet.http.HttpSession)5 ServletException (javax.servlet.ServletException)5 Connector (org.apache.catalina.connector.Connector)5 ServletRequest (jakarta.servlet.ServletRequest)4 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)4 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)3 LifecycleException (org.apache.catalina.LifecycleException)3 ServletException (jakarta.servlet.ServletException)2