spring-projects / spring-projects/spring-session

spring-session-jdbc ignores specified MockMvc request session

Open
#2,037 3 comments 2 reactions 1 assignee View on GitHub

@marcusdacoregio is already working on this.

Since Dec 1, 2022.

in: docs status: ideal-for-contribution type: enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.