Skip to content

Align WebFluxTags#uri() with WebMvcTags#uri() #15609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,6 +41,8 @@ public final class WebFluxTags {

private static final Tag URI_ROOT = Tag.of("uri", "root");

private static final Tag URI_UNKNOWN = Tag.of("uri", "UNKNOWN");

private static final Tag EXCEPTION_NONE = Tag.of("exception", "None");

private static final Tag OUTCOME_UNKNOWN = Tag.of("outcome", "UNKNOWN");
Expand Down Expand Up @@ -86,7 +88,10 @@ public static Tag status(ServerWebExchange exchange) {

/**
* Creates a {@code uri} tag based on the URI of the given {@code exchange}. Uses the
* {@link HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE} best matching pattern.
* {@link HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE} best matching pattern if
* available. Falling back to {@code REDIRECTION} for 3xx responses, {@code NOT_FOUND}
* for 404 responses, {@code root} for requests with no path info, and {@code UNKNOWN}
* for all other requests.
* @param exchange the exchange
* @return the uri tag derived from the exchange
*/
Expand All @@ -105,11 +110,17 @@ public static Tag uri(ServerWebExchange exchange) {
return URI_NOT_FOUND;
}
}
String path = exchange.getRequest().getPath().value();
String path = getPathInfo(exchange);
if (path.isEmpty()) {
return URI_ROOT;
}
return Tag.of("uri", path);
return URI_UNKNOWN;
}

private static String getPathInfo(ServerWebExchange exchange) {
String path = exchange.getRequest().getPath().value();
String uri = StringUtils.hasText(path) ? path : "/";
return uri.replaceAll("//+", "/").replaceAll("/$", "");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -79,6 +79,28 @@ public void uriTagToleratesCustomResponseStatus() {
assertThat(tag.getValue()).isEqualTo("root");
}

@Test
public void uriTagValueIsRootWhenRequestHasNoPatternOrPathInfo() {
Tag tag = WebFluxTags.uri(this.exchange);
assertThat(tag.getValue()).isEqualTo("root");
}

@Test
public void uriTagValueIsRootWhenRequestHasNoPatternAndSlashPathInfo() {
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
ServerWebExchange exchange = MockServerWebExchange.from(request);
Tag tag = WebFluxTags.uri(exchange);
assertThat(tag.getValue()).isEqualTo("root");
}

@Test
public void uriTagValueIsUnknownWhenRequestHasNoPatternAndNonRootPathInfo() {
MockServerHttpRequest request = MockServerHttpRequest.get("/example").build();
ServerWebExchange exchange = MockServerWebExchange.from(request);
Tag tag = WebFluxTags.uri(exchange);
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}

@Test
public void methodTagToleratesNonStandardHttpMethods() {
ServerWebExchange exchange = mock(ServerWebExchange.class);
Expand Down