Java Code Examples for org.springframework.test.web.reactive.server.EntityExchangeResult

Following code examples demonstrate how to use org.springframework.test.web.reactive.server.EntityExchangeResultfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.test.web.reactive.server.EntityExchangeResultand various code implementation of this class.

	public static void main(String[] args) {

		int toBeSent = 2;
		Flux<String> flux = Flux.range(1, toBeSent).map(count -> {
			String result = "{\"content\":" + "\"" + count + "\"}";
			if (count == 1) {
				return "[" + result;
			}
			else if (count == toBeSent) {
				return "," + result + "]";
			}
			else {
				return "," + result;
			}
		});

		EntityExchangeResult<Void> result = WebTestClient.bindToServer().build()
				.contentType(MediaType.APPLICATION_JSON)
				.body(flux, String.class).exchange()
				.expectStatus().isOk()
				.expectBody().isEmpty();

		System.out.println(result);
	} 


	protected void assertErrors(EntityExchangeResult<TestErrorResponse> errorResponseResult, String... fields) {
		
		TestErrorResponse response = errorResponseResult.getResponseBody();
		assertEquals(fields.length, response.getErrors().size());
		
		assertTrue(response.getErrors().stream()
				.map(TestLemonFieldError::getField).collect(Collectors.toSet())
				.containsAll(Arrays.asList(fields)));
	} 

    public void whenUserNameIsBaeldung_thenWebFilterIsApplied() {
        EntityExchangeResult<String> result = webTestClient.get().uri("/users/baeldung")
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class)
                .returnResult();

        assertEquals(result.getResponseBody(), "baeldung");
        assertEquals(result.getResponseHeaders().getFirst("web-filter"), "web-filter-test");
    } 

    public void whenUserNameIsBaeldung_thenWebFilterIsApplied() {
        EntityExchangeResult<String> result = webTestClient.get().uri("/users/baeldung")
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class)
                .returnResult();

        assertEquals(result.getResponseBody(), "baeldung");
        assertEquals(result.getResponseHeaders().getFirst("web-filter"), "web-filter-test");
    } 

	public void test() {
		User testUser = new User();
		testUser.setId("testId");
		testUser.setPw("testPw");
		testUser.setName("Test Name");
		testUser.setAge(25);
		System.out.println(testUser.toJsonString());

		jwtToken = JwtService.generateJwtString(testUser);
		System.out.println(jwtToken);

		EntityExchangeResult<String> result = webTestClient.get()
				.uri("/jwt/check")
				.header("X-Auth-Token", jwtToken)
				.exchange()
				.expectStatus().isOk()
				.expectBody(String.class)
				.returnResult();

		System.out.println(result);
	} 

    public void returnRequestedObject(){

        EntityExchangeResult<CurrencyCode> result = client.post().uri("/currencies/get-current-currency-value-command")
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .exchange()
                .expectStatus().isOk()
                .expectBody(CurrencyCode.class)
                .returnResult();
    } 

    public void whenPlayerNameIsBaeldung_thenWebFilterIsApplied() {
        EntityExchangeResult<String> result = webTestClient.get().uri("/players/baeldung")
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class)
                .returnResult();

        assertEquals(result.getResponseBody(), "baeldung");
        assertEquals(result.getResponseHeaders().getFirst("web-filter"), "web-filter-test");
    } 

Advertisement
Javadoc
ExchangeResult sub-class that exposes the response body fully extracted to a representation of type

. @author Rossen Stoyanchev @since 5.0 @param the response body type @see FluxExchangeResult

Read More
Advertisement