JAVA-3090: updating README.md for spring-reactive
This commit is contained in:
@@ -7,11 +7,9 @@ The "REST With Spring" Classes: https://bit.ly/restwithspring
|
||||
|
||||
### Relevant Articles
|
||||
|
||||
- [Spring 5 WebClient](https://www.baeldung.com/spring-5-webclient)
|
||||
- [Exploring the Spring 5 WebFlux URL Matching](https://www.baeldung.com/spring-5-mvc-url-matching)
|
||||
- [Reactive WebSockets with Spring 5](https://www.baeldung.com/spring-5-reactive-websockets)
|
||||
- [Spring Webflux Filters](https://www.baeldung.com/spring-webflux-filters)
|
||||
- [How to Set a Header on a Response with Spring 5](https://www.baeldung.com/spring-response-header)
|
||||
- [Handling Errors in Spring WebFlux](https://www.baeldung.com/spring-webflux-errors)
|
||||
- [A Guide to Spring Session Reactive Support: WebSession](https://www.baeldung.com/spring-session-reactive)
|
||||
- More articles: [[next -->]](/spring-5-reactive-2)
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.web.error.ErrorAttributeOptions;
|
||||
import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
|
||||
@Component
|
||||
public class GlobalErrorAttributes extends DefaultErrorAttributes{
|
||||
|
||||
private HttpStatus status = HttpStatus.BAD_REQUEST;
|
||||
private String message = "please provide a name";
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
|
||||
Map<String, Object> map = super.getErrorAttributes(request, options);
|
||||
map.put("status", getStatus());
|
||||
map.put("message", getMessage());
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the status
|
||||
*/
|
||||
public HttpStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param status the status to set
|
||||
*/
|
||||
public void setStatus(HttpStatus status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the message
|
||||
*/
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message the message to set
|
||||
*/
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.WebProperties;
|
||||
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
|
||||
import org.springframework.boot.web.error.ErrorAttributeOptions;
|
||||
import org.springframework.boot.web.reactive.error.ErrorAttributes;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
@Order(-2)
|
||||
public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler {
|
||||
|
||||
public GlobalErrorWebExceptionHandler(GlobalErrorAttributes g, ApplicationContext applicationContext,
|
||||
ServerCodecConfigurer serverCodecConfigurer) {
|
||||
super(g, new WebProperties.Resources(), applicationContext);
|
||||
super.setMessageWriters(serverCodecConfigurer.getWriters());
|
||||
super.setMessageReaders(serverCodecConfigurer.getReaders());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RouterFunction<ServerResponse> getRoutingFunction(final ErrorAttributes errorAttributes) {
|
||||
return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
|
||||
}
|
||||
|
||||
private Mono<ServerResponse> renderErrorResponse(final ServerRequest request) {
|
||||
|
||||
final Map<String, Object> errorPropertiesMap = getErrorAttributes(request, ErrorAttributeOptions.defaults());
|
||||
|
||||
return ServerResponse.status(HttpStatus.BAD_REQUEST)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(BodyInserters.fromValue(errorPropertiesMap));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
public class NameRequiredException extends ResponseStatusException {
|
||||
|
||||
public NameRequiredException(HttpStatus status, String message, Throwable e) {
|
||||
super(status, message, e);
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling.handlers;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class Handler1 {
|
||||
|
||||
public Mono<ServerResponse> handleRequest1(ServerRequest request) {
|
||||
return sayHello(request).onErrorReturn("Hello, Stranger")
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s));
|
||||
}
|
||||
|
||||
private Mono<String> sayHello(ServerRequest request) {
|
||||
try {
|
||||
return Mono.just("Hello, " + request.queryParam("name")
|
||||
.get());
|
||||
} catch (Exception e) {
|
||||
return Mono.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling.handlers;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class Handler2 {
|
||||
|
||||
public Mono<ServerResponse> handleRequest2(ServerRequest request) {
|
||||
return
|
||||
sayHello(request)
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s))
|
||||
.onErrorResume(e -> sayHelloFallback()
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s)));
|
||||
}
|
||||
|
||||
private Mono<String> sayHello(ServerRequest request) {
|
||||
try {
|
||||
return Mono.just("Hello, " + request.queryParam("name")
|
||||
.get());
|
||||
} catch (Exception e) {
|
||||
return Mono.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Mono<String> sayHelloFallback() {
|
||||
return Mono.just("Hello, Stranger");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling.handlers;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class Handler3 {
|
||||
|
||||
public Mono<ServerResponse> handleRequest3(ServerRequest request) {
|
||||
return
|
||||
sayHello(request)
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s))
|
||||
.onErrorResume(e -> (Mono.just("Hi, I looked around for your name but found: " +
|
||||
e.getMessage())).flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s)));
|
||||
}
|
||||
|
||||
private Mono<String> sayHello(ServerRequest request) {
|
||||
try {
|
||||
return Mono.just("Hello, " + request.queryParam("name")
|
||||
.get());
|
||||
} catch (Exception e) {
|
||||
return Mono.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling.handlers;
|
||||
|
||||
import com.baeldung.reactive.errorhandling.NameRequiredException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class Handler4 {
|
||||
|
||||
public Mono<ServerResponse> handleRequest4(ServerRequest request) {
|
||||
return ServerResponse.ok()
|
||||
.body(sayHello(request)
|
||||
.onErrorResume(e ->
|
||||
Mono.error(new NameRequiredException(
|
||||
HttpStatus.BAD_REQUEST, "please provide a name", e))), String.class);
|
||||
}
|
||||
|
||||
private Mono<String> sayHello(ServerRequest request) {
|
||||
try {
|
||||
return Mono.just("Hello, " + request.queryParam("name").get());
|
||||
} catch (Exception e) {
|
||||
return Mono.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling.handlers;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class Handler5 {
|
||||
|
||||
public Mono<ServerResponse> handleRequest5(ServerRequest request) {
|
||||
return ServerResponse.ok()
|
||||
.body(sayHello(request), String.class);
|
||||
|
||||
}
|
||||
|
||||
private Mono<String> sayHello(ServerRequest request) {
|
||||
return Mono.just("Hello, " + request.queryParam("name").get());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling.routers;
|
||||
|
||||
import com.baeldung.reactive.errorhandling.handlers.Handler1;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
@Component
|
||||
public class Router1 {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> routeRequest1(Handler1 handler) {
|
||||
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint1")
|
||||
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling.routers;
|
||||
|
||||
import com.baeldung.reactive.errorhandling.handlers.Handler2;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
@Component
|
||||
public class Router2 {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> routeRequest2(Handler2 handler) {
|
||||
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint2")
|
||||
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling.routers;
|
||||
|
||||
import com.baeldung.reactive.errorhandling.handlers.Handler3;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
@Component
|
||||
public class Router3 {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> routeRequest3(Handler3 handler) {
|
||||
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint3")
|
||||
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest3);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling.routers;
|
||||
|
||||
import com.baeldung.reactive.errorhandling.handlers.Handler4;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
@Component
|
||||
public class Router4 {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> routeRequest4(Handler4 handler) {
|
||||
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint4")
|
||||
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest4);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
package com.baeldung.reactive.errorhandling.routers;
|
||||
|
||||
import com.baeldung.reactive.errorhandling.handlers.Handler5;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
@Component
|
||||
public class Router5 {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> routeRequest5(Handler5 handler) {
|
||||
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint5")
|
||||
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest5);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.web.reactive;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Task {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final int id;
|
||||
|
||||
public Task(@JsonProperty("name") String name, @JsonProperty("id") int id) {
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Task{" + "name='" + name + '\'' + ", id=" + id + '}';
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.web.reactive.client;
|
||||
|
||||
public class Foo {
|
||||
|
||||
private String name;
|
||||
|
||||
public Foo() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Foo(String name) {
|
||||
super();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.web.reactive.client;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration;
|
||||
|
||||
@SpringBootApplication(exclude = { ReactiveSecurityAutoConfiguration.class })
|
||||
public class WebClientApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WebClientApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.baeldung.web.reactive.client;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
public class WebClientController {
|
||||
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
@GetMapping("/resource")
|
||||
public Map<String, String> getResource() {
|
||||
Map<String, String> response = new HashMap<>();
|
||||
response.put("field", "value");
|
||||
return response;
|
||||
}
|
||||
|
||||
@PostMapping("/resource")
|
||||
public Mono<String> postStringResource(@RequestBody Mono<String> bodyString) {
|
||||
return bodyString.map(body -> "processed-" + body);
|
||||
}
|
||||
|
||||
@PostMapping("/resource-foo")
|
||||
public Mono<String> postFooResource(@RequestBody Mono<Foo> bodyFoo) {
|
||||
return bodyFoo.map(foo -> "processedFoo-" + foo.getName());
|
||||
}
|
||||
|
||||
@PostMapping(value = "/resource-multipart", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public String handleFormUpload(@RequestPart("key1") String value1, @RequestPart("key2") String value2) {
|
||||
return "processed-" + value1 + "-" + value2;
|
||||
}
|
||||
}
|
||||
@@ -1,176 +0,0 @@
|
||||
package com.baeldung.reactive.errorhandling;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@DirtiesContext
|
||||
@WithMockUser
|
||||
@AutoConfigureWebTestClient(timeout = "10000")
|
||||
public class ErrorHandlingIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebTestClient webTestClient;
|
||||
|
||||
@Test
|
||||
public void givenErrorReturn_whenUsernamePresent_thenOk() throws IOException {
|
||||
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint1?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Tony", s);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenErrorReturn_whenNoUsername_thenOk() throws IOException {
|
||||
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint1")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Stranger", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeFallback_whenUsernamePresent_thenOk() throws IOException {
|
||||
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint2?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Tony", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeFallback_whenNoUsername_thenOk() throws IOException {
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint2")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Stranger", s);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeDynamicValue_whenUsernamePresent_thenOk() throws IOException {
|
||||
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint3?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Tony", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeDynamicValue_whenNoUsername_thenOk() throws IOException {
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint3")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hi, I looked around for your name but found: No value present", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeRethrow_whenUsernamePresent_thenOk() throws IOException {
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint4?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Tony", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeRethrow_whenNoUsername_thenOk() throws IOException {
|
||||
|
||||
webTestClient.get()
|
||||
.uri("/api/endpoint4")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isBadRequest()
|
||||
.expectHeader()
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.expectBody()
|
||||
.jsonPath("$.message")
|
||||
.isNotEmpty()
|
||||
.jsonPath("$.message")
|
||||
.isEqualTo("please provide a name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGlobalErrorHandling_whenUsernamePresent_thenOk() throws IOException {
|
||||
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint5?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Tony", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGlobalErrorHandling_whenNoUsername_thenOk() throws IOException {
|
||||
webTestClient.get()
|
||||
.uri("/api/endpoint5")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isBadRequest()
|
||||
.expectHeader()
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.expectBody()
|
||||
.jsonPath("$.message")
|
||||
.isNotEmpty()
|
||||
.jsonPath("$.message")
|
||||
.isEqualTo("please provide a name");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.web.client;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.baeldung.web.reactive.client.WebClientApplication;
|
||||
|
||||
@SpringBootTest(classes = WebClientApplication.class)
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
@@ -1,331 +0,0 @@
|
||||
package com.baeldung.web.client;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.core.codec.CodecException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ReactiveHttpOutputMessage;
|
||||
import org.springframework.http.client.reactive.ClientHttpRequest;
|
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.reactive.function.BodyInserter;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;
|
||||
import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec;
|
||||
import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
|
||||
import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
|
||||
import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
|
||||
import org.springframework.web.reactive.function.client.WebClientRequestException;
|
||||
|
||||
import com.baeldung.web.reactive.client.Foo;
|
||||
import com.baeldung.web.reactive.client.WebClientApplication;
|
||||
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.handler.timeout.ReadTimeoutException;
|
||||
import io.netty.handler.timeout.ReadTimeoutHandler;
|
||||
import io.netty.handler.timeout.WriteTimeoutHandler;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.netty.http.client.HttpClient;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
@SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class WebClientIntegrationTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
private static final String BODY_VALUE = "bodyValue";
|
||||
private static final ParameterizedTypeReference<Map<String, String>> MAP_RESPONSE_REF = new ParameterizedTypeReference<Map<String, String>>() {
|
||||
};
|
||||
|
||||
@Test
|
||||
public void givenDifferentWebClientCreationMethods_whenUsed_thenObtainExpectedResponse() {
|
||||
// WebClient creation
|
||||
WebClient client1 = WebClient.create();
|
||||
WebClient client2 = WebClient.create("http://localhost:" + port);
|
||||
WebClient client3 = WebClient.builder()
|
||||
.baseUrl("http://localhost:" + port)
|
||||
.defaultCookie("cookieKey", "cookieValue")
|
||||
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
||||
.defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080"))
|
||||
.build();
|
||||
|
||||
// response assertions
|
||||
StepVerifier.create(retrieveResponse(client1.post()
|
||||
.uri("http://localhost:" + port + "/resource")))
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(retrieveResponse(client2))
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(retrieveResponse(client3))
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
// assert response without specifying URI
|
||||
StepVerifier.create(retrieveResponse(client1))
|
||||
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
|
||||
.contains("Connection refused"))
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDifferentMethodSpecifications_whenUsed_thenObtainExpectedResponse() {
|
||||
// request specification
|
||||
RequestBodyUriSpec uriSpecPost1 = createDefaultClient().method(HttpMethod.POST);
|
||||
RequestBodyUriSpec uriSpecPost2 = createDefaultClient().post();
|
||||
RequestHeadersUriSpec<?> requestGet = createDefaultClient().get();
|
||||
|
||||
// response assertions
|
||||
StepVerifier.create(retrieveResponse(uriSpecPost1))
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(retrieveResponse(uriSpecPost2))
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(retrieveGetResponse(requestGet))
|
||||
.expectNextMatches(nextMap -> nextMap.get("field")
|
||||
.equals("value"))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDifferentUriSpecifications_whenUsed_thenObtainExpectedResponse() {
|
||||
// uri specification
|
||||
RequestBodySpec bodySpecUsingString = createDefaultPostRequest().uri("/resource");
|
||||
RequestBodySpec bodySpecUsingUriBuilder = createDefaultPostRequest().uri(uriBuilder -> uriBuilder.pathSegment("resource")
|
||||
.build());
|
||||
RequestBodySpec bodySpecusingURI = createDefaultPostRequest().uri(URI.create("http://localhost:" + port + "/resource"));
|
||||
RequestBodySpec bodySpecOverridenBaseUri = createDefaultPostRequest().uri(URI.create("/resource"));
|
||||
RequestBodySpec bodySpecOverridenBaseUri2 = WebClient.builder()
|
||||
.baseUrl("http://localhost:" + port)
|
||||
.build()
|
||||
.post()
|
||||
.uri(URI.create("/resource"));
|
||||
|
||||
// response assertions
|
||||
StepVerifier.create(retrieveResponse(bodySpecUsingString))
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(retrieveResponse(bodySpecUsingUriBuilder))
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(retrieveResponse(bodySpecusingURI))
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
// assert sending request overriding base URI
|
||||
StepVerifier.create(retrieveResponse(bodySpecOverridenBaseUri))
|
||||
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
|
||||
.contains("Connection refused"))
|
||||
.verify();
|
||||
StepVerifier.create(retrieveResponse(bodySpecOverridenBaseUri2))
|
||||
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
|
||||
.contains("Connection refused"))
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDifferentBodySpecifications_whenUsed_thenObtainExpectedResponse() {
|
||||
// request body specifications
|
||||
RequestHeadersSpec<?> headersSpecPost1 = createDefaultPostResourceRequest().body(BodyInserters.fromPublisher(Mono.just(BODY_VALUE), String.class));
|
||||
RequestHeadersSpec<?> headersSpecPost2 = createDefaultPostResourceRequest().body(BodyInserters.fromValue(BODY_VALUE));
|
||||
RequestHeadersSpec<?> headersSpecPost3 = createDefaultPostResourceRequest().bodyValue(BODY_VALUE);
|
||||
RequestHeadersSpec<?> headersSpecFooPost = createDefaultPostRequest().uri("/resource-foo")
|
||||
.body(Mono.just(new Foo("fooName")), Foo.class);
|
||||
BodyInserter<Object, ReactiveHttpOutputMessage> inserterPlainObject = BodyInserters.fromValue(new Object());
|
||||
RequestHeadersSpec<?> headersSpecPlainObject = createDefaultPostResourceRequest().body(inserterPlainObject);
|
||||
|
||||
// request body specifications - using other inserter method (multipart request)
|
||||
LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
||||
map.add("key1", "multipartValue1");
|
||||
map.add("key2", "multipartValue2");
|
||||
BodyInserter<MultiValueMap<String, Object>, ClientHttpRequest> inserterMultipart = BodyInserters.fromMultipartData(map);
|
||||
RequestHeadersSpec<?> headersSpecInserterMultipart = createDefaultPostRequest().uri("/resource-multipart")
|
||||
.body(inserterMultipart);
|
||||
|
||||
// response assertions
|
||||
StepVerifier.create(retrieveResponse(headersSpecPost1))
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(retrieveResponse(headersSpecPost2))
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(retrieveResponse(headersSpecPost3))
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(retrieveResponse(headersSpecFooPost))
|
||||
.expectNext("processedFoo-fooName")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(retrieveResponse(headersSpecInserterMultipart))
|
||||
.expectNext("processed-multipartValue1-multipartValue2")
|
||||
.verifyComplete();
|
||||
// assert error plain `new Object()` as request body
|
||||
StepVerifier.create(retrieveResponse(headersSpecPlainObject))
|
||||
.expectError(CodecException.class)
|
||||
.verify();
|
||||
// assert response for request with no body
|
||||
Mono<Map<String, String>> responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono(responseHandler -> {
|
||||
assertThat(responseHandler.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||
return responseHandler.bodyToMono(MAP_RESPONSE_REF);
|
||||
});
|
||||
StepVerifier.create(responsePostWithNoBody)
|
||||
.expectNextMatches(nextMap -> nextMap.get("error")
|
||||
.equals("Bad Request"))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPostSpecifications_whenHeadersAdded_thenObtainExpectedResponse() {
|
||||
// request header specification
|
||||
RequestHeadersSpec<?> headersSpecInserterStringWithHeaders = createDefaultPostResourceRequestResponse().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
||||
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
|
||||
.acceptCharset(StandardCharsets.UTF_8)
|
||||
.ifNoneMatch("*")
|
||||
.ifModifiedSince(ZonedDateTime.now());
|
||||
|
||||
// response assertions
|
||||
StepVerifier.create(retrieveResponse(headersSpecInserterStringWithHeaders))
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDifferentResponseSpecifications_whenUsed_thenObtainExpectedResponse() {
|
||||
ResponseSpec responseSpecPostString = createDefaultPostResourceRequestResponse().retrieve();
|
||||
Mono<String> responsePostString = responseSpecPostString.bodyToMono(String.class);
|
||||
Mono<String> responsePostString2 = createDefaultPostResourceRequestResponse().exchangeToMono(response -> {
|
||||
if (response.statusCode()
|
||||
.equals(HttpStatus.OK)) {
|
||||
return response.bodyToMono(String.class);
|
||||
} else if (response.statusCode()
|
||||
.is4xxClientError()) {
|
||||
return Mono.just("Error response");
|
||||
} else {
|
||||
return response.createException()
|
||||
.flatMap(Mono::error);
|
||||
}
|
||||
});
|
||||
Mono<String> responsePostNoBody = createDefaultPostResourceRequest().exchangeToMono(response -> {
|
||||
if (response.statusCode()
|
||||
.equals(HttpStatus.OK)) {
|
||||
return response.bodyToMono(String.class);
|
||||
} else if (response.statusCode()
|
||||
.is4xxClientError()) {
|
||||
return Mono.just("Error response");
|
||||
} else {
|
||||
return response.createException()
|
||||
.flatMap(Mono::error);
|
||||
}
|
||||
});
|
||||
Mono<Map<String, String>> responseGet = createDefaultClient().get()
|
||||
.uri("/resource")
|
||||
.retrieve()
|
||||
.bodyToMono(MAP_RESPONSE_REF);
|
||||
|
||||
// response assertions
|
||||
StepVerifier.create(responsePostString)
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(responsePostString2)
|
||||
.expectNext("processed-bodyValue")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(responsePostNoBody)
|
||||
.expectNext("Error response")
|
||||
.verifyComplete();
|
||||
StepVerifier.create(responseGet)
|
||||
.expectNextMatches(nextMap -> nextMap.get("field")
|
||||
.equals("value"))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWebClientWithTimeoutConfigurations_whenRequestUsingWronglyConfiguredPublisher_thenObtainTimeout() {
|
||||
HttpClient httpClient = HttpClient.create()
|
||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000)
|
||||
.responseTimeout(Duration.ofMillis(1000))
|
||||
.doOnConnected(conn -> conn.addHandlerLast(new ReadTimeoutHandler(1000, TimeUnit.MILLISECONDS))
|
||||
.addHandlerLast(new WriteTimeoutHandler(1000, TimeUnit.MILLISECONDS)));
|
||||
|
||||
WebClient timeoutClient = WebClient.builder()
|
||||
.baseUrl("http://localhost:" + port)
|
||||
.clientConnector(new ReactorClientHttpConnector(httpClient))
|
||||
.build();
|
||||
|
||||
RequestHeadersSpec<?> neverendingMonoBodyRequest = timeoutClient.post()
|
||||
.uri("/resource")
|
||||
.body(Mono.never(), String.class);
|
||||
|
||||
StepVerifier.create(neverendingMonoBodyRequest.retrieve()
|
||||
.bodyToMono(String.class))
|
||||
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ReadTimeoutException.class.isAssignableFrom(ex.getCause()
|
||||
.getClass()))
|
||||
.verify();
|
||||
}
|
||||
|
||||
// helper methods to create default instances
|
||||
private WebClient createDefaultClient() {
|
||||
return WebClient.create("http://localhost:" + port);
|
||||
}
|
||||
|
||||
private RequestBodyUriSpec createDefaultPostRequest() {
|
||||
return createDefaultClient().post();
|
||||
}
|
||||
|
||||
private RequestBodySpec createDefaultPostResourceRequest() {
|
||||
return createDefaultPostRequest().uri("/resource");
|
||||
}
|
||||
|
||||
private RequestHeadersSpec<?> createDefaultPostResourceRequestResponse() {
|
||||
return createDefaultPostResourceRequest().bodyValue(BODY_VALUE);
|
||||
}
|
||||
|
||||
// helper methods to retrieve a response based on different steps of the process (specs)
|
||||
private Mono<String> retrieveResponse(WebClient client) {
|
||||
return client.post()
|
||||
.uri("/resource")
|
||||
.bodyValue(BODY_VALUE)
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
}
|
||||
|
||||
private Mono<String> retrieveResponse(RequestBodyUriSpec spec) {
|
||||
return spec.uri("/resource")
|
||||
.bodyValue(BODY_VALUE)
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
}
|
||||
|
||||
private Mono<Map<String, String>> retrieveGetResponse(RequestHeadersUriSpec<?> spec) {
|
||||
return spec.uri("/resource")
|
||||
.retrieve()
|
||||
.bodyToMono(MAP_RESPONSE_REF);
|
||||
}
|
||||
|
||||
private Mono<String> retrieveResponse(RequestBodySpec spec) {
|
||||
return spec.bodyValue(BODY_VALUE)
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
}
|
||||
|
||||
private Mono<String> retrieveResponse(RequestHeadersSpec<?> spec) {
|
||||
return spec.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
}
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
package com.baeldung.web.client;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import org.springframework.web.server.WebHandler;
|
||||
|
||||
import com.baeldung.web.reactive.client.WebClientApplication;
|
||||
import com.baeldung.web.reactive.client.WebClientController;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@SpringBootTest(classes = WebClientApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class WebTestClientIntegrationTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
private WebClientController controller;
|
||||
|
||||
private final RouterFunction ROUTER_FUNCTION = RouterFunctions.route(RequestPredicates.GET("/resource"), request -> ServerResponse.ok()
|
||||
.build());
|
||||
private final WebHandler WEB_HANDLER = exchange -> Mono.empty();
|
||||
|
||||
@Test
|
||||
public void testWebTestClientWithServerWebHandler() {
|
||||
WebTestClient.bindToWebHandler(WEB_HANDLER)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWebTestClientWithRouterFunction() {
|
||||
WebTestClient.bindToRouterFunction(ROUTER_FUNCTION)
|
||||
.build()
|
||||
.get()
|
||||
.uri("/resource")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody()
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
public void testWebTestClientWithServerURL() {
|
||||
WebTestClient.bindToServer()
|
||||
.baseUrl("http://localhost:" + port)
|
||||
.build()
|
||||
.get()
|
||||
.uri("/resource")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody()
|
||||
.jsonPath("field")
|
||||
.isEqualTo("value");
|
||||
;
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
public void testWebTestClientWithApplicationContext() {
|
||||
WebTestClient.bindToApplicationContext(context)
|
||||
.build()
|
||||
.get()
|
||||
.uri("/resource")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody()
|
||||
.jsonPath("field")
|
||||
.isEqualTo("value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWebTestClientWithController() {
|
||||
WebTestClient.bindToController(controller)
|
||||
.build()
|
||||
.get()
|
||||
.uri("/resource")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody()
|
||||
.jsonPath("field")
|
||||
.isEqualTo("value");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user