コード例 #1
0
  public void testGetSession() {
    String requestedSessionId = "REQUESTED_SESSION_ID";

    Context context = Mockito.mock(Context.class);

    CassandraTemplate template = new TestCassandraTemplate();
    template.setLogSessionsOnStartup(true);
    template.initialize(getClass().getClassLoader());

    CassandraManager manager = new HectorCassandraManager();
    manager.setCassandraTemplate(template);
    Mockito.when(context.getManager()).thenReturn(manager);
    Request request = new Request();
    request.setContext(context);
    // fake cookie sent a value
    request.setRequestedSessionId(requestedSessionId);
    HttpSession session = request.getSession();
    Assert.assertFalse(requestedSessionId.equals(session.getId()));
  }
コード例 #2
0
  private void oneTest(
      String allow,
      String deny,
      boolean denyStatus,
      boolean addConnectorPort,
      boolean auth,
      String property,
      String type,
      boolean allowed) {
    // PREPARE
    RequestFilterValve valve = null;
    Connector connector = new Connector();
    Context context = new StandardContext();
    Request request = new Request();
    Response response = new MockResponse();
    StringBuilder msg = new StringBuilder();
    int expected = allowed ? OK : FORBIDDEN;

    connector.setPort(PORT);
    request.setConnector(connector);
    request.setContext(context);
    request.setCoyoteRequest(new org.apache.coyote.Request());

    if (type == null) {
      fail("Invalid test with null type");
    }
    if (property != null) {
      if (type.equals("Addr")) {
        valve = new RemoteAddrValve();
        request.setRemoteAddr(property);
        msg.append(" ip='" + property + "'");
      } else if (type.equals("Host")) {
        valve = new RemoteHostValve();
        request.setRemoteHost(property);
        msg.append(" host='" + property + "'");
      } else {
        fail("Invalid test type" + type);
      }
    }
    valve.setNext(new TerminatingValve());

    if (allow != null) {
      valve.setAllow(allow);
      msg.append(" allow='" + allow + "'");
    }
    if (deny != null) {
      valve.setDeny(deny);
      msg.append(" deny='" + deny + "'");
    }
    if (denyStatus) {
      valve.setDenyStatus(CUSTOM);
      msg.append(" denyStatus='" + CUSTOM + "'");
      if (!allowed) {
        expected = CUSTOM;
      }
    }
    if (addConnectorPort) {
      if (valve instanceof RemoteAddrValve) {
        ((RemoteAddrValve) valve).setAddConnectorPort(true);
      } else if (valve instanceof RemoteHostValve) {
        ((RemoteHostValve) valve).setAddConnectorPort(true);
      } else {
        fail("Can only set 'addConnectorPort' for RemoteAddrValve and RemoteHostValve");
      }
      msg.append(" addConnectorPort='true'");
    }
    if (auth) {
      context.setPreemptiveAuthentication(true);
      valve.setInvalidAuthenticationWhenDeny(true);
      msg.append(" auth='true'");
    }

    // TEST
    try {
      valve.invoke(request, response);
    } catch (IOException ex) {
      // Ignore
    } catch (ServletException ex) {
      // Ignore
    }

    // VERIFY
    if (!allowed && auth) {
      assertEquals(msg.toString(), OK, response.getStatus());
      assertEquals(msg.toString(), "invalid", request.getHeader("authorization"));
    } else {
      assertEquals(msg.toString(), expected, response.getStatus());
    }
  }