Search in sources :

Example 61 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class CrossOriginTests method setup.

@Before
public void setup() {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    Properties props = new Properties();
    props.setProperty("myOrigin", "http://example.com");
    wac.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("ps", props));
    wac.registerSingleton("ppc", PropertySourcesPlaceholderConfigurer.class);
    wac.refresh();
    this.handlerMapping.setRemoveSemicolonContent(false);
    wac.getAutowireCapableBeanFactory().initializeBean(this.handlerMapping, "hm");
    this.request.setMethod("GET");
    this.request.addHeader(HttpHeaders.ORIGIN, "http://domain.com/");
}
Also used : PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Properties(java.util.Properties) Before(org.junit.Before)

Example 62 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-boot by spring-projects.

the class ResourceServerPropertiesTests method setListableBeanFactory.

private void setListableBeanFactory() {
    ListableBeanFactory beanFactory = new StaticWebApplicationContext() {

        @Override
        public String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
            if (type.isAssignableFrom(ResourceServerTokenServicesConfiguration.class)) {
                return new String[] { "ResourceServerTokenServicesConfiguration" };
            }
            return new String[0];
        }
    };
    this.properties.setBeanFactory(beanFactory);
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory)

Example 63 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-boot by spring-projects.

the class ApplicationContextRequestMatcherTests method initializeAndMatchesAreNotCalledIfContextIsIgnored.

@Test
void initializeAndMatchesAreNotCalledIfContextIsIgnored() {
    StaticWebApplicationContext context = createWebApplicationContext();
    TestApplicationContextRequestMatcher<ApplicationContext> matcher = new TestApplicationContextRequestMatcher<ApplicationContext>(ApplicationContext.class) {

        @Override
        protected boolean ignoreApplicationContext(WebApplicationContext webApplicationContext) {
            return true;
        }

        @Override
        protected void initialized(Supplier<ApplicationContext> context) {
            throw new IllegalStateException();
        }

        @Override
        protected boolean matches(HttpServletRequest request, Supplier<ApplicationContext> context) {
            throw new IllegalStateException();
        }
    };
    MockHttpServletRequest request = new MockHttpServletRequest(context.getServletContext());
    assertThat(matcher.matches(request)).isFalse();
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) WebApplicationContext(org.springframework.web.context.WebApplicationContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Supplier(java.util.function.Supplier) WebApplicationContext(org.springframework.web.context.WebApplicationContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 64 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-boot by spring-projects.

the class ApplicationContextRequestMatcherTests method createWebApplicationContext.

private StaticWebApplicationContext createWebApplicationContext() {
    StaticWebApplicationContext context = new StaticWebApplicationContext();
    MockServletContext servletContext = new MockServletContext();
    context.setServletContext(servletContext);
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
    return context;
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext)

Example 65 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-boot by spring-projects.

the class ApplicationContextRequestMatcherTests method matchesWhenContextClassIsExistingBeanShouldProvideBean.

@Test
void matchesWhenContextClassIsExistingBeanShouldProvideBean() {
    StaticWebApplicationContext context = createWebApplicationContext();
    context.registerSingleton("existingBean", ExistingBean.class);
    assertThat(new TestApplicationContextRequestMatcher<>(ExistingBean.class).callMatchesAndReturnProvidedContext(context).get()).isEqualTo(context.getBean(ExistingBean.class));
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)101 Test (org.junit.jupiter.api.Test)54 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)21 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)20 MockServletContext (org.springframework.mock.web.test.MockServletContext)16 Test (org.junit.Test)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14 ServletContext (jakarta.servlet.ServletContext)11 View (org.springframework.web.servlet.View)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)10 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 Before (org.junit.Before)8 WebApplicationContext (org.springframework.web.context.WebApplicationContext)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)6 Properties (java.util.Properties)5