Search in sources :

Example 6 with MockHttpServletRequest

use of cn.taketoday.mock.web.MockHttpServletRequest in project today-framework by TAKETODAY.

the class ClassPathBeanDefinitionScannerScopeIntegrationTests method setup.

@BeforeEach
void setup() {
    MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
    oldRequestWithSession.setSession(new MockHttpSession());
    this.oldRequestAttributesWithSession = new ServletRequestContext(null, oldRequestWithSession, new MockHttpServletResponse());
    MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
    newRequestWithSession.setSession(new MockHttpSession());
    this.newRequestAttributesWithSession = new ServletRequestContext(null, newRequestWithSession, new MockHttpServletResponse());
}
Also used : MockHttpServletRequest(cn.taketoday.mock.web.MockHttpServletRequest) MockHttpSession(cn.taketoday.mock.web.MockHttpSession) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockHttpServletResponse(cn.taketoday.mock.web.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with MockHttpServletRequest

use of cn.taketoday.mock.web.MockHttpServletRequest in project today-framework by TAKETODAY.

the class DelegatingFilterProxyRegistrationBeanTests method initShouldNotCauseEarlyInitialization.

@Test
void initShouldNotCauseEarlyInitialization() throws Exception {
    this.applicationContext.registerBeanDefinition("mockFilter", new RootBeanDefinition(MockFilter.class));
    DelegatingFilterProxyRegistrationBean registrationBean = createFilterRegistrationBean();
    Filter filter = registrationBean.getFilter();
    filter.init(new MockFilterConfig());
    assertThat(mockFilterInitialized.get()).isNull();
    filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
    assertThat(mockFilterInitialized.get()).isTrue();
}
Also used : Filter(jakarta.servlet.Filter) MockHttpServletRequest(cn.taketoday.mock.web.MockHttpServletRequest) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) MockFilterChain(cn.taketoday.mock.web.MockFilterChain) MockHttpServletResponse(cn.taketoday.mock.web.MockHttpServletResponse) MockFilterConfig(cn.taketoday.mock.web.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Example 8 with MockHttpServletRequest

use of cn.taketoday.mock.web.MockHttpServletRequest in project today-framework by TAKETODAY.

the class AopNamespaceHandlerScopeIntegrationTests method testSessionScoping.

@Test
void testSessionScoping() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestContextHolder.set(new ServletRequestContext(null, request, new MockHttpServletResponse()));
    assertThat(AopUtils.isAopProxy(sessionScoped)).as("Should be AOP proxy").isTrue();
    boolean condition1 = sessionScoped instanceof TestBean;
    assertThat(condition1).as("Should not be target class proxy").isFalse();
    assertThat(sessionScopedAlias).isSameAs(sessionScoped);
    assertThat(AopUtils.isAopProxy(testBean)).as("Should be AOP proxy").isTrue();
    boolean condition = testBean instanceof TestBean;
    assertThat(condition).as("Regular bean should be JDK proxy").isFalse();
    String rob = "Rob Harrop";
    String bram = "Bram Smeets";
    assertThat(sessionScoped.getName()).isEqualTo(rob);
    sessionScoped.setName(bram);
    assertThat(((Advised) sessionScoped).getAdvisors().length > 0).as("Should have advisors").isTrue();
}
Also used : TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MockHttpServletRequest(cn.taketoday.mock.web.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockHttpServletResponse(cn.taketoday.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 9 with MockHttpServletRequest

use of cn.taketoday.mock.web.MockHttpServletRequest in project today-framework by TAKETODAY.

the class ServletWebServerApplicationContextTests method delegatingFilterProxyRegistrationBeansSkipsTargetBeanNames.

@Test
void delegatingFilterProxyRegistrationBeansSkipsTargetBeanNames() {
    addWebServerFactoryBean();
    DelegatingFilterProxyRegistrationBean initializer = new DelegatingFilterProxyRegistrationBean("filterBean");
    this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer));
    BeanDefinition filterBeanDefinition = beanDefinition(new IllegalStateException("Create FilterBean Failure"));
    filterBeanDefinition.setLazyInit(true);
    this.context.registerBeanDefinition("filterBean", filterBeanDefinition);
    this.context.refresh();
    ServletContext servletContext = getWebServerFactory().getServletContext();
    then(servletContext).should(atMost(1)).addFilter(anyString(), this.filterCaptor.capture());
    // Up to this point the filterBean should not have been created, calling
    // the delegate proxy will trigger creation and an exception
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> {
        this.filterCaptor.getValue().init(new MockFilterConfig());
        this.filterCaptor.getValue().doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
    }).havingRootCause().isInstanceOf(IllegalStateException.class).withMessageContaining("Create FilterBean Failure");
}
Also used : DelegatingFilterProxyRegistrationBean(cn.taketoday.framework.web.servlet.DelegatingFilterProxyRegistrationBean) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) MockHttpServletRequest(cn.taketoday.mock.web.MockHttpServletRequest) ServletContext(jakarta.servlet.ServletContext) AbstractBeanDefinition(cn.taketoday.beans.factory.support.AbstractBeanDefinition) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) MockFilterChain(cn.taketoday.mock.web.MockFilterChain) MockHttpServletResponse(cn.taketoday.mock.web.MockHttpServletResponse) MockFilterConfig(cn.taketoday.mock.web.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Example 10 with MockHttpServletRequest

use of cn.taketoday.mock.web.MockHttpServletRequest in project today-framework by TAKETODAY.

the class MockFilterChainTests method setup.

@BeforeEach
void setup() {
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
}
Also used : MockHttpServletRequest(cn.taketoday.mock.web.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.mock.web.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

MockHttpServletRequest (cn.taketoday.mock.web.MockHttpServletRequest)16 MockHttpServletResponse (cn.taketoday.mock.web.MockHttpServletResponse)12 Test (org.junit.jupiter.api.Test)10 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)8 BeforeEach (org.junit.jupiter.api.BeforeEach)6 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)4 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)4 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)4 MockFilterChain (cn.taketoday.mock.web.MockFilterChain)4 MockFilterConfig (cn.taketoday.mock.web.MockFilterConfig)4 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)2 AbstractBeanDefinition (cn.taketoday.beans.factory.support.AbstractBeanDefinition)2 DelegatingFilterProxyRegistrationBean (cn.taketoday.framework.web.servlet.DelegatingFilterProxyRegistrationBean)2 MockHttpSession (cn.taketoday.mock.web.MockHttpSession)2 RequestContext (cn.taketoday.web.RequestContext)2 Filter (jakarta.servlet.Filter)2 ServletContext (jakarta.servlet.ServletContext)2 Locale (java.util.Locale)2 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)2