Search in sources :

Example 1 with TesterResponse

use of org.apache.tomcat.unittest.TesterResponse in project tomcat70 by apache.

the class TestRealmBase method doRoleTest.

private void doRoleTest(List<String> userRoles, List<String> constraintOneRoles, List<String> constraintTwoRoles, List<String> applicationRoles, boolean expected) throws IOException {
    TesterMapRealm mapRealm = new TesterMapRealm();
    // Configure the security constraints for the resource
    SecurityConstraint constraintOne = new SecurityConstraint();
    if (constraintOneRoles != null) {
        constraintOne.setAuthConstraint(true);
        for (String constraintRole : constraintOneRoles) {
            constraintOne.addAuthRole(constraintRole);
        }
    }
    SecurityConstraint constraintTwo = new SecurityConstraint();
    if (constraintTwoRoles != null) {
        constraintTwo.setAuthConstraint(true);
        for (String constraintRole : constraintTwoRoles) {
            constraintTwo.addAuthRole(constraintRole);
        }
    }
    SecurityConstraint[] constraints = new SecurityConstraint[] { constraintOne, constraintTwo };
    // Set up the mock request and response
    Request request = new Request();
    Response response = new TesterResponse();
    Context context = new TesterContext();
    for (String applicationRole : applicationRoles) {
        context.addSecurityRole(applicationRole);
    }
    request.setContext(context);
    // Configure the users in the Realm
    if (userRoles != null) {
        GenericPrincipal gp = new GenericPrincipal(USER1, PWD, userRoles);
        request.setUserPrincipal(gp);
    }
    // Check if user meets constraints
    boolean result = mapRealm.hasResourcePermission(request, response, constraints, null);
    Assert.assertEquals(Boolean.valueOf(expected), Boolean.valueOf(result));
}
Also used : TesterResponse(org.apache.tomcat.unittest.TesterResponse) Response(org.apache.catalina.connector.Response) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) TesterMapRealm(org.apache.catalina.startup.TesterMapRealm) Request(org.apache.catalina.connector.Request) TesterRequest(org.apache.tomcat.unittest.TesterRequest) TesterResponse(org.apache.tomcat.unittest.TesterResponse) TesterContext(org.apache.tomcat.unittest.TesterContext) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint)

Example 2 with TesterResponse

use of org.apache.tomcat.unittest.TesterResponse in project tomcat70 by apache.

the class TestRemoteIpFilter method testRemoteIpFilter.

private MockFilterChain testRemoteIpFilter(FilterDef filterDef, Request request) throws LifecycleException, IOException, ServletException {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    RemoteIpFilter remoteIpFilter = new RemoteIpFilter();
    filterDef.setFilterClass(RemoteIpFilter.class.getName());
    filterDef.setFilter(remoteIpFilter);
    filterDef.setFilterName(RemoteIpFilter.class.getName());
    root.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(RemoteIpFilter.class.getName());
    filterMap.addURLPattern("*");
    root.addFilterMap(filterMap);
    getTomcatInstance().start();
    MockFilterChain filterChain = new MockFilterChain();
    // TEST
    TesterResponse response = new TesterResponse();
    response.setRequest(request);
    remoteIpFilter.doFilter(request, response, filterChain);
    return filterChain;
}
Also used : Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Tomcat(org.apache.catalina.startup.Tomcat) TesterResponse(org.apache.tomcat.unittest.TesterResponse) FilterMap(org.apache.catalina.deploy.FilterMap)

Example 3 with TesterResponse

use of org.apache.tomcat.unittest.TesterResponse in project tomcat by apache.

the class TestResponseUtil method testAddValidWithValidSingleHeader.

@Test
public void testAddValidWithValidSingleHeader() {
    TesterResponse response = new TesterResponse();
    response.getCoyoteResponse();
    response.addHeader("vary", "foo, bar");
    List<String> expected = new ArrayList<>();
    expected.add("foo");
    expected.add("bar");
    expected.add("too");
    doTestAddVaryFieldName(response, "too", expected);
}
Also used : ArrayList(java.util.ArrayList) TesterResponse(org.apache.tomcat.unittest.TesterResponse) Test(org.junit.Test)

Example 4 with TesterResponse

use of org.apache.tomcat.unittest.TesterResponse in project tomcat by apache.

the class TestResponseUtil method testAddValidWithValidHeaders.

@Test
public void testAddValidWithValidHeaders() {
    TesterResponse response = new TesterResponse();
    response.getCoyoteResponse();
    response.addHeader("vary", "foo");
    response.addHeader("vary", "bar");
    List<String> expected = new ArrayList<>();
    expected.add("foo");
    expected.add("bar");
    expected.add("too");
    doTestAddVaryFieldName(response, "too", expected);
}
Also used : ArrayList(java.util.ArrayList) TesterResponse(org.apache.tomcat.unittest.TesterResponse) Test(org.junit.Test)

Example 5 with TesterResponse

use of org.apache.tomcat.unittest.TesterResponse in project tomcat by apache.

the class TestResponseUtil method testAddValidWithValidHeadersIncludingAll.

@Test
public void testAddValidWithValidHeadersIncludingAll() {
    TesterResponse response = new TesterResponse();
    response.getCoyoteResponse();
    response.addHeader("vary", "foo");
    response.addHeader("vary", "*");
    List<String> expected = new ArrayList<>();
    expected.add("*");
    doTestAddVaryFieldName(response, "too", expected);
}
Also used : ArrayList(java.util.ArrayList) TesterResponse(org.apache.tomcat.unittest.TesterResponse) Test(org.junit.Test)

Aggregations

TesterResponse (org.apache.tomcat.unittest.TesterResponse)18 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)14 Context (org.apache.catalina.Context)6 TesterContext (org.apache.tomcat.unittest.TesterContext)6 Response (org.apache.catalina.connector.Response)4 TesterMapRealm (org.apache.catalina.startup.TesterMapRealm)4 TesterRequest (org.apache.tomcat.unittest.TesterRequest)4 Request (org.apache.catalina.connector.Request)2 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)2 Tomcat (org.apache.catalina.startup.Tomcat)2 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)2 ServletSecurityElement (jakarta.servlet.ServletSecurityElement)1 ServletSecurity (jakarta.servlet.annotation.ServletSecurity)1 ServletSecurityElement (javax.servlet.ServletSecurityElement)1 ServletSecurity (javax.servlet.annotation.ServletSecurity)1 FilterMap (org.apache.catalina.deploy.FilterMap)1 SecurityCollection (org.apache.catalina.deploy.SecurityCollection)1 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)1 SecurityCollection (org.apache.tomcat.util.descriptor.web.SecurityCollection)1