BAEL-944 Exploring the Spring MVC URL Matching Improvements (#1954)

* Example Code For Evaluation Article

This is an example code for the evaluation article on "Different Types of Bean Injection in Spring"

* Added unit tests

* Minor changes to application context

* Removed code committed for evaluation article

* BAEL-944 Demonstrating the problems with new Url pattern matching in Spring 5

* BAEL-944 Demonstrating the problems with new Url pattern matching in Spring 5

* BAEL-944 Exploring the Spring MVC URL Matching Improvements

* BAEL-944 Exploring the Spring MVC URL Matching Improvements

* BAEL-944 Exploring the Spring MVC URL Matching Improvements

* BAEL-944 Code Formatting and solving build issue

* BAEL-944 Resolving build issue due to change in Spring version

* BAEL-944 Resolving build issue

* BAEL-944 Formatting code

* BAEL-944 Moving tests to correct package

* BAEL-944 Moving tests to correct package

* BAEL-944 Replacing @RequestMapping by @GetMapping

* BAEL-944 Remove unnecessary attribute name, "value" in annotations
This commit is contained in:
Mansi
2017-06-21 06:30:10 +05:30
committed by yetanotherallisonf
parent ec46b8b338
commit 91b0efccd2
11 changed files with 323 additions and 4 deletions

View File

@@ -0,0 +1,110 @@
package com.baeldung.functional;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.web.server.WebServer;
import org.springframework.test.web.reactive.server.WebTestClient;
public class ExploreSpring5URLPatternUsingRouterFunctionsTest {
private static WebTestClient client;
private static WebServer server;
@BeforeClass
public static void setup() throws Exception {
server = new ExploreSpring5URLPatternUsingRouterFunctions().start();
client = WebTestClient.bindToServer()
.baseUrl("http://localhost:" + server.getPort())
.build();
}
@AfterClass
public static void destroy() {
server.stop();
}
@Test
public void givenRouter_whenGetPathWithSingleCharWildcard_thenGotPathPattern() throws Exception {
client.get()
.uri("/paths")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("/p?ths");
}
@Test
public void givenRouter_whenMultipleURIVariablePattern_thenGotPathVariable() throws Exception {
client.get()
.uri("/test/ab/cd")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("/ab/cd");
}
@Test
public void givenRouter_whenGetMultipleCharWildcard_thenGotPathPattern() throws Exception {
client.get()
.uri("/wildcard")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("/*card path was accessed");
}
@Test
public void givenRouter_whenGetMultiplePathVaribleInSameSegment_thenGotPathVariables() throws Exception {
client.get()
.uri("/baeldung_tutorial")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("baeldung , tutorial");
}
@Test
public void givenRouter_whenGetRegexInPathVarible_thenGotPathVariable() throws Exception {
client.get()
.uri("/abcd")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("/{baeldung:[a-z]+} was accessed and baeldung=abcd");
client.get()
.uri("/1234")
.exchange()
.expectStatus()
.is4xxClientError();
}
@Test
public void givenResources_whenAccess_thenGot() throws Exception {
client.get()
.uri("/files/test/test.txt")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("test");
client.get()
.uri("/files/hello.txt")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("hello");
}
}

View File

@@ -0,0 +1,102 @@
package com.baeldung.web;
import com.baeldung.web.PathPatternController;
import com.baeldung.Spring5Application;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Spring5Application.class)
public class PathPatternsUsingHandlerMethodTest {
private static WebTestClient client;
@BeforeClass
public static void setUp() {
client = WebTestClient.bindToController(new PathPatternController())
.build();
}
@Test
public void givenHandlerMethod_whenMultipleURIVariablePattern_then200() {
client.get()
.uri("/spring5/ab/cd")
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBody()
.equals("/ab/cd");
}
@Test
public void givenHandlerMethod_whenURLWithWildcardTakingZeroOrMoreChar_then200() {
client.get()
.uri("/spring5/userid")
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBody()
.equals("/spring5/*id");
}
@Test
public void givenHandlerMethod_whenURLWithWildcardTakingExactlyOneChar_then200() {
client.get()
.uri("/string5")
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBody()
.equals("/s?ring5");
}
@Test
public void givenHandlerMethod_whenURLWithWildcardTakingZeroOrMorePathSegments_then200() {
client.get()
.uri("/resources/baeldung")
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBody()
.equals("/resources/**");
}
@Test
public void givenHandlerMethod_whenURLWithRegexInPathVariable_thenExpectedOutput() {
client.get()
.uri("/abc")
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBody()
.equals("abc");
client.get()
.uri("/123")
.exchange()
.expectStatus()
.is4xxClientError();
}
@Test
public void givenHandlerMethod_whenURLWithMultiplePathVariablesInSameSegment_then200() {
client.get()
.uri("/baeldung_tutorial")
.exchange()
.expectStatus()
.is2xxSuccessful()
.expectBody()
.equals("Two variables are var1=baeldung and var2=tutorial");
}
}