Search in sources :

Example 1 with TesterContext

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

the class TestResponse method doTestSendRedirect.

private void doTestSendRedirect(String input, String expectedLocation) throws Exception {
    // Set-up.
    // Note: Not sufficient for testing relative -> absolute
    Connector connector = new Connector();
    org.apache.coyote.Response cResponse = new org.apache.coyote.Response();
    Response response = new Response();
    response.setConnector(connector);
    response.setCoyoteResponse(cResponse);
    Request request = new Request();
    org.apache.coyote.Request cRequest = new org.apache.coyote.Request();
    request.setCoyoteRequest(cRequest);
    Context context = new TesterContext();
    request.setContext(context);
    response.setRequest(request);
    // Do test
    response.sendRedirect(input);
    String location = response.getHeader("Location");
    Assert.assertEquals(expectedLocation, location);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) TesterRequest(org.apache.tomcat.unittest.TesterRequest) TesterContext(org.apache.tomcat.unittest.TesterContext)

Example 2 with TesterContext

use of org.apache.tomcat.unittest.TesterContext 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 3 with TesterContext

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

the class TestRealmBase method doTestDigestDigestPasswords.

private void doTestDigestDigestPasswords(String password, String digest, String digestedPassword) throws Exception {
    Context context = new TesterContext();
    TesterMapRealm realm = new TesterMapRealm();
    realm.setContainer(context);
    realm.setDigest(digest);
    realm.start();
    realm.addUser(USER1, digestedPassword);
    Principal p = realm.authenticate(USER1, password);
    Assert.assertNotNull(p);
    Assert.assertEquals(USER1, p.getName());
}
Also used : Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) TesterMapRealm(org.apache.catalina.startup.TesterMapRealm) TesterContext(org.apache.tomcat.unittest.TesterContext) Principal(java.security.Principal)

Example 4 with TesterContext

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

the class TestDigestAuthenticator method bug54521.

@Test
public void bug54521() throws LifecycleException {
    DigestAuthenticator digestAuthenticator = new DigestAuthenticator();
    TesterContext context = new TesterContext();
    context.setServletContext(new TesterServletContext());
    digestAuthenticator.setContainer(context);
    digestAuthenticator.start();
    Request request = new TesterRequest();
    final int count = 1000;
    Set<String> nonces = new HashSet<>();
    for (int i = 0; i < count; i++) {
        nonces.add(digestAuthenticator.generateNonce(request));
    }
    Assert.assertEquals(count, nonces.size());
}
Also used : TesterServletContext(org.apache.tomcat.unittest.TesterServletContext) TesterRequest(org.apache.tomcat.unittest.TesterRequest) Request(org.apache.catalina.connector.Request) TesterRequest(org.apache.tomcat.unittest.TesterRequest) TesterContext(org.apache.tomcat.unittest.TesterContext) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) HashSet(java.util.HashSet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 5 with TesterContext

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

the class TestResponse method setupResponse.

private Response setupResponse() {
    Connector connector = new Connector();
    org.apache.coyote.Response cResponse = new org.apache.coyote.Response();
    Response response = new Response();
    response.setCoyoteResponse(cResponse);
    Request request = new Request(connector);
    org.apache.coyote.Request cRequest = new org.apache.coyote.Request();
    request.setCoyoteRequest(cRequest);
    Context context = new TesterContext();
    request.getMappingData().context = context;
    response.setRequest(request);
    context.addLocaleEncodingMappingParameter(Locale.ENGLISH.getLanguage(), ISO_8859_1);
    context.addLocaleEncodingMappingParameter(Locale.CHINESE.getLanguage(), UTF_8);
    context.addLocaleEncodingMappingParameter(UNDETERMINED.toLanguageTag(), UNKNOWN);
    return response;
}
Also used : HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) TesterRequest(org.apache.tomcat.unittest.TesterRequest) TesterContext(org.apache.tomcat.unittest.TesterContext)

Aggregations

TesterContext (org.apache.tomcat.unittest.TesterContext)18 Context (org.apache.catalina.Context)15 Test (org.junit.Test)8 Request (org.apache.catalina.connector.Request)7 TesterRequest (org.apache.tomcat.unittest.TesterRequest)6 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 Host (org.apache.catalina.Host)4 TesterMapRealm (org.apache.catalina.startup.TesterMapRealm)4 TesterHost (org.apache.tomcat.unittest.TesterHost)4 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 DirContext (javax.naming.directory.DirContext)3 InitialDirContext (javax.naming.directory.InitialDirContext)3 Response (org.apache.catalina.connector.Response)3 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)3 Field (java.lang.reflect.Field)2 Principal (java.security.Principal)2 HashSet (java.util.HashSet)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Store (org.apache.catalina.Store)2