|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright 2002-2022 the original author or authors.
|
|
|
|
|
|
+ * Copyright 2002-2023 the original author or authors.
|
|
*
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -16,17 +16,24 @@
|
|
|
|
|
|
package org.springframework.security.web;
|
|
package org.springframework.security.web;
|
|
|
|
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+import io.micrometer.observation.Observation;
|
|
import io.micrometer.observation.ObservationHandler;
|
|
import io.micrometer.observation.ObservationHandler;
|
|
import io.micrometer.observation.ObservationRegistry;
|
|
import io.micrometer.observation.ObservationRegistry;
|
|
|
|
+import jakarta.servlet.Filter;
|
|
import jakarta.servlet.FilterChain;
|
|
import jakarta.servlet.FilterChain;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
|
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
|
|
|
|
|
|
+import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.BDDMockito.given;
|
|
import static org.mockito.BDDMockito.given;
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
+import static org.mockito.Mockito.times;
|
|
import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.verifyNoInteractions;
|
|
import static org.mockito.Mockito.verifyNoInteractions;
|
|
|
|
|
|
@@ -61,4 +68,23 @@ public class ObservationFilterChainDecoratorTests {
|
|
verifyNoInteractions(handler);
|
|
verifyNoInteractions(handler);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ void decorateFiltersWhenDefaultsThenObserves() throws Exception {
|
|
|
|
+ ObservationHandler<?> handler = mock(ObservationHandler.class);
|
|
|
|
+ given(handler.supportsContext(any())).willReturn(true);
|
|
|
|
+ ObservationRegistry registry = ObservationRegistry.create();
|
|
|
|
+ registry.observationConfig().observationHandler(handler);
|
|
|
|
+ ObservationFilterChainDecorator decorator = new ObservationFilterChainDecorator(registry);
|
|
|
|
+ FilterChain chain = mock(FilterChain.class);
|
|
|
|
+ Filter filter = mock(Filter.class);
|
|
|
|
+ FilterChain decorated = decorator.decorate(chain, List.of(filter));
|
|
|
|
+ decorated.doFilter(new MockHttpServletRequest("GET", "/"), new MockHttpServletResponse());
|
|
|
|
+ verify(handler, times(2)).onStart(any());
|
|
|
|
+ ArgumentCaptor<Observation.Event> event = ArgumentCaptor.forClass(Observation.Event.class);
|
|
|
|
+ verify(handler, times(2)).onEvent(event.capture(), any());
|
|
|
|
+ List<Observation.Event> events = event.getAllValues();
|
|
|
|
+ assertThat(events.get(0).getName()).isEqualTo(filter.getClass().getSimpleName() + ".before");
|
|
|
|
+ assertThat(events.get(1).getName()).isEqualTo(filter.getClass().getSimpleName() + ".after");
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|