From cb34b0b963f4c89ecbe31b78aa594de0a5fbb348 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 9 Nov 2017 17:16:31 -0500 Subject: [PATCH] Polish --- .../reactive/server/samples/CookieTests.java | 64 ------------------- ...erTests.java => HeaderAndCookieTests.java} | 42 +++++++----- .../function/client/DefaultWebClient.java | 3 +- .../client/DefaultWebClientTests.java | 4 +- 4 files changed, 31 insertions(+), 82 deletions(-) delete mode 100644 spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/CookieTests.java rename spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/{HeaderTests.java => HeaderAndCookieTests.java} (61%) 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 deleted file mode 100644 index c0f63f6f7dd..00000000000 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/CookieTests.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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-test/src/test/java/org/springframework/test/web/reactive/server/samples/HeaderTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/HeaderAndCookieTests.java similarity index 61% rename from spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/HeaderTests.java rename to spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/HeaderAndCookieTests.java index dcc96501579..301a998b872 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/HeaderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/HeaderAndCookieTests.java @@ -18,58 +18,70 @@ 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.RequestHeader; -import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** - * Tests with custom headers. - * + * Tests with headers and cookies. * @author Rossen Stoyanchev * @since 5.0 */ -public class HeaderTests { +public class HeaderAndCookieTests { - private final WebTestClient client = WebTestClient - .bindToController(new TestController()) - .configureClient().baseUrl("/header") - .build(); + private final WebTestClient client = WebTestClient.bindToController(new TestController()).build(); @Test public void requestResponseHeaderPair() throws Exception { - this.client.get().uri("/request-response-pair").header("h1", "in") + this.client.get().uri("/header-echo").header("h1", "in") .exchange() .expectStatus().isOk() .expectHeader().valueEquals("h1", "in-out"); } @Test - public void headerMultivalue() throws Exception { - this.client.get().uri("/multivalue") + public void headerMultipleValues() throws Exception { + this.client.get().uri("header-multi-value") .exchange() .expectStatus().isOk() .expectHeader().valueEquals("h1", "v1", "v2", "v3"); } + @Test + public void setCookies() { + this.client.get().uri("/cookie-echo") + .cookies(cookies -> cookies.add("k1", "v1")) + .exchange() + .expectHeader().valueMatches("Set-Cookie", "k1=v1"); + } + @RestController - @RequestMapping("header") static class TestController { - @GetMapping("request-response-pair") + @GetMapping("header-echo") ResponseEntity handleHeader(@RequestHeader("h1") String myHeader) { String value = myHeader + "-out"; return ResponseEntity.ok().header("h1", value).build(); } - @GetMapping("multivalue") - ResponseEntity multivalue() { + @GetMapping("header-multi-value") + ResponseEntity multiValue() { return ResponseEntity.ok().header("h1", "v1", "v2", "v3").build(); } + + @GetMapping("cookie-echo") + 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 4f1cd48e383..35a2fc54857 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 @@ -270,8 +270,7 @@ class DefaultWebClient implements WebClient { } @Override - public DefaultRequestBodyUriSpec cookies( - Consumer> cookiesConsumer) { + public DefaultRequestBodyUriSpec cookies(Consumer> cookiesConsumer) { Assert.notNull(cookiesConsumer, "'cookiesConsumer' must not be null"); cookiesConsumer.accept(getCookies()); return this; diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultWebClientTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultWebClientTests.java index 12b77b51139..9b7810cb0e0 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultWebClientTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultWebClientTests.java @@ -88,7 +88,9 @@ public class DefaultWebClientTests { @Test public void requestHeaderAndCookie() throws Exception { WebClient client = builder().build(); - client.get().uri("/path").accept(MediaType.APPLICATION_JSON).cookie("id", "123").exchange(); + client.get().uri("/path").accept(MediaType.APPLICATION_JSON) + .cookies(cookies -> cookies.add("id", "123")) // SPR-16178 + .exchange(); ClientRequest request = verifyExchange(); assertEquals("application/json", request.headers().getFirst("Accept"));