mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Merge pull request #39430 from BenchmarkingBuffalo
* pr/39430: Add possibility for custom MimeMappings Closes gh-39430
This commit is contained in:
commit
500584f052
@ -37,6 +37,7 @@ import org.springframework.boot.convert.DurationUnit;
|
|||||||
import org.springframework.boot.web.server.Compression;
|
import org.springframework.boot.web.server.Compression;
|
||||||
import org.springframework.boot.web.server.Cookie;
|
import org.springframework.boot.web.server.Cookie;
|
||||||
import org.springframework.boot.web.server.Http2;
|
import org.springframework.boot.web.server.Http2;
|
||||||
|
import org.springframework.boot.web.server.MimeMappings;
|
||||||
import org.springframework.boot.web.server.Shutdown;
|
import org.springframework.boot.web.server.Shutdown;
|
||||||
import org.springframework.boot.web.server.Ssl;
|
import org.springframework.boot.web.server.Ssl;
|
||||||
import org.springframework.boot.web.servlet.server.Encoding;
|
import org.springframework.boot.web.servlet.server.Encoding;
|
||||||
@ -71,6 +72,7 @@ import org.springframework.util.unit.DataSize;
|
|||||||
* @author Parviz Rozikov
|
* @author Parviz Rozikov
|
||||||
* @author Florian Storz
|
* @author Florian Storz
|
||||||
* @author Michael Weidmann
|
* @author Michael Weidmann
|
||||||
|
* @author Lasse Wulff
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
|
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
|
||||||
@ -115,6 +117,8 @@ public class ServerProperties {
|
|||||||
@NestedConfigurationProperty
|
@NestedConfigurationProperty
|
||||||
private final Compression compression = new Compression();
|
private final Compression compression = new Compression();
|
||||||
|
|
||||||
|
private final MimeMappings mimeMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT);
|
||||||
|
|
||||||
@NestedConfigurationProperty
|
@NestedConfigurationProperty
|
||||||
private final Http2 http2 = new Http2();
|
private final Http2 http2 = new Http2();
|
||||||
|
|
||||||
@ -186,6 +190,14 @@ public class ServerProperties {
|
|||||||
return this.compression;
|
return this.compression;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MimeMappings getMimeMappings() {
|
||||||
|
return this.mimeMappings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMimeMappings(Map<String, String> customMappings) {
|
||||||
|
customMappings.forEach(this.mimeMappings::add);
|
||||||
|
}
|
||||||
|
|
||||||
public Http2 getHttp2() {
|
public Http2 getHttp2() {
|
||||||
return this.http2;
|
return this.http2;
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ import org.springframework.util.CollectionUtils;
|
|||||||
* @author Olivier Lamy
|
* @author Olivier Lamy
|
||||||
* @author Yunkun Huang
|
* @author Yunkun Huang
|
||||||
* @author Scott Frederick
|
* @author Scott Frederick
|
||||||
|
* @author Lasse Wulff
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
public class ServletWebServerFactoryCustomizer
|
public class ServletWebServerFactoryCustomizer
|
||||||
@ -94,6 +95,7 @@ public class ServletWebServerFactoryCustomizer
|
|||||||
map.from(() -> this.cookieSameSiteSuppliers)
|
map.from(() -> this.cookieSameSiteSuppliers)
|
||||||
.whenNot(CollectionUtils::isEmpty)
|
.whenNot(CollectionUtils::isEmpty)
|
||||||
.to(factory::setCookieSameSiteSuppliers);
|
.to(factory::setCookieSameSiteSuppliers);
|
||||||
|
map.from(this.serverProperties::getMimeMappings).to(factory::setMimeMappings);
|
||||||
this.webListenerRegistrars.forEach((registrar) -> registrar.register(factory));
|
this.webListenerRegistrars.forEach((registrar) -> registrar.register(factory));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,6 +46,8 @@ import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories;
|
|||||||
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
|
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
|
||||||
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
|
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
|
||||||
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
||||||
|
import org.springframework.boot.web.server.MimeMappings;
|
||||||
|
import org.springframework.boot.web.server.MimeMappings.Mapping;
|
||||||
import org.springframework.test.util.ReflectionTestUtils;
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
import org.springframework.util.unit.DataSize;
|
import org.springframework.util.unit.DataSize;
|
||||||
|
|
||||||
@ -66,6 +68,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
* @author Rafiullah Hamedy
|
* @author Rafiullah Hamedy
|
||||||
* @author Chris Bono
|
* @author Chris Bono
|
||||||
* @author Parviz Rozikov
|
* @author Parviz Rozikov
|
||||||
|
* @author Lasse Wulff
|
||||||
*/
|
*/
|
||||||
@DirtiesUrlFactories
|
@DirtiesUrlFactories
|
||||||
class ServerPropertiesTests {
|
class ServerPropertiesTests {
|
||||||
@ -182,6 +185,21 @@ class ServerPropertiesTests {
|
|||||||
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets /copy");
|
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets /copy");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDefaultMimeMapping() {
|
||||||
|
assertThat(this.properties.getMimeMappings())
|
||||||
|
.containsExactly(MimeMappings.DEFAULT.getAll().toArray(new Mapping[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCustomizedMimeMapping() {
|
||||||
|
MimeMappings expectedMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT);
|
||||||
|
expectedMappings.add("mjs", "text/javascript");
|
||||||
|
bind("server.mime-mappings.mjs", "text/javascript");
|
||||||
|
assertThat(this.properties.getMimeMappings())
|
||||||
|
.containsExactly(expectedMappings.getAll().toArray(new Mapping[0]));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCustomizeUriEncoding() {
|
void testCustomizeUriEncoding() {
|
||||||
bind("server.tomcat.uri-encoding", "US-ASCII");
|
bind("server.tomcat.uri-encoding", "US-ASCII");
|
||||||
|
@ -45,6 +45,7 @@ import static org.mockito.Mockito.mock;
|
|||||||
*
|
*
|
||||||
* @author Brian Clozel
|
* @author Brian Clozel
|
||||||
* @author Yunkun Huang
|
* @author Yunkun Huang
|
||||||
|
* @author Lasse Wulff
|
||||||
*/
|
*/
|
||||||
class ServletWebServerFactoryCustomizerTests {
|
class ServletWebServerFactoryCustomizerTests {
|
||||||
|
|
||||||
@ -72,6 +73,13 @@ class ServletWebServerFactoryCustomizerTests {
|
|||||||
then(factory).should().setDisplayName("TestName");
|
then(factory).should().setDisplayName("TestName");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCustomMimeMappings() {
|
||||||
|
ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);
|
||||||
|
this.customizer.customize(factory);
|
||||||
|
then(factory).should().setMimeMappings(this.properties.getMimeMappings());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCustomizeDefaultServlet() {
|
void testCustomizeDefaultServlet() {
|
||||||
ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);
|
ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);
|
||||||
|
Loading…
Reference in New Issue
Block a user