@Test // SPR-14992 public void writeAndAutoFlushBeforeComplete() { Mono<String> result = this.webClient.get() .uri("/write-and-never-complete") .retrieve() .bodyToFlux(String.class) .next(); StepVerifier.create(result) .expectNextMatches(s -> s.startsWith("0123456789")) .expectComplete() .verify(Duration.ofSeconds(10L)); }
@Test // SPR-16494 @Ignore // https://github.com/reactor/reactor-netty/issues/283 public void serverDetectsClientDisconnect() { assumeTrue(this.server instanceof ReactorHttpServer); Flux<String> result = this.webClient.get() .uri("/infinite") .accept(TEXT_EVENT_STREAM) .retrieve() .bodyToFlux(String.class); StepVerifier.create(result) .expectNext("foo 0") .expectNext("foo 1") .thenCancel() .verify(Duration.ofSeconds(5L)); SseController controller = this.wac.getBean(SseController.class); controller.cancellation.block(Duration.ofSeconds(5)); }
@Test public void writeAndFlushWith() { Mono<String> result = this.webClient.get() .uri("/write-and-flush") .retrieve() .bodyToFlux(String.class) .takeUntil(s -> s.endsWith("data1")) .reduce((s1, s2) -> s1 + s2); StepVerifier.create(result) .expectNext("data0data1") .expectComplete() .verify(Duration.ofSeconds(10L)); }
@Test // SPR-15946 public void shouldGetErrorSignalOnEmptyErrorResponse() { prepareResponse(response -> response.setResponseCode(404) .setHeader("Content-Type", "text/plain")); Mono<String> result = this.webClient.get().uri("/greeting") .retrieve() .bodyToMono(String.class); StepVerifier.create(result) .expectError(WebClientResponseException.class) .verify(Duration.ofSeconds(3)); expectRequestCount(1); expectRequest(request -> { assertEquals("*/*", request.getHeader(HttpHeaders.ACCEPT)); assertEquals("/greeting", request.getPath()); }); }
private void testOnStatus(Throwable expected, Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) { HttpStatus errorStatus = HttpStatus.BAD_GATEWAY; this.server.enqueue(new MockResponse() .setResponseCode(errorStatus.value()) .setHeader("Content-Type", "application/json") .setChunkedBody("{\"error\" : {\"status\" : 502, \"message\" : \"Bad gateway.\"}}", 5)); Mono<String> mono = this.webClient.get() .uri("/json").accept(MediaType.APPLICATION_JSON) .retrieve() .onStatus(status -> status.equals(errorStatus), exceptionFunction) .bodyToMono(String.class); StepVerifier.create(mono).expectErrorSatisfies(actual -> assertSame(expected, actual)).verify(DELAY); assertEquals(1, this.server.getRequestCount()); }
@Test public void sseAsPerson() { Flux<Person> result = this.webClient.get() .uri("/person") .accept(TEXT_EVENT_STREAM) .retrieve() .bodyToFlux(Person.class); StepVerifier.create(result) .expectNext(new Person("foo 0")) .expectNext(new Person("foo 1")) .thenCancel() .verify(Duration.ofSeconds(5L)); }
@Test public void jsonStreaming() { Flux<Person> result = this.webClient.get() .uri("/stream") .accept(APPLICATION_STREAM_JSON) .retrieve() .bodyToFlux(Person.class); StepVerifier.create(result) .expectNext(new Person("foo 0")) .expectNext(new Person("foo 1")) .thenCancel() .verify(); }
@Test public void requestBodyFlux() { Mono<String> result = webClient .post() .uri("/requestBodyFlux") .syncBody(generateBody()) .retrieve() .bodyToMono(String.class); StepVerifier.create(result) .consumeNextWith(body -> assertEquals( "[fieldPart,fileParts:foo.txt,fileParts:logo.png,jsonPart]", body)) .verifyComplete(); }
@Test public void sseAsPerson() { Flux<Person> result = this.webClient.get() .uri("/person") .accept(TEXT_EVENT_STREAM) .retrieve() .bodyToFlux(Person.class); StepVerifier.create(result) .expectNext(new Person("foo 0")) .expectNext(new Person("foo 1")) .expectComplete() .verify(Duration.ofSeconds(5L)); }
@Test public void modelAttribute() { Mono<String> result = webClient .post() .uri("/modelAttribute") .syncBody(generateBody()) .retrieve() .bodyToMono(String.class); StepVerifier.create(result) .consumeNextWith(body -> assertEquals( "FormBean[fieldValue,[fileParts:foo.txt,fileParts:logo.png]]", body)) .verifyComplete(); }
@Test public void requestBodyMap() { Mono<String> result = webClient .post() .uri("/requestBodyMap") .syncBody(generateBody()) .retrieve() .bodyToMono(String.class); StepVerifier.create(result) .consumeNextWith(body -> assertEquals( "Map[[fieldPart],[fileParts:foo.txt,fileParts:logo.png],[jsonPart]]", body)) .verifyComplete(); }
@Test public void sseAsEventWithoutAcceptHeader() { Flux<ServerSentEvent<Person>> result = this.webClient.get() .uri("/event") .accept(TEXT_EVENT_STREAM) .retrieve() .bodyToFlux(new ParameterizedTypeReference<ServerSentEvent<Person>>() {}); verifyPersonEvents(result); }
@Test public void sseAsEvent() { Assume.assumeTrue(server instanceof JettyHttpServer); Flux<ServerSentEvent<Person>> result = this.webClient.get() .uri("/event") .accept(TEXT_EVENT_STREAM) .retrieve() .bodyToFlux(new ParameterizedTypeReference<ServerSentEvent<Person>>() {}); verifyPersonEvents(result); }
@Test public void sseAsString() { Flux<String> result = this.webClient.get() .uri("/string") .accept(TEXT_EVENT_STREAM) .retrieve() .bodyToFlux(String.class); StepVerifier.create(result) .expectNext("foo 0") .expectNext("foo 1") .thenCancel() .verify(Duration.ofSeconds(5L)); }
@Test public void smileStreaming() { Flux<Person> result = this.webClient.get() .uri("/stream") .accept(new MediaType("application", "stream+x-jackson-smile")) .retrieve() .bodyToFlux(Person.class); StepVerifier.create(result) .expectNext(new Person("foo 0")) .expectNext(new Person("foo 1")) .thenCancel() .verify(); }
@Test public void sseAsString() { Flux<String> result = this.webClient.get() .uri("/string") .accept(TEXT_EVENT_STREAM) .retrieve() .bodyToFlux(String.class); StepVerifier.create(result) .expectNext("foo 0") .expectNext("foo 1") .expectComplete() .verify(Duration.ofSeconds(5L)); }
@Test public void bodyToMonoVoid() { this.server.enqueue(new MockResponse() .setResponseCode(201) .setHeader("Content-Type", "application/json") .setChunkedBody("{\"foo\" : {\"bar\" : \"123\", \"baz\" : \"456\"}}", 5)); Mono<Void> mono = this.webClient.get() .uri("/json").accept(MediaType.APPLICATION_JSON) .retrieve() .bodyToMono(Void.class); StepVerifier.create(mono).expectComplete().verify(Duration.ofSeconds(3)); assertEquals(1, this.server.getRequestCount()); }
@Test // SPR-17482 public void bodyToMonoVoidWithoutContentType() { this.server.enqueue(new MockResponse() .setResponseCode(HttpStatus.ACCEPTED.value()) .setChunkedBody("{\"foo\" : \"123\", \"baz\" : \"456\", \"baz\" : \"456\"}", 5)); Mono<Map<String, String>> mono = this.webClient.get() .uri("/sample").accept(MediaType.APPLICATION_JSON) .retrieve() .bodyToMono(new ParameterizedTypeReference<Map<String, String>>() {}); StepVerifier.create(mono).expectError(UnsupportedMediaTypeException.class).verify(Duration.ofSeconds(3)); assertEquals(1, this.server.getRequestCount()); }
@Test public void defaultInstance() { Mono<Msg> result = this.webClient.get() .uri("/default-instance") .retrieve() .bodyToMono(Msg.class); StepVerifier.create(result) .verifyComplete(); }
@Test public void empty() { Mono<Msg> result = this.webClient.get() .uri("/empty") .retrieve() .bodyToMono(Msg.class); StepVerifier.create(result) .verifyComplete(); }