From a5d20ffed7d7dd65494645f52d0e4fb251a5181f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 28 Jun 2018 14:23:41 +0100 Subject: [PATCH] Avoid NPE when creating method tag for WebFlux req with non-standard method Previously, a NullPointerException would occur when WebFluxTags attempted to create a method Tag for a request with a non-standard method. This commit updates WebFluxTags to use getMethodValue(), which will never return null, rather than getMethod(), which may return null, when determining the tag's value for the given request. Closes gh-13596 --- .../metrics/web/reactive/server/WebFluxTags.java | 2 +- .../web/reactive/server/WebFluxTagsTests.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java index 5d1fdcd069..5266508b32 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTags.java @@ -55,7 +55,7 @@ public final class WebFluxTags { * @return the method tag whose value is a capitalized method (e.g. GET). */ public static Tag method(ServerWebExchange exchange) { - return Tag.of("method", exchange.getRequest().getMethod().toString()); + return Tag.of("method", exchange.getRequest().getMethodValue()); } /** diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java index 3ac3b54e16..9db50ef6b4 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/server/WebFluxTagsTests.java @@ -21,12 +21,16 @@ import org.junit.Before; import org.junit.Test; import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.MockServerHttpRequest; import org.springframework.mock.web.server.MockServerWebExchange; import org.springframework.web.reactive.HandlerMapping; +import org.springframework.web.server.ServerWebExchange; import org.springframework.web.util.pattern.PathPatternParser; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** * Tests for {@link WebFluxTags}. @@ -80,4 +84,14 @@ public class WebFluxTagsTests { assertThat(tag.getValue()).isEqualTo("UNKNOWN"); } + @Test + public void methodTagToleratesNonStandardHttpMethods() { + ServerWebExchange exchange = mock(ServerWebExchange.class); + ServerHttpRequest request = mock(ServerHttpRequest.class); + given(exchange.getRequest()).willReturn(request); + given(request.getMethodValue()).willReturn("CUSTOM"); + Tag tag = WebFluxTags.method(exchange); + assertThat(tag.getValue()).isEqualTo("CUSTOM"); + } + }