From b487db294ba83f875625b761db4e013e19791c4e Mon Sep 17 00:00:00 2001 From: anton_bondarenko Date: Thu, 9 Nov 2017 00:25:38 +0200 Subject: [PATCH] Fix NullPointer when setting cookies in RequestUriSpec --- .../reactive/server/samples/CookieTests.java | 64 +++++++++++++++++++ .../function/client/DefaultWebClient.java | 2 +- 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/CookieTests.java diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/CookieTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/CookieTests.java new file mode 100644 index 0000000000..c0f63f6f7d --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/CookieTests.java @@ -0,0 +1,64 @@ +/* + * Copyright 2002-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.web.reactive.server.samples; + +import org.junit.Test; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.bind.annotation.CookieValue; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Tests with error status codes or error conditions. + * + * @author Anton Bondarenko + * @since 5.0 + */ +public class CookieTests { + + private final WebTestClient client = WebTestClient + .bindToController(new TestController()) + .configureClient().baseUrl("/cookie") + .build(); + + @Test + public void setCookies() { + this.client.get().uri("/send-cookies") + .cookies(cookies -> cookies.add("k1", "v1")) + .exchange() + .expectHeader().valueMatches("Set-Cookie", "k1=v1"); + } + + @RestController + @RequestMapping("cookie") + static class TestController { + + @GetMapping("send-cookies") + ResponseEntity handleCookie(@CookieValue("k1") String cookieValue) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Set-Cookie", "k1=" + cookieValue); + return new ResponseEntity<>(headers, HttpStatus.OK); + } + + } + +} diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java index 89bf43c9a6..4f1cd48e38 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java @@ -273,7 +273,7 @@ class DefaultWebClient implements WebClient { public DefaultRequestBodyUriSpec cookies( Consumer> cookiesConsumer) { Assert.notNull(cookiesConsumer, "'cookiesConsumer' must not be null"); - cookiesConsumer.accept(this.cookies); + cookiesConsumer.accept(getCookies()); return this; }