Ejemplo n.º 1
0
  @Test
  public void shouldUseDifferentBucketsWhenUsingValidPartitionKey() throws Exception {
    FakeTimeService time = new FakeTimeService(0);
    Expression<String> expr =
        Expression.valueOf("${matches(exchange.foo, 'bar-00') ?'bucket-00' :''}", String.class);
    ThrottlingFilter filter = new ThrottlingFilter(time, 1, duration("3 seconds"), expr);

    Handler handler = new ResponseHandler(Status.OK);

    // The time does not need to advance
    Exchange exchange = new Exchange();
    Promise<Response, NeverThrowsException> promise;

    exchange.put("foo", "bar-00");
    promise = filter.filter(exchange, new Request(), handler);
    assertThat(promise.get().getStatus()).isEqualTo(Status.OK);

    exchange.put("foo", "bar-00");
    promise = filter.filter(exchange, new Request(), handler);
    assertThat(promise.get().getStatus()).isEqualTo(Status.TOO_MANY_REQUESTS);

    exchange.put("foo", "bar-01");
    promise = filter.filter(exchange, new Request(), handler);
    assertThat(promise.get().getStatus()).isEqualTo(Status.OK);
  }
  @Test
  public void testRefreshAuthenticationHeader() throws Exception {

    HttpBasicAuthFilter filter =
        new HttpBasicAuthFilter(
            Expression.valueOf("bjensen", String.class),
            Expression.valueOf("${exchange.password}", String.class),
            failureHandler);
    filter.setCacheHeader(true);

    // Mock cache content for credentials
    when(session.get(endsWith(":userpass")))
        .thenReturn(null, INITIAL_CREDENTIALS, INITIAL_CREDENTIALS, REFRESHED_CREDENTIALS);

    // Scenario:
    //  first request (cache the value after initial round-trip)
    //  second request (cached value is OK)
    //  third request (cached value is no longer valid, trigger a refresh)
    doAnswer(new UnauthorizedAnswer())
        .doAnswer(new AuthorizedAnswer(INITIAL_CREDENTIALS))
        .doAnswer(new AuthorizedAnswer(INITIAL_CREDENTIALS))
        .doAnswer(new UnauthorizedAnswer())
        .doAnswer(new AuthorizedAnswer(REFRESHED_CREDENTIALS))
        .when(terminalHandler)
        .handle(any(Exchange.class), any(Request.class));

    // Initial round-trip
    Exchange first = newExchange();
    first.put("password", "hifalutin");
    Response firstResponse = filter.filter(first, newRequest(), terminalHandler).getOrThrow();

    // Usage of cached value
    Exchange second = newExchange();
    Response secondResponse = filter.filter(second, newRequest(), terminalHandler).getOrThrow();

    // Cached value is no longer valid, trigger a user/pass refresh
    Exchange third = newExchange();
    third.put("password", "hifalutin2");
    Response thirdResponse = filter.filter(third, newRequest(), terminalHandler).getOrThrow();

    // Terminal handler should be called 5 times, not 6
    // first: 2 times
    // second: 1 time
    // third: 2 times
    verify(terminalHandler, times(5)).handle(any(Exchange.class), any(Request.class));
    // Session should be updated with cached value 2 times
    verify(session).put(endsWith(":userpass"), eq(INITIAL_CREDENTIALS));
    verify(session).put(endsWith(":userpass"), eq(REFRESHED_CREDENTIALS));

    // Responses should be OK for all outgoing responses
    assertThat(firstResponse.getStatus()).isEqualTo(Status.OK);
    assertThat(secondResponse.getStatus()).isEqualTo(Status.OK);
    assertThat(thirdResponse.getStatus()).isEqualTo(Status.OK);
  }