spring-projects / spring-projects/spring-session
spring-session-jdbc ignores specified MockMvc request session
@marcusdacoregio is already working on this.
Since Dec 1, 2022.
- Dominant language
- Java
- Stars
- 1.9k
- Forks
- 1.2k
- Avg merge
- 4h 27m
- Merged PRs (30d)
- 55
Description
Describe the bug
When using spring-session-jdbc, MockMvc's MockHttpServletRequestBuilder#session is ignored. It seems as though once SessionRepositoryFilter is added to the filter chain, it ignores any session configured by mockMvc
To Reproduce
Download the github repo demoing the problem and run ./gradlew test. To fix the test, add the MapSessionRepository bean to the context.
Expected behavior
From the demo repo, I would have expected the session that was provided through mockMvc would be the session that was received by the controller.
Sample
Github repo and, for posterity, inlining the relevant files.
build.gradle
plugins {
id 'org.springframework.boot' version '2.6.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.session:spring-session-core'
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.session:spring-session-jdbc'
implementation "com.h2database:h2"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
DemoApplication.java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RestController
public static class MyController {
@GetMapping("/")
public String foo(HttpSession session) {
return (String) session.getAttribute("foo");
}
}
}
DemoApplicationTests.java
@SpringBootTest
@AutoConfigureMockMvc
class DemoApplicationTests {
@Autowired
MockMvc mockMvc;
@TestConfiguration
public static class FooConfig {
// Uncomment to make the test pass.
// @Bean
// public MapSessionRepository mapSessionRepository() {
// return new MapSessionRepository(new ConcurrentHashMap<>());
// }
}
@Test
void contextLoads() throws Exception {
MockHttpSession session = new MockHttpSession();
session.setAttribute("foo", "bar");
mockMvc.perform(MockMvcRequestBuilders.get("/").session(session))
.andExpect(status().isOk())
.andExpect(content().string("bar"));
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.