BAEL-3085 Using a slash character in Spring URLs (#7823)
* slashes in Spring URLs * cleanup * restored format of pom * using more descriptive names * using more descriptive names * slash in urls code moved to spring-mvc-simple-2 module
This commit is contained in:
parent
cc23ac598a
commit
957ad4587c
@ -18,6 +18,11 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
@ -0,0 +1,14 @@
|
||||
package com.baeldung.spring.slash;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application implements WebMvcConfigurer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.spring.slash;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("slash")
|
||||
public class SlashParsingController {
|
||||
|
||||
@GetMapping("mypaths/{anything}")
|
||||
public String pathVariable(@PathVariable("anything") String anything) {
|
||||
return anything;
|
||||
}
|
||||
|
||||
@GetMapping("all/**")
|
||||
public String allDirectories(HttpServletRequest request) {
|
||||
return request.getRequestURI()
|
||||
.split(request.getContextPath() + "/all/")[1];
|
||||
}
|
||||
|
||||
@GetMapping("all")
|
||||
public String queryParameter(@RequestParam("param") String param) {
|
||||
return param;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package com.baeldung.spring.slash;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
|
||||
@AutoConfigureMockMvc
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest
|
||||
public class SlashParsingControllerIntTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void whenUsingPathVariablemWithoutSlashes_thenStatusOk() throws Exception {
|
||||
final String stringWithoutSlashes = "noslash";
|
||||
|
||||
MvcResult mvcResult = mockMvc.perform(get("/slash/mypaths/" + stringWithoutSlashes))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn();
|
||||
|
||||
assertEquals(stringWithoutSlashes, mvcResult.getResponse()
|
||||
.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingPathVariableWithSlashes_thenStatusNotFound() throws Exception {
|
||||
final String stringWithSlashes = "url/with/slashes";
|
||||
|
||||
mockMvc.perform(get("/slash/mypaths/" + stringWithSlashes))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllFallbackEndpoint_whenUsingPathWithSlashes_thenStatusOk() throws Exception {
|
||||
final String stringWithSlashes = "url/for/testing/purposes";
|
||||
|
||||
MvcResult mvcResult = mockMvc.perform(get("/slash/all/" + stringWithSlashes))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn();
|
||||
|
||||
assertEquals(stringWithSlashes, mvcResult.getResponse()
|
||||
.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllFallbackEndpoint_whenUsingConsecutiveSlashes_thenPathNormalized() throws Exception {
|
||||
final String stringWithSlashes = "http://myurl.com";
|
||||
|
||||
MvcResult mvcResult = mockMvc.perform(get("/slash/all/" + stringWithSlashes))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn();
|
||||
|
||||
String stringWithSlashesNormalized = URI.create("/slash/all/" + stringWithSlashes)
|
||||
.normalize()
|
||||
.toString()
|
||||
.split("/slash/all/")[1];
|
||||
|
||||
assertEquals(stringWithSlashesNormalized, mvcResult.getResponse()
|
||||
.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingSlashesInQueryParam_thenParameterAccepted() throws Exception {
|
||||
final String stringWithSlashes = "url/for////testing/purposes";
|
||||
|
||||
MvcResult mvcResult = mockMvc.perform(get("/slash/all").param("param", stringWithSlashes))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn();
|
||||
|
||||
assertEquals(stringWithSlashes, mvcResult.getResponse()
|
||||
.getContentAsString());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user