Skip to content

Commit

Permalink
Modify more tests for static resource location filtering
Browse files Browse the repository at this point in the history
Fix `WebFluxAutoConfigurationTests` following upstream Spring Framework
changes. Also refine `WebMvcAutoConfigurationTests` to check the locations
are set even if they are filtered.

See gh-28223
  • Loading branch information
philwebb committed Oct 14, 2021
1 parent 3d21ac7 commit 9abf9e6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,7 +50,6 @@
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.format.Parser;
import org.springframework.format.Printer;
import org.springframework.format.support.FormattingConversionService;
Expand Down Expand Up @@ -151,12 +150,13 @@ void shouldRegisterResourceHandlerMapping() {
SimpleUrlHandlerMapping hm = context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
assertThat(hm.getUrlMap().get("/**")).isInstanceOf(ResourceWebHandler.class);
ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/**");
assertThat(staticHandler.getLocations()).hasSize(4);
assertThat(staticHandler).extracting("locationValues").asList().hasSize(4);
assertThat(staticHandler.getLocations()).hasSize(1);
assertThat(staticHandler.getLocations().get(0)).hasToString("class path resource [public/]");
assertThat(hm.getUrlMap().get("/webjars/**")).isInstanceOf(ResourceWebHandler.class);
ResourceWebHandler webjarsHandler = (ResourceWebHandler) hm.getUrlMap().get("/webjars/**");
assertThat(webjarsHandler.getLocations()).hasSize(1);
assertThat(webjarsHandler.getLocations().get(0))
.isEqualTo(new ClassPathResource("/META-INF/resources/webjars/"));
assertThat(webjarsHandler).extracting("locationValues").asList()
.containsExactly("classpath:/META-INF/resources/webjars/");
});
}

Expand All @@ -166,7 +166,7 @@ void shouldMapResourcesToCustomPath() {
SimpleUrlHandlerMapping hm = context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
assertThat(hm.getUrlMap().get("/static/**")).isInstanceOf(ResourceWebHandler.class);
ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/static/**");
assertThat(staticHandler.getLocations()).hasSize(4);
assertThat(staticHandler).extracting("locationValues").asList().hasSize(4);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -177,8 +178,10 @@ void handlerMappingsCreated() {
void resourceHandlerMapping() {
this.contextRunner.run((context) -> {
Map<String, List<Resource>> locations = getResourceMappingLocations(context);
assertThat(locations.get("/**")).hasSize(2);
assertThat(locations.get("/webjars/**")).hasSize(0);
assertThat(locations.get("/**")).hasSize(5);
assertThat(locations.get("/webjars/**")).hasSize(1);
assertThat(locations.get("/webjars/**").get(0))
.isEqualTo(new ClassPathResource("/META-INF/resources/webjars/"));
assertThat(getResourceResolvers(context, "/webjars/**")).hasSize(1);
assertThat(getResourceTransformers(context, "/webjars/**")).hasSize(0);
assertThat(getResourceResolvers(context, "/**")).hasSize(1);
Expand All @@ -190,17 +193,17 @@ void resourceHandlerMapping() {
void customResourceHandlerMapping() {
this.contextRunner.withPropertyValues("spring.mvc.static-path-pattern:/static/**").run((context) -> {
Map<String, List<Resource>> locations = getResourceMappingLocations(context);
assertThat(locations.get("/static/**")).hasSize(2);
assertThat(locations.get("/static/**")).hasSize(5);
assertThat(getResourceResolvers(context, "/static/**")).hasSize(1);
});
}

@Test
void resourceHandlerMappingOverrideWebjars() {
this.contextRunner.withUserConfiguration(WebJarsResources.class).run((context) -> {
this.contextRunner.withUserConfiguration(WebJars.class).run((context) -> {
Map<String, List<Resource>> locations = getResourceMappingLocations(context);
assertThat(locations.get("/webjars/**")).hasSize(1);
assertThat(locations.get("/webjars/**").get(0).getFilename()).isEqualTo("test");
assertThat(locations.get("/webjars/**").get(0)).isEqualTo(new ClassPathResource("/foo/"));
});
}

Expand All @@ -209,7 +212,7 @@ void resourceHandlerMappingOverrideAll() {
this.contextRunner.withUserConfiguration(AllResources.class).run((context) -> {
Map<String, List<Resource>> locations = getResourceMappingLocations(context);
assertThat(locations.get("/**")).hasSize(1);
assertThat(locations.get("/**").get(0).getFilename()).isEqualTo("test");
assertThat(locations.get("/**").get(0)).isEqualTo(new ClassPathResource("/foo/"));
});
}

Expand Down Expand Up @@ -1040,7 +1043,7 @@ private void assertResourceHttpRequestHandler(AssertableWebApplicationContext co
protected Map<String, List<Resource>> getResourceMappingLocations(ApplicationContext context) {
Object bean = context.getBean("resourceHandlerMapping");
if (bean instanceof HandlerMapping) {
return getMappingLocations((HandlerMapping) bean);
return getMappingLocations(context, (HandlerMapping) bean);
}
assertThat(bean.toString()).isEqualTo("null");
return Collections.emptyMap();
Expand All @@ -1059,11 +1062,18 @@ protected List<ResourceTransformer> getResourceTransformers(ApplicationContext c
}

@SuppressWarnings("unchecked")
protected Map<String, List<Resource>> getMappingLocations(HandlerMapping mapping) {
private Map<String, List<Resource>> getMappingLocations(ApplicationContext context, HandlerMapping mapping) {
Map<String, List<Resource>> mappingLocations = new LinkedHashMap<>();
getHandlerMap(mapping).forEach((key, value) -> {
Object locations = ReflectionTestUtils.getField(value, "locationsToUse");
mappingLocations.put(key, (List<Resource>) locations);
List<String> locationValues = (List<String>) ReflectionTestUtils.getField(value, "locationValues");
List<Resource> locationResources = (List<Resource>) ReflectionTestUtils.getField(value,
"locationResources");
List<Resource> resources = new ArrayList<>();
for (String locationValue : locationValues) {
resources.add(context.getResource(locationValue));
}
resources.addAll(locationResources);
mappingLocations.put(key, resources);
});
return mappingLocations;
}
Expand Down Expand Up @@ -1094,11 +1104,11 @@ protected void renderMergedOutputModel(Map<String, Object> model, HttpServletReq
}

@Configuration(proxyBeanMethods = false)
static class WebJarsResources implements WebMvcConfigurer {
static class WebJars implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**").addResourceLocations(new ClassPathResource("/test", getClass()));
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/foo/");
}

}
Expand All @@ -1108,7 +1118,7 @@ static class AllResources implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(new ClassPathResource("/test", getClass()));
registry.addResourceHandler("/**").addResourceLocations("classpath:/foo/");
}

}
Expand Down

0 comments on commit 9abf9e6

Please sign in to comment.