Search in sources :

Example 51 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-framework by spring-projects.

the class MockMultipartHttpServletRequestBuilderTests method test.

@Test
public void test() {
    MockHttpServletRequestBuilder parent = new MockHttpServletRequestBuilder(HttpMethod.GET, "/");
    parent.characterEncoding("UTF-8");
    Object result = new MockMultipartHttpServletRequestBuilder("/fileUpload").merge(parent);
    assertNotNull(result);
    assertEquals(MockMultipartHttpServletRequestBuilder.class, result.getClass());
    MockMultipartHttpServletRequestBuilder builder = (MockMultipartHttpServletRequestBuilder) result;
    MockHttpServletRequest request = builder.buildRequest(new MockServletContext());
    assertEquals("UTF-8", request.getCharacterEncoding());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 52 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class ConditionalOnNotWebApplicationTests method testNotWebApplicationWithServletContext.

@Test
public void testNotWebApplicationWithServletContext() {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(NotWebApplicationConfiguration.class);
    ctx.setServletContext(new MockServletContext());
    ctx.refresh();
    this.context = ctx;
    assertThat(this.context.getBeansOfType(String.class)).isEmpty();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 53 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class SpringDataWebAutoConfigurationTests method webSupportIsAutoConfiguredInWebApplicationContexts.

@Test
public void webSupportIsAutoConfiguredInWebApplicationContexts() {
    this.context = new AnnotationConfigWebApplicationContext();
    ((AnnotationConfigWebApplicationContext) this.context).register(SpringDataWebAutoConfiguration.class);
    this.context.refresh();
    ((AnnotationConfigWebApplicationContext) this.context).setServletContext(new MockServletContext());
    Map<String, PageableHandlerMethodArgumentResolver> beans = this.context.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
    assertThat(beans).hasSize(1);
}
Also used : PageableHandlerMethodArgumentResolver(org.springframework.data.web.PageableHandlerMethodArgumentResolver) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 54 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class HypermediaAutoConfigurationTests method linkDiscoverersCreated.

@Test
public void linkDiscoverersCreated() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(BaseConfig.class);
    this.context.refresh();
    LinkDiscoverers discoverers = this.context.getBean(LinkDiscoverers.class);
    assertThat(discoverers).isNotNull();
    LinkDiscoverer discoverer = discoverers.getLinkDiscovererFor(MediaTypes.HAL_JSON);
    assertThat(HalLinkDiscoverer.class.isInstance(discoverer)).isTrue();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) LinkDiscoverer(org.springframework.hateoas.LinkDiscoverer) HalLinkDiscoverer(org.springframework.hateoas.hal.HalLinkDiscoverer) MockServletContext(org.springframework.mock.web.MockServletContext) LinkDiscoverers(org.springframework.hateoas.LinkDiscoverers) Test(org.junit.Test)

Example 55 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class HypermediaAutoConfigurationTests method customizationOfSupportedMediaTypesCanBeDisabled.

@Test
public void customizationOfSupportedMediaTypesCanBeDisabled() {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(BaseConfig.class);
    EnvironmentTestUtils.addEnvironment(this.context, "spring.hateoas.use-hal-as-default-json-media-type:false");
    this.context.refresh();
    RequestMappingHandlerAdapter handlerAdapter = this.context.getBean(RequestMappingHandlerAdapter.class);
    for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) {
        if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) {
            assertThat(converter.getSupportedMediaTypes()).containsExactly(MediaTypes.HAL_JSON);
        }
    }
}
Also used : TypeConstrainedMappingJackson2HttpMessageConverter(org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.MockServletContext)186 Test (org.junit.Test)135 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)82 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)80 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)74 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)54 MockRequestContext (org.springframework.webflow.test.MockRequestContext)46 Event (org.springframework.webflow.execution.Event)18 Before (org.junit.Before)16 EventFactorySupport (org.springframework.webflow.action.EventFactorySupport)14 MockMvc (org.springframework.test.web.servlet.MockMvc)13 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)12 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)9 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)8 MockFilterChain (org.springframework.mock.web.MockFilterChain)8 MockHttpSession (org.springframework.mock.web.MockHttpSession)8 Map (java.util.Map)7 Credential (org.apereo.cas.authentication.Credential)7 Cookie (javax.servlet.http.Cookie)6 FilterChainProxy (org.springframework.security.web.FilterChainProxy)6