Sofia opened SPR-14342 and commented
It would be very convenient to improve MockMvc to follow redirects and forwards. @dyser
showed me how I could use a "forwarder" result handler with DefaultMockMvcBuilder
's alwaysDo
. But as you can see from the screenshot, only the forwarded url is set -- the request object is just disregarded. If I could get a reference to the new request object, I could do the forwarding to the new url and supply the new request object in the forwarder result handler.
Affects: 4.3 GA
Attachments: - mock dispatcher setting forwaded url to mock response.png (528.71 kB)
Issue Links: - #19321 Actually support request forwarding in MockRequestDispatcher
0 votes, 5 watchers
Comment From: spring-projects-issues
Rossen Stoyanchev commented
We can add a "forward" method in MockMvcRequestBuilders along the lines of the existing asyncDispatch method, except extracting the forwardedUrl and setting it on the RequestBuilder. You can see from the Javadoc how that's meant to be used.
It should be easy for you to do something like that in your own code for now. In the mean time or if you'd like to submit a pull request, this one is well suited for a contribution.
Comment From: spring-projects-issues
Rob Winch commented
NOTE We already support following forwards in MockMvcWebConnection so it is possible we could extract this logic out.
As for redirects we rely on HtmlUnit to follow the redirects.
I'm thinking for MockMvc we should be careful about how this is done so that we remain passive. For example if we automatically follow redirects, then users tests will fail because their assertions for a redirect URL will no longer be valid.
Comment From: spring-projects-issues
lqjacklee commented
after read the code , i want to solve the issue . the solution as below , please review :
1 refactor the redirect() to throw new IllegalException("simple throw exception because of the redirect");
2 add the new method redirect(String url) which will create the new MockMVC contained the redirected url and return to the user.
expect your reply ,thanks a lot .
Comment From: spring-projects-issues
Rossen Stoyanchev commented
I don't think an exception is the way to handle this. I haven't thought it through entirely but I think we should start with adding forward(mvcResult)
and redirect(mvcResult)
methods to MockMvcRequestBuilders
along the lines of the already existing asyncDispatch
method. It enables an application to verify the state of things before the forward or redirect and then explicitly initiate the forward or redirect with minimal effort. On top of that an option to enable automatically following redirects and forwards, which probably be implemented somewhere in MockMvc along the lines of what Rob showed above in MockMvcWebConnection.
Comment From: spring-projects-issues
Rossen Stoyanchev commented
Tracing what Tomcat does to support forwarding, e.g. see ApplicationHttpRequest.java, ApplicationContext, and ApplicationDispatcher, it's appears quite non-trivial and extensive.
MockMvc is for running without a Servlet container and emulating forwarding would only makes sense if it was relatively straight forward. So I am inclined to resolve this as "Won't fix" and recommend running full integration tests instead where needed. The only other sane alternative I see is if we could define some very limited but meaningful subset of use cases for which forwarding should work and make no further promises beyond that.
Comment From: spring-projects-issues
Rossen Stoyanchev commented
Resolving as "Won't Fix" since general support for forwarding is well beyond the scope of the MockHttpServletRequest
. Consider testing directly against the forwarded URL and setting up the request as it would be expected at the point of forwarding.
Comment From: theMyth721
I am practicing building my own JSON form login, I understand with redirects, the client has to make a second request. But forwards should work, In my tests I am getting the correct response code, correct status, but the response body is null
Comment From: rstoyanchev
Try MockMvcWebTestClient.