Search in sources :

Example 1 with MockRequestDispatcher

use of org.springframework.web.testfixture.servlet.MockRequestDispatcher in project spring-framework by spring-projects.

the class ViewResolverTests method internalResourceViewResolverWithContextBeans.

@Test
public void internalResourceViewResolverWithContextBeans() throws Exception {
    this.wac.registerSingleton("myBean", TestBean.class);
    this.wac.registerSingleton("myBean2", TestBean.class);
    this.wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    Properties props = new Properties();
    props.setProperty("key1", "value1");
    vr.setAttributes(props);
    Map<String, Object> map = new HashMap<>();
    map.put("key2", 2);
    vr.setAttributesMap(map);
    vr.setExposeContextBeansAsAttributes(true);
    vr.setApplicationContext(this.wac);
    HttpServletRequest request = new MockHttpServletRequest(this.sc) {

        @Override
        public RequestDispatcher getRequestDispatcher(String path) {
            return new MockRequestDispatcher(path) {

                @Override
                public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
                    assertThat(forwardRequest.getAttribute("rc") == null).as("Correct rc attribute").isTrue();
                    assertThat(forwardRequest.getAttribute("key1")).isEqualTo("value1");
                    assertThat(forwardRequest.getAttribute("key2")).isEqualTo(2);
                    assertThat(forwardRequest.getAttribute("myBean")).isSameAs(wac.getBean("myBean"));
                    assertThat(forwardRequest.getAttribute("myBean2")).isSameAs(wac.getBean("myBean2"));
                }
            };
        }
    };
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    View view = vr.resolveViewName("example1", Locale.getDefault());
    view.render(new HashMap<String, Object>(), request, this.response);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Properties(java.util.Properties) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) View(org.springframework.web.servlet.View) MockRequestDispatcher(org.springframework.web.testfixture.servlet.MockRequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 2 with MockRequestDispatcher

use of org.springframework.web.testfixture.servlet.MockRequestDispatcher in project spring-framework by spring-projects.

the class InternalResourceViewTests method includeOnCommitted.

@Test
public void includeOnCommitted() throws Exception {
    given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
    given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn(null);
    given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));
    response.setCommitted(true);
    view.setUrl(url);
    // Can now try multiple tests
    view.render(model, request, response);
    assertThat(response.getIncludedUrl()).isEqualTo(url);
    model.forEach((k, v) -> verify(request).setAttribute(k, v));
}
Also used : MockRequestDispatcher(org.springframework.web.testfixture.servlet.MockRequestDispatcher) Test(org.junit.jupiter.api.Test)

Example 3 with MockRequestDispatcher

use of org.springframework.web.testfixture.servlet.MockRequestDispatcher in project spring-framework by spring-projects.

the class InternalResourceViewTests method includeOnAttribute.

@Test
public void includeOnAttribute() throws Exception {
    given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
    given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somepath");
    given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));
    view.setUrl(url);
    // Can now try multiple tests
    view.render(model, request, response);
    assertThat(response.getIncludedUrl()).isEqualTo(url);
    model.forEach((key, value) -> verify(request).setAttribute(key, value));
}
Also used : MockRequestDispatcher(org.springframework.web.testfixture.servlet.MockRequestDispatcher) Test(org.junit.jupiter.api.Test)

Example 4 with MockRequestDispatcher

use of org.springframework.web.testfixture.servlet.MockRequestDispatcher in project spring-framework by spring-projects.

the class ViewResolverTests method internalResourceViewResolverWithSpecificContextBeans.

@Test
public void internalResourceViewResolverWithSpecificContextBeans() throws Exception {
    this.wac.registerSingleton("myBean", TestBean.class);
    this.wac.registerSingleton("myBean2", TestBean.class);
    this.wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    Properties props = new Properties();
    props.setProperty("key1", "value1");
    vr.setAttributes(props);
    Map<String, Object> map = new HashMap<>();
    map.put("key2", 2);
    vr.setAttributesMap(map);
    vr.setExposedContextBeanNames(new String[] { "myBean2" });
    vr.setApplicationContext(this.wac);
    HttpServletRequest request = new MockHttpServletRequest(this.sc) {

        @Override
        public RequestDispatcher getRequestDispatcher(String path) {
            return new MockRequestDispatcher(path) {

                @Override
                public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
                    assertThat(forwardRequest.getAttribute("rc") == null).as("Correct rc attribute").isTrue();
                    assertThat(forwardRequest.getAttribute("key1")).isEqualTo("value1");
                    assertThat(forwardRequest.getAttribute("key2")).isEqualTo(2);
                    assertThat(forwardRequest.getAttribute("myBean")).isNull();
                    assertThat(forwardRequest.getAttribute("myBean2")).isSameAs(wac.getBean("myBean2"));
                }
            };
        }
    };
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    View view = vr.resolveViewName("example1", Locale.getDefault());
    view.render(new HashMap<String, Object>(), request, this.response);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Properties(java.util.Properties) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) View(org.springframework.web.servlet.View) MockRequestDispatcher(org.springframework.web.testfixture.servlet.MockRequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 5 with MockRequestDispatcher

use of org.springframework.web.testfixture.servlet.MockRequestDispatcher in project spring-framework by spring-projects.

the class InternalResourceViewTests method alwaysInclude.

@Test
public void alwaysInclude() throws Exception {
    given(request.getAttribute(View.PATH_VARIABLES)).willReturn(null);
    given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));
    view.setUrl(url);
    view.setAlwaysInclude(true);
    // Can now try multiple tests
    view.render(model, request, response);
    assertThat(response.getIncludedUrl()).isEqualTo(url);
    model.forEach((key, value) -> verify(request).setAttribute(key, value));
}
Also used : MockRequestDispatcher(org.springframework.web.testfixture.servlet.MockRequestDispatcher) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)5 MockRequestDispatcher (org.springframework.web.testfixture.servlet.MockRequestDispatcher)5 ServletRequest (jakarta.servlet.ServletRequest)2 ServletResponse (jakarta.servlet.ServletResponse)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 View (org.springframework.web.servlet.View)2 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)2 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)2