Add test to ensure filters are only called once.
import org.junit.Test;
import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
public class FilterTest {
@@ -65,4 +66,32 @@ public void shouldBePossibleToChainFiltersOneAfterAnother() {
// we expect to see them in reverse order.
assertThat(res.getHeaders("cheese")).containsExactly("brie", "cheddar");
}
+
+ @Test
+ public void eachFilterShouldOnlybeCalledOnce() {
+ AtomicInteger rootCalls = new AtomicInteger(0);
+
+ HttpHandler root = req -> {
+ rootCalls.incrementAndGet();
+ return new HttpResponse();
+ };
+
+ AtomicInteger filterOneCount = new AtomicInteger(0);
+ root = root.with(httpHandler -> req -> {
+ filterOneCount.incrementAndGet();
+ return httpHandler.execute(req);
+ });
+
+ AtomicInteger filterTwoCount = new AtomicInteger(0);
+ root = root.with(httpHandler -> req -> {
+ filterTwoCount.incrementAndGet();
+ return httpHandler.execute(req);
+ });
+
+ root.execute(new HttpRequest(GET, "/cheese"));
+
+ assertThat(rootCalls.get()).isEqualTo(1);
+ assertThat(filterOneCount.get()).isEqualTo(1);
+ assertThat(filterTwoCount.get()).isEqualTo(1);
+ }
}
