|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.test.web.reactive.server.samples; |
| 17 | + |
| 18 | +import java.nio.charset.StandardCharsets; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.springframework.http.MediaType; |
| 25 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 26 | +import org.springframework.web.bind.annotation.GetMapping; |
| 27 | +import org.springframework.web.bind.annotation.RestController; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests with a globally registered |
| 33 | + * {@link org.springframework.test.web.reactive.server.EntityExchangeResult} consumer. |
| 34 | + * |
| 35 | + * @author Rossen Stoyanchev |
| 36 | + */ |
| 37 | +public class GlobalEntityResultConsumerTests { |
| 38 | + |
| 39 | + private final StringBuilder output = new StringBuilder(); |
| 40 | + |
| 41 | + private final WebTestClient client = WebTestClient.bindToController(TestController.class) |
| 42 | + .configureClient() |
| 43 | + .entityExchangeResultConsumer(result -> { |
| 44 | + byte[] bytes = result.getResponseBodyContent(); |
| 45 | + this.output.append(new String(bytes, StandardCharsets.UTF_8)); |
| 46 | + }) |
| 47 | + .build(); |
| 48 | + |
| 49 | + |
| 50 | + @Test |
| 51 | + void json() { |
| 52 | + this.client.get().uri("/person/1") |
| 53 | + .accept(MediaType.APPLICATION_JSON) |
| 54 | + .exchange() |
| 55 | + .expectStatus().isOk() |
| 56 | + .expectBody().json("{\"name\":\"Joe\"}"); |
| 57 | + |
| 58 | + assertThat(this.output.toString()).isEqualTo("{\"name\":\"Joe\"}"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void entity() { |
| 63 | + this.client.get().uri("/person/1") |
| 64 | + .accept(MediaType.APPLICATION_JSON) |
| 65 | + .exchange() |
| 66 | + .expectStatus().isOk() |
| 67 | + .expectBody(Person.class).isEqualTo(new Person("Joe")); |
| 68 | + |
| 69 | + assertThat(this.output.toString()).isEqualTo("{\"name\":\"Joe\"}"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void entityList() { |
| 74 | + this.client.get().uri("/persons") |
| 75 | + .accept(MediaType.APPLICATION_JSON) |
| 76 | + .exchange() |
| 77 | + .expectStatus().isOk() |
| 78 | + .expectBodyList(Person.class).hasSize(2); |
| 79 | + |
| 80 | + assertThat(this.output.toString()) |
| 81 | + .isEqualTo("[{\"name\":\"Joe\"},{\"name\":\"Joseph\"}]"); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + @RestController |
| 86 | + static class TestController { |
| 87 | + |
| 88 | + @GetMapping("/person/{id}") |
| 89 | + Person getPerson() { |
| 90 | + return new Person("Joe"); |
| 91 | + } |
| 92 | + |
| 93 | + @GetMapping("/persons") |
| 94 | + List<Person> getPersons() { |
| 95 | + return Arrays.asList(new Person("Joe"), new Person("Joseph")); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments