Skip to content

Commit 957ad45

Browse files
marcos-lgKevinGilmore
authored andcommitted
BAEL-3085 Using a slash character in Spring URLs (eugenp#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
1 parent cc23ac5 commit 957ad45

4 files changed

Lines changed: 137 additions & 0 deletions

File tree

spring-mvc-simple-2/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<groupId>org.springframework.boot</groupId>
1919
<artifactId>spring-boot-starter-web</artifactId>
2020
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-test</artifactId>
24+
<scope>test</scope>
25+
</dependency>
2126
</dependencies>
2227

2328
<properties>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.spring.slash;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
6+
7+
@SpringBootApplication
8+
public class Application implements WebMvcConfigurer {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(Application.class, args);
12+
}
13+
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.spring.slash;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("slash")
13+
public class SlashParsingController {
14+
15+
@GetMapping("mypaths/{anything}")
16+
public String pathVariable(@PathVariable("anything") String anything) {
17+
return anything;
18+
}
19+
20+
@GetMapping("all/**")
21+
public String allDirectories(HttpServletRequest request) {
22+
return request.getRequestURI()
23+
.split(request.getContextPath() + "/all/")[1];
24+
}
25+
26+
@GetMapping("all")
27+
public String queryParameter(@RequestParam("param") String param) {
28+
return param;
29+
}
30+
31+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.baeldung.spring.slash;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6+
7+
import java.net.URI;
8+
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.test.context.junit.jupiter.SpringExtension;
15+
import org.springframework.test.web.servlet.MockMvc;
16+
import org.springframework.test.web.servlet.MvcResult;
17+
18+
@AutoConfigureMockMvc
19+
@ExtendWith(SpringExtension.class)
20+
@SpringBootTest
21+
public class SlashParsingControllerIntTest {
22+
23+
@Autowired
24+
private MockMvc mockMvc;
25+
26+
@Test
27+
public void whenUsingPathVariablemWithoutSlashes_thenStatusOk() throws Exception {
28+
final String stringWithoutSlashes = "noslash";
29+
30+
MvcResult mvcResult = mockMvc.perform(get("/slash/mypaths/" + stringWithoutSlashes))
31+
.andExpect(status().isOk())
32+
.andReturn();
33+
34+
assertEquals(stringWithoutSlashes, mvcResult.getResponse()
35+
.getContentAsString());
36+
}
37+
38+
@Test
39+
public void whenUsingPathVariableWithSlashes_thenStatusNotFound() throws Exception {
40+
final String stringWithSlashes = "url/with/slashes";
41+
42+
mockMvc.perform(get("/slash/mypaths/" + stringWithSlashes))
43+
.andExpect(status().isNotFound());
44+
}
45+
46+
@Test
47+
public void givenAllFallbackEndpoint_whenUsingPathWithSlashes_thenStatusOk() throws Exception {
48+
final String stringWithSlashes = "url/for/testing/purposes";
49+
50+
MvcResult mvcResult = mockMvc.perform(get("/slash/all/" + stringWithSlashes))
51+
.andExpect(status().isOk())
52+
.andReturn();
53+
54+
assertEquals(stringWithSlashes, mvcResult.getResponse()
55+
.getContentAsString());
56+
}
57+
58+
@Test
59+
public void givenAllFallbackEndpoint_whenUsingConsecutiveSlashes_thenPathNormalized() throws Exception {
60+
final String stringWithSlashes = "http://myurl.com";
61+
62+
MvcResult mvcResult = mockMvc.perform(get("/slash/all/" + stringWithSlashes))
63+
.andExpect(status().isOk())
64+
.andReturn();
65+
66+
String stringWithSlashesNormalized = URI.create("/slash/all/" + stringWithSlashes)
67+
.normalize()
68+
.toString()
69+
.split("/slash/all/")[1];
70+
71+
assertEquals(stringWithSlashesNormalized, mvcResult.getResponse()
72+
.getContentAsString());
73+
}
74+
75+
@Test
76+
public void whenUsingSlashesInQueryParam_thenParameterAccepted() throws Exception {
77+
final String stringWithSlashes = "url/for////testing/purposes";
78+
79+
MvcResult mvcResult = mockMvc.perform(get("/slash/all").param("param", stringWithSlashes))
80+
.andExpect(status().isOk())
81+
.andReturn();
82+
83+
assertEquals(stringWithSlashes, mvcResult.getResponse()
84+
.getContentAsString());
85+
}
86+
87+
}

0 commit comments

Comments
 (0)