Updates to ExchangeActions

Add assertEntity(Foo.class).map() -> Map<String, Foo>

Add assertBody().map() -> Map<String, String>

Rename andAssert(Consumer<?>) to "consume" in assertion classes

Remove andAssert + andDo from top-level ExchangeActions
This commit is contained in:
Rossen Stoyanchev
2017-02-13 20:37:42 -05:00
parent 656e7f801e
commit 130598ffb2
8 changed files with 185 additions and 145 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.test.web.reactive.server.samples;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
@@ -22,8 +24,11 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.web.reactive.server.WebTestClient;
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;
import static org.junit.Assert.assertEquals;
/**
* Tests with custom headers.
*
@@ -37,27 +42,45 @@ public class HeaderTests {
@Before
public void setUp() throws Exception {
this.client = WebTestClient.bindToController(new TestController()).build();
this.client = WebTestClient
.bindToController(new TestController())
.webClientSpec().baseUrl("/header")
.build();
}
@Test
public void customHeader() throws Exception {
this.client.get().uri("/header").header("h1", "ping")
public void requestResponseHeaderPair() throws Exception {
this.client.get().uri("/request-response-pair").header("h1", "in")
.exchange()
.assertStatus().isOk()
.assertHeader("h1").isEqualTo("ping-pong");
.assertHeader("h1").isEqualTo("in-out");
}
@Test
public void headerConsumer() throws Exception {
this.client.get().uri("/multivalue")
.exchange()
.assertStatus().isOk()
.assertHeader("h1").consume(value -> assertEquals("v1", value))
.assertHeader("h1").values().consume(values -> assertEquals(Arrays.asList("v1", "v2", "v3"), values));
}
@RestController
@RequestMapping("header")
static class TestController {
@GetMapping("header")
@GetMapping("request-response-pair")
ResponseEntity<Void> handleHeader(@RequestHeader("h1") String myHeader) {
String value = myHeader + "-pong";
String value = myHeader + "-out";
return ResponseEntity.ok().header("h1", value).build();
}
@GetMapping("multivalue")
ResponseEntity<Void> multivalue() {
return ResponseEntity.ok().header("h1", "v1", "v2", "v3").build();
}
}
}

View File

@@ -16,6 +16,8 @@
package org.springframework.test.web.reactive.server.samples;
import java.net.URI;
import java.util.LinkedHashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -35,6 +37,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.springframework.http.MediaType.TEXT_EVENT_STREAM;
@@ -65,7 +68,7 @@ public class ResponseEntityTests {
}
@Test
public void entityCollection() throws Exception {
public void entityList() throws Exception {
this.client.get().uri("/persons")
.exchange()
.assertStatus().isOk()
@@ -75,6 +78,14 @@ public class ResponseEntityTests {
.contains(new Person("Jane"), new Person("Jason"), new Person("John"));
}
@Test
public void entityMap() throws Exception {
this.client.get().uri("/persons?map=true")
.exchange()
.assertStatus().isOk()
.assertEntity(Person.class).map().hasSize(3).containsKeys("Jane", "Jason", "John");
}
@Test
public void entityStream() throws Exception {
this.client.get().uri("/persons").accept(TEXT_EVENT_STREAM)
@@ -90,7 +101,7 @@ public class ResponseEntityTests {
}
@Test
public void saveEntity() throws Exception {
public void postEntity() throws Exception {
this.client.post().uri("/persons")
.exchange(Mono.just(new Person("John")), Person.class)
.assertStatus().isCreated()
@@ -98,6 +109,14 @@ public class ResponseEntityTests {
.assertBody().isEmpty();
}
@Test
public void entityConsumer() throws Exception {
this.client.get().uri("/persons/John")
.exchange()
.assertStatus().isOk()
.assertEntity(Person.class).consume(p -> assertEquals(new Person("John"), p));
}
@RestController
@RequestMapping("/persons")
@@ -113,6 +132,15 @@ public class ResponseEntityTests {
return Flux.just(new Person("Jane"), new Person("Jason"), new Person("John"));
}
@GetMapping(params = "map")
Map<String, Person> getPersonsAsMap() {
Map<String, Person> map = new LinkedHashMap<>();
map.put("Jane", new Person("Jane"));
map.put("Jason", new Person("Jason"));
map.put("John", new Person("John"));
return map;
}
@GetMapping(produces = "text/event-stream")
Flux<Person> getPersonStream() {
return Flux.intervalMillis(100).onBackpressureBuffer(10).map(index -> new Person("N" + index));