JAVA-10376 Code Mismatch | Exploring the Spring 5 WebFlux URL Matching (#12509)

* JAVA-10376 Code Mismatch | Exploring the Spring 5 WebFlux URL Matching

* JAVA-10376 Code Mismatch | Exploring the Spring 5 WebFlux URL Matching
This commit is contained in:
anuragkumawat 2022-07-29 19:14:40 +05:30 committed by GitHub
parent 67c9cfec23
commit e97b4c899a
4 changed files with 30 additions and 10 deletions

View File

@ -24,11 +24,12 @@ public class ExploreSpring5URLPatternUsingRouterFunctions {
private RouterFunction<ServerResponse> routingFunction() { private RouterFunction<ServerResponse> routingFunction() {
return route(GET("/p?ths"), serverRequest -> ok().body(fromValue("/p?ths"))).andRoute(GET("/test/{*id}"), serverRequest -> ok().body(fromValue(serverRequest.pathVariable("id")))) return route(GET("/t?st"), serverRequest -> ok().body(fromValue("Path /t?st is accessed"))).andRoute(GET("/test/{*id}"), serverRequest -> ok().body(fromValue(serverRequest.pathVariable("id"))))
.andRoute(GET("/*card"), serverRequest -> ok().body(fromValue("/*card path was accessed"))) .andRoute(GET("/baeldung/*Id"), serverRequest -> ok().body(fromValue("/baeldung/*Id path was accessed")))
.andRoute(GET("/{var1}_{var2}"), serverRequest -> ok().body(fromValue(serverRequest.pathVariable("var1") + " , " + serverRequest.pathVariable("var2")))) .andRoute(GET("/{var1}_{var2}"), serverRequest -> ok().body(fromValue(serverRequest.pathVariable("var1") + " , " + serverRequest.pathVariable("var2"))))
.andRoute(GET("/{baeldung:[a-z]+}"), serverRequest -> ok().body(fromValue("/{baeldung:[a-z]+} was accessed and baeldung=" + serverRequest.pathVariable("baeldung")))) .andRoute(GET("/{baeldung:[a-z]+}"), serverRequest -> ok().body(fromValue("/{baeldung:[a-z]+} was accessed and baeldung=" + serverRequest.pathVariable("baeldung"))))
.and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/"))); .and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/")))
.and(RouterFunctions.resources("/resources/**", new ClassPathResource("resources/")));
} }
WebServer start() throws Exception { WebServer start() throws Exception {

View File

@ -27,12 +27,12 @@ public class ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest {
@Test @Test
public void givenRouter_whenGetPathWithSingleCharWildcard_thenGotPathPattern() throws Exception { public void givenRouter_whenGetPathWithSingleCharWildcard_thenGotPathPattern() throws Exception {
client.get() client.get()
.uri("/paths") .uri("/test")
.exchange() .exchange()
.expectStatus() .expectStatus()
.isOk() .isOk()
.expectBody(String.class) .expectBody(String.class)
.isEqualTo("/p?ths"); .isEqualTo("Path /t?st is accessed");
} }
@Test @Test
@ -50,12 +50,12 @@ public class ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest {
public void givenRouter_whenGetMultipleCharWildcard_thenGotPathPattern() throws Exception { public void givenRouter_whenGetMultipleCharWildcard_thenGotPathPattern() throws Exception {
client.get() client.get()
.uri("/wildcard") .uri("/baeldung/tutorialId")
.exchange() .exchange()
.expectStatus() .expectStatus()
.isOk() .isOk()
.expectBody(String.class) .expectBody(String.class)
.isEqualTo("/*card path was accessed"); .isEqualTo("/baeldung/*Id path was accessed");
} }
@Test @Test
@ -107,4 +107,14 @@ public class ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest {
.isEqualTo("hello"); .isEqualTo("hello");
} }
@Test
public void givenRouter_whenAccess_thenGot() throws Exception {
client.get()
.uri("/resources/test/test.txt")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("test");
}
} }

View File

@ -27,13 +27,21 @@ public class PathPatternsUsingHandlerMethodIntegrationTest {
@Test @Test
public void givenHandlerMethod_whenMultipleURIVariablePattern_then200() { public void givenHandlerMethod_whenMultipleURIVariablePattern_then200() {
client.get() client.get()
.uri("/spring5/ab/cd") .uri("/spring5/baeldung/tutorial")
.exchange() .exchange()
.expectStatus() .expectStatus()
.is2xxSuccessful() .is2xxSuccessful()
.expectBody() .expectBody()
.equals("/ab/cd"); .equals("/baeldung/tutorial");
client.get()
.uri("/spring5/baeldung")
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBody()
.equals("/baeldung");
} }
@Test @Test