Skip to content
This repository has been archived by the owner on Feb 5, 2022. It is now read-only.

Add test for "forward:..." #42

Open
dsyer opened this issue Aug 25, 2015 · 7 comments
Open

Add test for "forward:..." #42

dsyer opened this issue Aug 25, 2015 · 7 comments

Comments

@dsyer
Copy link

dsyer commented Aug 25, 2015

There is a test for a controller that uses "redirect:..." views but none for "forward:..." (and it seems to be non-obvious how to get the mock request dispatcher to dispatch back to the main servlet).

@rstoyanchev
Copy link
Contributor

Something like this?

        this.mockMvc.perform(get("/"))
            .andExpect(status().isOk())
            .andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp"));

@dsyer
Copy link
Author

dsyer commented Aug 25, 2015

That would be good. I also want to test the actual output from the forwarded resource (it's an API and the rest-docs should contain the proper response). Is there a way to do that? I think you showed me how to do once but I must have lost the knack.

@rstoyanchev
Copy link
Contributor

At the moment we only have an explicit option for async dispatches. Should probably add one for FORWARD and ERROR dispatches. Take a look at how MockMvcRequestBuilders#asyncDispatch is done.

@dsyer
Copy link
Author

dsyer commented Aug 26, 2015

Something like this?

    public static RequestBuilder forward(MvcResult result) {
        return new RequestBuilder() {

            @Override
            public MockHttpServletRequest buildRequest(
                    ServletContext servletContext) {
                MockHttpServletRequest request = result.getRequest();
                request.setRequestURI(result.getResponse().getForwardedUrl());
                return request;

            }

        };
    }

and then

    @Test
    public void getLocations() throws Exception {
        final MvcResult result = this.mockMvc
                .perform(MockMvcRequestBuilders.get("/")).andReturn();
        this.mockMvc.perform(forward(result))
            .andExpect(...);
    }

It seems clunky to me, and I'm not sure I care too much about shaving this yak. Would it not be possible to build this into the perform() method? E.g. as a global option:

    @Test
    public void getLocations() throws Exception {
        this.mockMvc.followForwards(true);
        this.mockMvc
                .perform(MockMvcRequestBuilders.get("/")).andExpect(...);
    }

@dsyer
Copy link
Author

dsyer commented Aug 26, 2015

Also, although the test passes, it doesn't work with restdocs (no snippets created). Cc @wilkinsona.

UPDATE: cancel that, it works fine with restdocs. It's just the yak shaving that I would like to fix.

@rstoyanchev
Copy link
Contributor

Yes, we probably could do some such option. It wouldn't work well with JSP but apart from that yes.

@OrangeDog
Copy link

MockMvc is supposed to be immutable, so that API wouldn't really work.
Perhaps .andPerformForward() returning a new ResultActions, if that's possible.

mockMvc.perform(get("/one"))
        .andExpect(status().isOk())
        .andExpect(forwardedUrl("/two"))
        .andPerformForward()
        .andExpect(content().string(...))

Or on the original builder somehow

mockMvc.perform(get("/one").with(followForwards()))

I see a ForwardRequestPostProcessor but that needs to know the forwarded URL on construction.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants