Skip to content

spring-session-jdbc ignores specified MockMvc request session #2037

Open
@vrish88

Description

@vrish88

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"));
	}
}

Metadata

Metadata

Labels

in: docsAn issue in Documentation or samplesstatus: ideal-for-contributionAn issue that we actively are looking for someone to help us withtype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions