Ensure default mime mappings are applied
Fixes gh-40860
This commit is contained in:
@@ -120,7 +120,7 @@ public class ServerProperties {
|
||||
/**
|
||||
* Custom MIME mappings in addition to the default MIME mappings.
|
||||
*/
|
||||
private final MimeMappings mimeMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT);
|
||||
private final MimeMappings mimeMappings = new MimeMappings();
|
||||
|
||||
@NestedConfigurationProperty
|
||||
private final Http2 http2 = new Http2();
|
||||
|
||||
@@ -95,7 +95,7 @@ public class ServletWebServerFactoryCustomizer
|
||||
map.from(() -> this.cookieSameSiteSuppliers)
|
||||
.whenNot(CollectionUtils::isEmpty)
|
||||
.to(factory::setCookieSameSiteSuppliers);
|
||||
map.from(this.serverProperties::getMimeMappings).to(factory::setMimeMappings);
|
||||
map.from(this.serverProperties::getMimeMappings).to(factory::addMimeMappings);
|
||||
this.webListenerRegistrars.forEach((registrar) -> registrar.register(factory));
|
||||
}
|
||||
|
||||
|
||||
@@ -187,13 +187,12 @@ class ServerPropertiesTests {
|
||||
|
||||
@Test
|
||||
void testDefaultMimeMapping() {
|
||||
assertThat(this.properties.getMimeMappings())
|
||||
.containsExactly(MimeMappings.DEFAULT.getAll().toArray(new Mapping[0]));
|
||||
assertThat(this.properties.getMimeMappings()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCustomizedMimeMapping() {
|
||||
MimeMappings expectedMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT);
|
||||
MimeMappings expectedMappings = new MimeMappings();
|
||||
expectedMappings.add("mjs", "text/javascript");
|
||||
bind("server.mime-mappings.mjs", "text/javascript");
|
||||
assertThat(this.properties.getMimeMappings())
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
@@ -29,6 +30,7 @@ import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
||||
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
|
||||
import org.springframework.boot.web.server.Cookie;
|
||||
import org.springframework.boot.web.server.MimeMappings;
|
||||
import org.springframework.boot.web.server.Shutdown;
|
||||
import org.springframework.boot.web.server.Ssl;
|
||||
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
|
||||
@@ -74,10 +76,25 @@ class ServletWebServerFactoryCustomizerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCustomMimeMappings() {
|
||||
void withNoCustomMimeMappingsThenEmptyMimeMappingsIsAdded() {
|
||||
ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
then(factory).should().setMimeMappings(this.properties.getMimeMappings());
|
||||
ArgumentCaptor<MimeMappings> mimeMappingsCaptor = ArgumentCaptor.forClass(MimeMappings.class);
|
||||
then(factory).should().addMimeMappings(mimeMappingsCaptor.capture());
|
||||
MimeMappings mimeMappings = mimeMappingsCaptor.getValue();
|
||||
assertThat(mimeMappings.getAll()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withCustomMimeMappingsThenPopulatedMimeMappingsIsAdded() {
|
||||
this.properties.getMimeMappings().add("a", "alpha");
|
||||
this.properties.getMimeMappings().add("b", "bravo");
|
||||
ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);
|
||||
this.customizer.customize(factory);
|
||||
ArgumentCaptor<MimeMappings> mimeMappingsCaptor = ArgumentCaptor.forClass(MimeMappings.class);
|
||||
then(factory).should().addMimeMappings(mimeMappingsCaptor.capture());
|
||||
MimeMappings mimeMappings = mimeMappingsCaptor.getValue();
|
||||
assertThat(mimeMappings.getAll()).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user