Code Monkey home page Code Monkey logo

Comments (7)

rstoyanchev avatar rstoyanchev commented on June 16, 2024 1

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

from spring-mvc-showcase.

OrangeDog avatar OrangeDog commented on June 16, 2024 1

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.

from spring-mvc-showcase.

rstoyanchev avatar rstoyanchev commented on June 16, 2024

Something like this?

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

from spring-mvc-showcase.

dsyer avatar dsyer commented on June 16, 2024

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.

from spring-mvc-showcase.

rstoyanchev avatar rstoyanchev commented on June 16, 2024

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.

from spring-mvc-showcase.

dsyer avatar dsyer commented on June 16, 2024

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(...);
    }

from spring-mvc-showcase.

dsyer avatar dsyer commented on June 16, 2024

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.

from spring-mvc-showcase.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.