@Test public void contextPath() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "management.contextPath:/test"); this.applicationContext.register(RootConfig.class, EndpointConfig.class, ServerPortConfig.class, PropertyPlaceholderAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, JacksonAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/test/endpoint", ports.get().server, "endpointoutput"); }
@Test public void overrideServerProperties() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "server.displayName:foo"); this.applicationContext.register(RootConfig.class, EndpointConfig.class, ServerPortConfig.class, PropertyPlaceholderAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, JacksonAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); ServerProperties serverProperties = this.applicationContext .getBean(ServerProperties.class); assertThat(serverProperties.getDisplayName()).isEqualTo("foo"); }
@Test public void testWebConfiguration() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(SecurityAutoConfiguration.class, WebMvcAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class, JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "security.basic.enabled:false"); this.context.refresh(); assertThat(this.context.getBean(AuthenticationManagerBuilder.class)).isNotNull(); FilterChainProxy filterChainProxy = this.context.getBean(FilterChainProxy.class); // 1 for static resources, one for management endpoints and one for the rest assertThat(filterChainProxy.getFilterChains()).hasSize(3); assertThat(filterChainProxy.getFilters("/beans")).isNotEmpty(); assertThat(filterChainProxy.getFilters("/beans/")).isNotEmpty(); assertThat(filterChainProxy.getFilters("/beans.foo")).isNotEmpty(); assertThat(filterChainProxy.getFilters("/beans/foo/bar")).isNotEmpty(); }
@Test public void contextPath() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "management.contextPath:/test"); this.applicationContext.register(RootConfig.class, EndpointConfig.class, ServerPortConfig.class, PropertyPlaceholderAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, JacksonAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/test/endpoint", ports.get().server, "endpointoutput"); this.applicationContext.close(); assertAllClosed(); }
@Test public void testWebConfiguration() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(SecurityAutoConfiguration.class, WebMvcAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class, JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "security.basic.enabled:false"); this.context.refresh(); assertNotNull(this.context.getBean(AuthenticationManagerBuilder.class)); FilterChainProxy filterChainProxy = this.context.getBean(FilterChainProxy.class); // 4 for static resources, one for management endpoints and one for the rest assertThat(filterChainProxy.getFilterChains(), hasSize(6)); assertThat(filterChainProxy.getFilters("/beans"), hasSize(greaterThan(0))); assertThat(filterChainProxy.getFilters("/beans/"), hasSize(greaterThan(0))); assertThat(filterChainProxy.getFilters("/beans.foo"), hasSize(greaterThan(0))); assertThat(filterChainProxy.getFilters("/beans/foo/bar"), hasSize(greaterThan(0))); }
@Before public void setup() { this.context = new AnnotationConfigApplicationContext(); this.context.register(JacksonAutoConfiguration.class, this.configClass); this.context.getEnvironment().getSystemProperties().putAll(systemProperties); // this.context.refresh(); }
@Test public void realmSameForManagement() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(AuthenticationConfig.class, SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class, JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class, WebMvcAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); Filter filter = this.context.getBean("springSecurityFilterChain", Filter.class); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .addFilters(filter).build(); // no user (Main) mockMvc.perform(MockMvcRequestBuilders.get("/home")) .andExpect(MockMvcResultMatchers.status().isUnauthorized()) .andExpect(springAuthenticateRealmHeader()); // invalid user (Main) mockMvc.perform( MockMvcRequestBuilders.get("/home").header("authorization", "Basic xxx")) .andExpect(MockMvcResultMatchers.status().isUnauthorized()) .andExpect(springAuthenticateRealmHeader()); // no user (Management) mockMvc.perform(MockMvcRequestBuilders.get("/beans")) .andExpect(MockMvcResultMatchers.status().isUnauthorized()) .andExpect(springAuthenticateRealmHeader()); // invalid user (Management) mockMvc.perform( MockMvcRequestBuilders.get("/beans").header("authorization", "Basic xxx")) .andExpect(MockMvcResultMatchers.status().isUnauthorized()) .andExpect(springAuthenticateRealmHeader()); }
@Before public void createContext() { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, JolokiaAutoConfiguration.class, WebMvcAutoConfiguration.class); }
@Test public void jacksonIsPreferredByDefaultWhenBothGsonAndJacksonAreAvailable() { this.context.register(GsonAutoConfiguration.class, JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class); this.context.refresh(); assertConverterBeanExists(MappingJackson2HttpMessageConverter.class, "mappingJackson2HttpMessageConverter"); assertConverterBeanRegisteredWithHttpMessageConverters( MappingJackson2HttpMessageConverter.class); assertThat(this.context.getBeansOfType(GsonHttpMessageConverter.class)).isEmpty(); }
@Test public void gsonCanBePreferredWhenBothGsonAndJacksonAreAvailable() { this.context.register(GsonAutoConfiguration.class, JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "spring.http.converters.preferred-json-mapper:gson"); this.context.refresh(); assertConverterBeanExists(GsonHttpMessageConverter.class, "gsonHttpMessageConverter"); assertConverterBeanRegisteredWithHttpMessageConverters( GsonHttpMessageConverter.class); assertThat(this.context.getBeansOfType(MappingJackson2HttpMessageConverter.class)) .isEmpty(); }
@Test public void jacksonIsPreferredByDefaultWhenBothGsonAndJacksonAreAvailable() { this.context.register(GsonAutoConfiguration.class, JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class); this.context.refresh(); assertConverterBeanExists(MappingJackson2HttpMessageConverter.class, "mappingJackson2HttpMessageConverter"); assertConverterBeanRegisteredWithHttpMessageConverters( MappingJackson2HttpMessageConverter.class); assertEquals(0, this.context.getBeansOfType(GsonHttpMessageConverter.class).size()); }
@Test public void gsonCanBePreferredWhenBothGsonAndJacksonAreAvailable() { this.context.register(GsonAutoConfiguration.class, JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "spring.http.converters.preferred-json-mapper:gson"); this.context.refresh(); assertConverterBeanExists(GsonHttpMessageConverter.class, "gsonHttpMessageConverter"); assertConverterBeanRegisteredWithHttpMessageConverters( GsonHttpMessageConverter.class); assertEquals(0, this.context .getBeansOfType(MappingJackson2HttpMessageConverter.class).size()); }
@Before public void setup() { this.context = new AnnotationConfigApplicationContext(); this.context.register(JacksonAutoConfiguration.class, FeaturesConfig.class, Config.class); this.context.refresh(); }
private void load(Class<?> config, String... environment) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(ctx, environment); if (config != null) { ctx.register(config); } ctx.register(WebClientAutoConfiguration.class, JacksonAutoConfiguration.class, InitializrAutoConfiguration.class); ctx.refresh(); this.context = ctx; }
@Before public void setup() { this.context = new AnnotationConfigApplicationContext(); this.context.register(JacksonAutoConfiguration.class, this.configClass); this.context.refresh(); }