Align the mime mapping configuration across all three embedded containers
Closes gh-4161
This commit is contained in:
parent
a79131f8d2
commit
ee93307402
|
|
@ -88,6 +88,7 @@ public final class MimeMappings implements Iterable<Mapping> {
|
|||
mappings.add("jpg", "image/jpeg");
|
||||
mappings.add("js", "application/javascript");
|
||||
mappings.add("jsf", "text/plain");
|
||||
mappings.add("json", "application/json");
|
||||
mappings.add("jspf", "text/plain");
|
||||
mappings.add("kar", "audio/midi");
|
||||
mappings.add("latex", "application/x-latex");
|
||||
|
|
@ -111,6 +112,7 @@ public final class MimeMappings implements Iterable<Mapping> {
|
|||
mappings.add("mpega", "audio/x-mpeg");
|
||||
mappings.add("mpg", "video/mpeg");
|
||||
mappings.add("mpv2", "video/mpeg2");
|
||||
mappings.add("ms", "application/x-wais-source");
|
||||
mappings.add("nc", "application/x-netcdf");
|
||||
mappings.add("oda", "application/oda");
|
||||
mappings.add("odb", "application/vnd.oasis.opendocument.database");
|
||||
|
|
@ -206,7 +208,6 @@ public final class MimeMappings implements Iterable<Mapping> {
|
|||
mappings.add("wmv", "video/x-ms-wmv");
|
||||
mappings.add("wrl", "model/vrml");
|
||||
mappings.add("wspolicy", "application/wspolicy+xml");
|
||||
mappings.add("Z", "application/x-compress");
|
||||
mappings.add("z", "application/x-compress");
|
||||
mappings.add("zip", "application/zip");
|
||||
DEFAULT = unmodifiableMappings(mappings);
|
||||
|
|
@ -373,6 +374,12 @@ public final class MimeMappings implements Iterable<Mapping> {
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Mapping [extension=" + this.extension + ", mimeType=" + this.mimeType
|
||||
+ "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,11 @@ import java.net.URISyntaxException;
|
|||
import java.nio.charset.Charset;
|
||||
import java.security.KeyStore;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.net.ssl.SSLException;
|
||||
|
|
@ -61,6 +65,9 @@ import org.springframework.util.concurrent.ListenableFuture;
|
|||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasEntry;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.lessThan;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
|
@ -527,6 +534,31 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
|
|||
assertThat(getFactory().getSessionTimeout(), equalTo(30 * 60));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mimeMappingsAreCorrectlyConfigured() throws Exception {
|
||||
AbstractEmbeddedServletContainerFactory factory = getFactory();
|
||||
this.container = factory.getEmbeddedServletContainer();
|
||||
Map<String, String> configuredMimeMappings = getActualMimeMappings();
|
||||
Set<Entry<String, String>> entrySet = configuredMimeMappings.entrySet();
|
||||
Collection<MimeMappings.Mapping> expectedMimeMappings = getExpectedMimeMappings();
|
||||
for (Entry<String, String> entry : entrySet) {
|
||||
assertThat(expectedMimeMappings,
|
||||
hasItem(new MimeMappings.Mapping(entry.getKey(), entry.getValue())));
|
||||
}
|
||||
for (MimeMappings.Mapping mapping : expectedMimeMappings) {
|
||||
assertThat(configuredMimeMappings,
|
||||
hasEntry(mapping.getExtension(), mapping.getMimeType()));
|
||||
}
|
||||
assertThat(configuredMimeMappings.size(),
|
||||
is(equalTo(expectedMimeMappings.size())));
|
||||
}
|
||||
|
||||
protected abstract Map<String, String> getActualMimeMappings();
|
||||
|
||||
protected Collection<MimeMappings.Mapping> getExpectedMimeMappings() {
|
||||
return MimeMappings.DEFAULT.getAll();
|
||||
}
|
||||
|
||||
private void addTestTxtFile(AbstractEmbeddedServletContainerFactory factory)
|
||||
throws IOException {
|
||||
FileCopyUtils.copy("test",
|
||||
|
|
|
|||
|
|
@ -21,22 +21,13 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.Wrapper;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.willAnswer;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link MimeMappings}.
|
||||
|
|
@ -48,11 +39,6 @@ public class MimeMappingsTests {
|
|||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void defaults() throws Exception {
|
||||
assertThat(MimeMappings.DEFAULT, equalTo(getTomatDefaults()));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void defaultsCannotBeModified() throws Exception {
|
||||
MimeMappings.DEFAULT.add("foo", "foo/bar");
|
||||
|
|
@ -155,21 +141,4 @@ public class MimeMappingsTests {
|
|||
assertThat(unmodifiable.get("foo"), nullValue());
|
||||
}
|
||||
|
||||
private MimeMappings getTomatDefaults() {
|
||||
final MimeMappings mappings = new MimeMappings();
|
||||
Context ctx = mock(Context.class);
|
||||
Wrapper wrapper = mock(Wrapper.class);
|
||||
given(ctx.createWrapper()).willReturn(wrapper);
|
||||
willAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] args = invocation.getArguments();
|
||||
mappings.add((String) args[0], (String) args[1]);
|
||||
return null;
|
||||
}
|
||||
}).given(ctx).addMimeMapping(anyString(), anyString());
|
||||
Tomcat.initWebappDefaults(ctx);
|
||||
return mappings;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.boot.context.embedded.jetty;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.eclipse.jetty.server.Handler;
|
||||
|
|
@ -161,4 +162,11 @@ public class JettyEmbeddedServletContainerFactoryTests
|
|||
testBasicSslWithKeyStore("classpath:test.jks");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, String> getActualMimeMappings() {
|
||||
WebAppContext context = (WebAppContext) ((JettyEmbeddedServletContainer) this.container)
|
||||
.getServer().getHandler();
|
||||
return context.getMimeTypes().getMimeMap();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ import org.mockito.InOrder;
|
|||
|
||||
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
|
||||
import org.springframework.boot.context.embedded.Ssl;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
|
@ -311,6 +312,15 @@ public class TomcatEmbeddedServletContainerFactoryTests
|
|||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
protected Map<String, String> getActualMimeMappings() {
|
||||
Context context = (Context) ((TomcatEmbeddedServletContainer) this.container)
|
||||
.getTomcat().getHost().findChildren()[0];
|
||||
return (Map<String, String>) ReflectionTestUtils.getField(context,
|
||||
"mimeMappings");
|
||||
}
|
||||
|
||||
private void assertTimeout(TomcatEmbeddedServletContainerFactory factory,
|
||||
int expected) {
|
||||
Tomcat tomcat = getTomcat(factory);
|
||||
|
|
|
|||
|
|
@ -17,10 +17,15 @@
|
|||
package org.springframework.boot.context.embedded.undertow;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import io.undertow.Undertow.Builder;
|
||||
import io.undertow.servlet.api.DeploymentInfo;
|
||||
import io.undertow.servlet.api.DeploymentManager;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
|
|
@ -28,8 +33,10 @@ import org.springframework.boot.context.embedded.AbstractEmbeddedServletContaine
|
|||
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
|
||||
import org.springframework.boot.context.embedded.ErrorPage;
|
||||
import org.springframework.boot.context.embedded.ExampleServlet;
|
||||
import org.springframework.boot.context.embedded.MimeMappings.Mapping;
|
||||
import org.springframework.boot.context.embedded.ServletRegistrationBean;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
|
@ -149,4 +156,20 @@ public class UndertowEmbeddedServletContainerFactoryTests
|
|||
assertEquals("/", contextPath.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, String> getActualMimeMappings() {
|
||||
return ((DeploymentManager) ReflectionTestUtils.getField(this.container,
|
||||
"manager")).getDeployment().getMimeExtensionMappings();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<Mapping> getExpectedMimeMappings() {
|
||||
// Unlike Tomcat and Jetty, Undertow performs a case-sensitive match on file
|
||||
// extension so it has a mapping for "z" and "Z".
|
||||
Set<Mapping> expectedMappings = new HashSet<Mapping>(
|
||||
super.getExpectedMimeMappings());
|
||||
expectedMappings.add(new Mapping("Z", "application/x-compress"));
|
||||
return expectedMappings;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue