Merge pull request #1590 from yneth/master

This commit is contained in:
Rossen Stoyanchev 2017-11-09 17:17:04 -05:00
commit 988c5f2b56
3 changed files with 32 additions and 19 deletions

View File

@ -18,58 +18,70 @@ package org.springframework.test.web.reactive.server.samples;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.web.reactive.server.WebTestClient; 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.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
* Tests with custom headers. * Tests with headers and cookies.
*
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 5.0 * @since 5.0
*/ */
public class HeaderTests { public class HeaderAndCookieTests {
private final WebTestClient client = WebTestClient private final WebTestClient client = WebTestClient.bindToController(new TestController()).build();
.bindToController(new TestController())
.configureClient().baseUrl("/header")
.build();
@Test @Test
public void requestResponseHeaderPair() throws Exception { 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() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectHeader().valueEquals("h1", "in-out"); .expectHeader().valueEquals("h1", "in-out");
} }
@Test @Test
public void headerMultivalue() throws Exception { public void headerMultipleValues() throws Exception {
this.client.get().uri("/multivalue") this.client.get().uri("header-multi-value")
.exchange() .exchange()
.expectStatus().isOk() .expectStatus().isOk()
.expectHeader().valueEquals("h1", "v1", "v2", "v3"); .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 @RestController
@RequestMapping("header")
static class TestController { static class TestController {
@GetMapping("request-response-pair") @GetMapping("header-echo")
ResponseEntity<Void> handleHeader(@RequestHeader("h1") String myHeader) { ResponseEntity<Void> handleHeader(@RequestHeader("h1") String myHeader) {
String value = myHeader + "-out"; String value = myHeader + "-out";
return ResponseEntity.ok().header("h1", value).build(); return ResponseEntity.ok().header("h1", value).build();
} }
@GetMapping("multivalue") @GetMapping("header-multi-value")
ResponseEntity<Void> multivalue() { ResponseEntity<Void> multiValue() {
return ResponseEntity.ok().header("h1", "v1", "v2", "v3").build(); return ResponseEntity.ok().header("h1", "v1", "v2", "v3").build();
} }
@GetMapping("cookie-echo")
ResponseEntity<Void> handleCookie(@CookieValue("k1") String cookieValue) {
HttpHeaders headers = new HttpHeaders();
headers.set("Set-Cookie", "k1=" + cookieValue);
return new ResponseEntity<>(headers, HttpStatus.OK);
}
} }
} }

View File

@ -270,10 +270,9 @@ class DefaultWebClient implements WebClient {
} }
@Override @Override
public DefaultRequestBodyUriSpec cookies( public DefaultRequestBodyUriSpec cookies(Consumer<MultiValueMap<String, String>> cookiesConsumer) {
Consumer<MultiValueMap<String, String>> cookiesConsumer) {
Assert.notNull(cookiesConsumer, "'cookiesConsumer' must not be null"); Assert.notNull(cookiesConsumer, "'cookiesConsumer' must not be null");
cookiesConsumer.accept(this.cookies); cookiesConsumer.accept(getCookies());
return this; return this;
} }

View File

@ -88,7 +88,9 @@ public class DefaultWebClientTests {
@Test @Test
public void requestHeaderAndCookie() throws Exception { public void requestHeaderAndCookie() throws Exception {
WebClient client = builder().build(); 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(); ClientRequest request = verifyExchange();
assertEquals("application/json", request.headers().getFirst("Accept")); assertEquals("application/json", request.headers().getFirst("Accept"));