ClientRequest.attribute(name) defensively handles null value
Issue: SPR-17486
This commit is contained in:
parent
b3aad549c9
commit
d96a7b4bfc
|
@ -86,13 +86,7 @@ public interface ClientRequest {
|
||||||
* @return the attribute value
|
* @return the attribute value
|
||||||
*/
|
*/
|
||||||
default Optional<Object> attribute(String name) {
|
default Optional<Object> attribute(String name) {
|
||||||
Map<String, Object> attributes = attributes();
|
return Optional.ofNullable(attributes().get(name));
|
||||||
if (attributes.containsKey(name)) {
|
|
||||||
return Optional.of(attributes.get(name));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -62,7 +62,6 @@ public class DefaultWebClientTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void basic() {
|
public void basic() {
|
||||||
|
|
||||||
this.builder.build().get().uri("/path").exchange();
|
this.builder.build().get().uri("/path").exchange();
|
||||||
|
|
||||||
ClientRequest request = verifyAndGetRequest();
|
ClientRequest request = verifyAndGetRequest();
|
||||||
|
@ -73,7 +72,6 @@ public class DefaultWebClientTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void uriBuilder() {
|
public void uriBuilder() {
|
||||||
|
|
||||||
this.builder.build().get()
|
this.builder.build().get()
|
||||||
.uri(builder -> builder.path("/path").queryParam("q", "12").build())
|
.uri(builder -> builder.path("/path").queryParam("q", "12").build())
|
||||||
.exchange();
|
.exchange();
|
||||||
|
@ -85,7 +83,6 @@ public class DefaultWebClientTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void uriBuilderWithPathOverride() {
|
public void uriBuilderWithPathOverride() {
|
||||||
|
|
||||||
this.builder.build().get()
|
this.builder.build().get()
|
||||||
.uri(builder -> builder.replacePath("/path").build())
|
.uri(builder -> builder.replacePath("/path").build())
|
||||||
.exchange();
|
.exchange();
|
||||||
|
@ -97,7 +94,6 @@ public class DefaultWebClientTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void requestHeaderAndCookie() {
|
public void requestHeaderAndCookie() {
|
||||||
|
|
||||||
this.builder.build().get().uri("/path").accept(MediaType.APPLICATION_JSON)
|
this.builder.build().get().uri("/path").accept(MediaType.APPLICATION_JSON)
|
||||||
.cookies(cookies -> cookies.add("id", "123")) // SPR-16178
|
.cookies(cookies -> cookies.add("id", "123")) // SPR-16178
|
||||||
.exchange();
|
.exchange();
|
||||||
|
@ -110,7 +106,6 @@ public class DefaultWebClientTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultHeaderAndCookie() {
|
public void defaultHeaderAndCookie() {
|
||||||
|
|
||||||
WebClient client = this.builder
|
WebClient client = this.builder
|
||||||
.defaultHeader("Accept", "application/json").defaultCookie("id", "123")
|
.defaultHeader("Accept", "application/json").defaultCookie("id", "123")
|
||||||
.build();
|
.build();
|
||||||
|
@ -125,7 +120,6 @@ public class DefaultWebClientTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultHeaderAndCookieOverrides() {
|
public void defaultHeaderAndCookieOverrides() {
|
||||||
|
|
||||||
WebClient client = this.builder
|
WebClient client = this.builder
|
||||||
.defaultHeader("Accept", "application/json")
|
.defaultHeader("Accept", "application/json")
|
||||||
.defaultCookie("id", "123")
|
.defaultCookie("id", "123")
|
||||||
|
@ -141,7 +135,6 @@ public class DefaultWebClientTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultRequest() {
|
public void defaultRequest() {
|
||||||
|
|
||||||
ThreadLocal<String> context = new NamedThreadLocal<>("foo");
|
ThreadLocal<String> context = new NamedThreadLocal<>("foo");
|
||||||
|
|
||||||
Map<String, Object> actual = new HashMap<>();
|
Map<String, Object> actual = new HashMap<>();
|
||||||
|
@ -176,7 +169,6 @@ public class DefaultWebClientTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void mutateDoesCopy() {
|
public void mutateDoesCopy() {
|
||||||
|
|
||||||
// First, build the clients
|
// First, build the clients
|
||||||
|
|
||||||
WebClient.Builder builder = WebClient.builder()
|
WebClient.Builder builder = WebClient.builder()
|
||||||
|
@ -216,8 +208,7 @@ public class DefaultWebClientTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void attributes() {
|
public void withStringAttribute() {
|
||||||
|
|
||||||
Map<String, Object> actual = new HashMap<>();
|
Map<String, Object> actual = new HashMap<>();
|
||||||
ExchangeFilterFunction filter = (request, next) -> {
|
ExchangeFilterFunction filter = (request, next) -> {
|
||||||
actual.putAll(request.attributes());
|
actual.putAll(request.attributes());
|
||||||
|
@ -230,11 +221,32 @@ public class DefaultWebClientTests {
|
||||||
.exchange();
|
.exchange();
|
||||||
|
|
||||||
assertEquals("bar", actual.get("foo"));
|
assertEquals("bar", actual.get("foo"));
|
||||||
|
|
||||||
|
ClientRequest request = verifyAndGetRequest();
|
||||||
|
assertEquals("bar", request.attribute("foo").get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void withNullAttribute() {
|
||||||
|
Map<String, Object> actual = new HashMap<>();
|
||||||
|
ExchangeFilterFunction filter = (request, next) -> {
|
||||||
|
actual.putAll(request.attributes());
|
||||||
|
return next.exchange(request);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.builder.filter(filter).build()
|
||||||
|
.get().uri("/path")
|
||||||
|
.attribute("foo", null)
|
||||||
|
.exchange();
|
||||||
|
|
||||||
|
assertNull(actual.get("foo"));
|
||||||
|
|
||||||
|
ClientRequest request = verifyAndGetRequest();
|
||||||
|
assertFalse(request.attribute("foo").isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void apply() {
|
public void apply() {
|
||||||
|
|
||||||
WebClient client = this.builder
|
WebClient client = this.builder
|
||||||
.apply(builder -> builder
|
.apply(builder -> builder
|
||||||
.defaultHeader("Accept", "application/json")
|
.defaultHeader("Accept", "application/json")
|
||||||
|
|
Loading…
Reference in New Issue