Ensure default mime mappings are applied

Fixes gh-40860
This commit is contained in:
Andy Wilkinson
2024-05-22 12:19:31 +01:00
parent baf34c43f3
commit da4c2db3a7
7 changed files with 55 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@@ -178,6 +178,11 @@ public abstract class AbstractServletWebServerFactory extends AbstractConfigurab
this.mimeMappings = new MimeMappings(mimeMappings);
}
@Override
public void addMimeMappings(MimeMappings mimeMappings) {
mimeMappings.forEach((mapping) -> this.mimeMappings.add(mapping.getExtension(), mapping.getMimeType()));
}
/**
* Returns the document root which will be used by the web context to serve static
* files.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2024 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.
@@ -82,6 +82,13 @@ public interface ConfigurableServletWebServerFactory
*/
void setMimeMappings(MimeMappings mimeMappings);
/**
* Adds mime-type mappings.
* @param mimeMappings the mime type mappings to add
* @since 3.3.0
*/
void addMimeMappings(MimeMappings mimeMappings);
/**
* Sets the document root directory which will be used by the web context to serve
* static files.

View File

@@ -34,6 +34,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
@@ -1010,6 +1011,23 @@ public abstract class AbstractServletWebServerFactoryTests {
assertThat(configuredMimeMappings).containsExactlyInAnyOrderElementsOf(expectedMimeMappings);
}
@Test
void additionalMimeMappingsCanBeConfigured() {
AbstractServletWebServerFactory factory = getFactory();
MimeMappings additionalMimeMappings = new MimeMappings();
additionalMimeMappings.add("a", "alpha");
additionalMimeMappings.add("b", "bravo");
factory.addMimeMappings(additionalMimeMappings);
this.webServer = factory.getWebServer();
Collection<MimeMappings.Mapping> configuredMimeMappings = getActualMimeMappings().entrySet()
.stream()
.map((entry) -> new MimeMappings.Mapping(entry.getKey(), entry.getValue()))
.toList();
List<MimeMappings.Mapping> expectedMimeMappings = new ArrayList<>(MimeMappings.DEFAULT.getAll());
expectedMimeMappings.addAll(additionalMimeMappings.getAll());
assertThat(configuredMimeMappings).containsExactlyInAnyOrderElementsOf(expectedMimeMappings);
}
@Test
void rootServletContextResource() {
AbstractServletWebServerFactory factory = getFactory();