|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.security.web;
|
18 | 18 |
|
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import io.micrometer.observation.Observation; |
19 | 22 | import io.micrometer.observation.ObservationHandler;
|
20 | 23 | import io.micrometer.observation.ObservationRegistry;
|
| 24 | +import jakarta.servlet.Filter; |
21 | 25 | import jakarta.servlet.FilterChain;
|
22 | 26 | import org.junit.jupiter.api.Test;
|
| 27 | +import org.mockito.ArgumentCaptor; |
23 | 28 |
|
24 | 29 | import org.springframework.mock.web.MockHttpServletRequest;
|
25 | 30 | import org.springframework.mock.web.MockHttpServletResponse;
|
26 | 31 |
|
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
27 | 33 | import static org.mockito.ArgumentMatchers.any;
|
28 | 34 | import static org.mockito.BDDMockito.given;
|
29 | 35 | import static org.mockito.Mockito.mock;
|
| 36 | +import static org.mockito.Mockito.times; |
30 | 37 | import static org.mockito.Mockito.verify;
|
31 | 38 | import static org.mockito.Mockito.verifyNoInteractions;
|
32 | 39 |
|
@@ -61,4 +68,23 @@ void decorateWhenNoopThenDoesNotObserve() throws Exception {
|
61 | 68 | verifyNoInteractions(handler);
|
62 | 69 | }
|
63 | 70 |
|
| 71 | + @Test |
| 72 | + void decorateFiltersWhenDefaultsThenObserves() throws Exception { |
| 73 | + ObservationHandler<?> handler = mock(ObservationHandler.class); |
| 74 | + given(handler.supportsContext(any())).willReturn(true); |
| 75 | + ObservationRegistry registry = ObservationRegistry.create(); |
| 76 | + registry.observationConfig().observationHandler(handler); |
| 77 | + ObservationFilterChainDecorator decorator = new ObservationFilterChainDecorator(registry); |
| 78 | + FilterChain chain = mock(FilterChain.class); |
| 79 | + Filter filter = mock(Filter.class); |
| 80 | + FilterChain decorated = decorator.decorate(chain, List.of(filter)); |
| 81 | + decorated.doFilter(new MockHttpServletRequest("GET", "/"), new MockHttpServletResponse()); |
| 82 | + verify(handler, times(2)).onStart(any()); |
| 83 | + ArgumentCaptor<Observation.Event> event = ArgumentCaptor.forClass(Observation.Event.class); |
| 84 | + verify(handler, times(2)).onEvent(event.capture(), any()); |
| 85 | + List<Observation.Event> events = event.getAllValues(); |
| 86 | + assertThat(events.get(0).getName()).isEqualTo(filter.getClass().getSimpleName() + ".before"); |
| 87 | + assertThat(events.get(1).getName()).isEqualTo(filter.getClass().getSimpleName() + ".after"); |
| 88 | + } |
| 89 | + |
64 | 90 | }
|
0 commit comments