Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate RequestAttributes before invoking FilterChain in MockMvc [SPR-13217] #17808

Closed
spring-issuemaster opened this issue Jul 10, 2015 · 1 comment
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Jul 10, 2015

Sam Brannen opened SPR-13217 and commented

Status Quo

The combination of loading a WebApplicationContext with the Spring TestContext Framework (TCF) and using the Spring MVC Test Framework results in two instances of MockHttpServletRequest being created. See #17803 for additional information.

The fact that two mocked requests are created can lead to a subtle bug if a Servlet Filter (or a Spring-managed component invoked by a filter) attempts to access the RequestAttributes before the TestDispatcherServlet is invoked by MockMvc. Specifically, the filter (or its collaborator) will work with the mocked request created by the ServletTestExecutionListener; whereas, the controller that is invoked by MockMvc will work with the mocked request managed by MockMvc. Consequently, if the controller expects the filter to store information in the request (e.g., request attributes), the test will fail.

Analysis

In production deployments the aforementioned bug will not occur if the RequestContextFilter or RequestContextListener is configured for the web application. When using MockMvc with the TCF, however, it is necessary to manually add a RequestContextFilter to MockMvc before any such custom filters -- for example:

this.mockMvc = MockMvcBuilders
  .webAppContextSetup(this.wac)
  .addFilters(new RequestContextFilter(), myFilter)
  .build();

Deliverables

  1. Ensure that RequestAttributes are populated in the RequestContextHolder using the MockHttpServletRequest managed by MockMvc before any filters are invoked by MockMvc.

Affects: 3.2 GA

Issue Links:

  • #17803 Reuse MockHttpServletRequest from ServletTestExecutionListener in Spring MVC Test framework
@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jul 10, 2015

Sam Brannen commented

Fixed in GitHub commit 9c46228:

Populate RequestAttributes before invoking Filters in MockMvc

When using the Spring TestContext Framework (TCF) to load a WebApplicationContext and the Spring MVC Test Framework (MockMvc) to test a controller, two instances of MockHttpServletRequest will be created. Due to an ordering issue with regard to population of the RequestAttributes, it is therefore possible that a filter accesses the mocked request managed by the TCF, while the controller accesses the mocked request managed by MockMvc, and this leads to test failures if the controller expects data from the filter to be present in the request.

This commit fixes this bug by ensuring that the RequestAttributes backed by the mocked request managed by MockMvc are stored in the RequestContextHolder before any filters are invoked by MockMvc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.