@ -16,13 +16,10 @@
package org.springframework.boot.actuate.trace.http.reactive ;
package org.springframework.boot.actuate.trace.http.reactive ;
import java.io.IOException ;
import java.security.Principal ;
import java.security.Principal ;
import java.time.Duration ;
import java.time.Duration ;
import java.util.EnumSet ;
import java.util.EnumSet ;
import javax.servlet.ServletException ;
import org.junit.Test ;
import org.junit.Test ;
import reactor.core.publisher.Mono ;
import reactor.core.publisher.Mono ;
@ -33,10 +30,11 @@ import org.springframework.boot.actuate.trace.http.Include;
import org.springframework.boot.actuate.web.trace.reactive.HttpTraceWebFilter ;
import org.springframework.boot.actuate.web.trace.reactive.HttpTraceWebFilter ;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest ;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest ;
import org.springframework.mock.web.server.MockServerWebExchange ;
import org.springframework.mock.web.server.MockServerWebExchange ;
import org.springframework.web.server.ServerWebExchange ;
import org.springframework.web.server.ServerWebExchangeDecorator ;
import org.springframework.web.server.ServerWebExchangeDecorator ;
import org.springframework.web.server.WebFilterChain ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
import static org.mockito.BDDMockito.given ;
import static org.mockito.BDDMockito.given ;
import static org.mockito.Mockito.mock ;
import static org.mockito.Mockito.mock ;
@ -56,8 +54,8 @@ public class HttpTraceWebFilterTests {
this . tracer , EnumSet . allOf ( Include . class ) ) ;
this . tracer , EnumSet . allOf ( Include . class ) ) ;
@Test
@Test
public void filterTracesExchange ( ) throws ServletException , IOException {
public void filterTracesExchange ( ) {
this . filter . f ilter(
executeF ilter(
MockServerWebExchange
MockServerWebExchange
. from ( MockServerHttpRequest . get ( "https://api.example.com" ) ) ,
. from ( MockServerHttpRequest . get ( "https://api.example.com" ) ) ,
( exchange ) - > Mono . empty ( ) ) . block ( Duration . ofSeconds ( 30 ) ) ;
( exchange ) - > Mono . empty ( ) ) . block ( Duration . ofSeconds ( 30 ) ) ;
@ -65,9 +63,8 @@ public class HttpTraceWebFilterTests {
}
}
@Test
@Test
public void filterCapturesSessionIdWhenSessionIsUsed ( )
public void filterCapturesSessionIdWhenSessionIsUsed ( ) {
throws ServletException , IOException {
executeFilter (
this . filter . filter (
MockServerWebExchange
MockServerWebExchange
. from ( MockServerHttpRequest . get ( "https://api.example.com" ) ) ,
. from ( MockServerHttpRequest . get ( "https://api.example.com" ) ) ,
( exchange ) - > {
( exchange ) - > {
@ -82,9 +79,8 @@ public class HttpTraceWebFilterTests {
}
}
@Test
@Test
public void filterDoesNotCaptureIdOfUnusedSession ( )
public void filterDoesNotCaptureIdOfUnusedSession ( ) {
throws ServletException , IOException {
executeFilter (
this . filter . filter (
MockServerWebExchange
MockServerWebExchange
. from ( MockServerHttpRequest . get ( "https://api.example.com" ) ) ,
. from ( MockServerHttpRequest . get ( "https://api.example.com" ) ) ,
( exchange ) - > {
( exchange ) - > {
@ -97,10 +93,10 @@ public class HttpTraceWebFilterTests {
}
}
@Test
@Test
public void filterCapturesPrincipal ( ) throws ServletException , IOException {
public void filterCapturesPrincipal ( ) {
Principal principal = mock ( Principal . class ) ;
Principal principal = mock ( Principal . class ) ;
given ( principal . getName ( ) ) . willReturn ( "alice" ) ;
given ( principal . getName ( ) ) . willReturn ( "alice" ) ;
this . filter . f ilter( new ServerWebExchangeDecorator ( MockServerWebExchange
executeF ilter( new ServerWebExchangeDecorator ( MockServerWebExchange
. from ( MockServerHttpRequest . get ( "https://api.example.com" ) ) ) {
. from ( MockServerHttpRequest . get ( "https://api.example.com" ) ) ) {
@Override
@Override
@ -120,17 +116,9 @@ public class HttpTraceWebFilterTests {
assertThat ( tracedPrincipal . getName ( ) ) . isEqualTo ( "alice" ) ;
assertThat ( tracedPrincipal . getName ( ) ) . isEqualTo ( "alice" ) ;
}
}
@Test
private Mono < Void > executeFilter ( ServerWebExchange exchange , WebFilterChain chain ) {
public void statusIsAssumedToBe500WhenChainFails ( )
return this . filter . filter ( exchange , chain )
throws ServletException , IOException {
. then ( Mono . defer ( ( ) - > exchange . getResponse ( ) . setComplete ( ) ) ) ;
assertThatExceptionOfType ( Exception . class ) . isThrownBy ( ( ) - > this . filter
. filter ( MockServerWebExchange
. from ( MockServerHttpRequest . get ( "https://api.example.com" ) ) ,
( exchange ) - > Mono . error ( new RuntimeException ( ) ) )
. block ( Duration . ofSeconds ( 30 ) ) ) ;
assertThat ( this . repository . findAll ( ) ) . hasSize ( 1 ) ;
assertThat ( this . repository . findAll ( ) . get ( 0 ) . getResponse ( ) . getStatus ( ) )
. isEqualTo ( 500 ) ;
}
}
}
}