Skip to content

Commit ee93307

Browse files
committed
Align the mime mapping configuration across all three embedded containers
Closes gh-4161
1 parent a79131f commit ee93307

File tree

6 files changed

+81
-32
lines changed

6 files changed

+81
-32
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/MimeMappings.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public final class MimeMappings implements Iterable<Mapping> {
8888
mappings.add("jpg", "image/jpeg");
8989
mappings.add("js", "application/javascript");
9090
mappings.add("jsf", "text/plain");
91+
mappings.add("json", "application/json");
9192
mappings.add("jspf", "text/plain");
9293
mappings.add("kar", "audio/midi");
9394
mappings.add("latex", "application/x-latex");
@@ -111,6 +112,7 @@ public final class MimeMappings implements Iterable<Mapping> {
111112
mappings.add("mpega", "audio/x-mpeg");
112113
mappings.add("mpg", "video/mpeg");
113114
mappings.add("mpv2", "video/mpeg2");
115+
mappings.add("ms", "application/x-wais-source");
114116
mappings.add("nc", "application/x-netcdf");
115117
mappings.add("oda", "application/oda");
116118
mappings.add("odb", "application/vnd.oasis.opendocument.database");
@@ -206,7 +208,6 @@ public final class MimeMappings implements Iterable<Mapping> {
206208
mappings.add("wmv", "video/x-ms-wmv");
207209
mappings.add("wrl", "model/vrml");
208210
mappings.add("wspolicy", "application/wspolicy+xml");
209-
mappings.add("Z", "application/x-compress");
210211
mappings.add("z", "application/x-compress");
211212
mappings.add("zip", "application/zip");
212213
DEFAULT = unmodifiableMappings(mappings);
@@ -373,6 +374,12 @@ public boolean equals(Object obj) {
373374
return false;
374375
}
375376

377+
@Override
378+
public String toString() {
379+
return "Mapping [extension=" + this.extension + ", mimeType=" + this.mimeType
380+
+ "]";
381+
}
382+
376383
}
377384

378385
}

spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
import java.nio.charset.Charset;
2626
import java.security.KeyStore;
2727
import java.util.Arrays;
28+
import java.util.Collection;
2829
import java.util.Date;
30+
import java.util.Map;
31+
import java.util.Map.Entry;
32+
import java.util.Set;
2933
import java.util.concurrent.TimeUnit;
3034

3135
import javax.net.ssl.SSLException;
@@ -61,6 +65,9 @@
6165

6266
import static org.hamcrest.Matchers.containsString;
6367
import static org.hamcrest.Matchers.equalTo;
68+
import static org.hamcrest.Matchers.hasEntry;
69+
import static org.hamcrest.Matchers.hasItem;
70+
import static org.hamcrest.Matchers.is;
6471
import static org.hamcrest.Matchers.lessThan;
6572
import static org.hamcrest.Matchers.notNullValue;
6673
import static org.junit.Assert.assertEquals;
@@ -527,6 +534,31 @@ public void defaultSessionTimeout() throws Exception {
527534
assertThat(getFactory().getSessionTimeout(), equalTo(30 * 60));
528535
}
529536

537+
@Test
538+
public void mimeMappingsAreCorrectlyConfigured() throws Exception {
539+
AbstractEmbeddedServletContainerFactory factory = getFactory();
540+
this.container = factory.getEmbeddedServletContainer();
541+
Map<String, String> configuredMimeMappings = getActualMimeMappings();
542+
Set<Entry<String, String>> entrySet = configuredMimeMappings.entrySet();
543+
Collection<MimeMappings.Mapping> expectedMimeMappings = getExpectedMimeMappings();
544+
for (Entry<String, String> entry : entrySet) {
545+
assertThat(expectedMimeMappings,
546+
hasItem(new MimeMappings.Mapping(entry.getKey(), entry.getValue())));
547+
}
548+
for (MimeMappings.Mapping mapping : expectedMimeMappings) {
549+
assertThat(configuredMimeMappings,
550+
hasEntry(mapping.getExtension(), mapping.getMimeType()));
551+
}
552+
assertThat(configuredMimeMappings.size(),
553+
is(equalTo(expectedMimeMappings.size())));
554+
}
555+
556+
protected abstract Map<String, String> getActualMimeMappings();
557+
558+
protected Collection<MimeMappings.Mapping> getExpectedMimeMappings() {
559+
return MimeMappings.DEFAULT.getAll();
560+
}
561+
530562
private void addTestTxtFile(AbstractEmbeddedServletContainerFactory factory)
531563
throws IOException {
532564
FileCopyUtils.copy("test",

spring-boot/src/test/java/org/springframework/boot/context/embedded/MimeMappingsTests.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,13 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24-
import org.apache.catalina.Context;
25-
import org.apache.catalina.Wrapper;
26-
import org.apache.catalina.startup.Tomcat;
2724
import org.junit.Rule;
2825
import org.junit.Test;
2926
import org.junit.rules.ExpectedException;
30-
import org.mockito.invocation.InvocationOnMock;
31-
import org.mockito.stubbing.Answer;
3227

3328
import static org.hamcrest.Matchers.equalTo;
3429
import static org.hamcrest.Matchers.nullValue;
3530
import static org.junit.Assert.assertThat;
36-
import static org.mockito.BDDMockito.given;
37-
import static org.mockito.BDDMockito.willAnswer;
38-
import static org.mockito.Matchers.anyString;
39-
import static org.mockito.Mockito.mock;
4031

4132
/**
4233
* Tests for {@link MimeMappings}.
@@ -48,11 +39,6 @@ public class MimeMappingsTests {
4839
@Rule
4940
public ExpectedException thrown = ExpectedException.none();
5041

51-
@Test
52-
public void defaults() throws Exception {
53-
assertThat(MimeMappings.DEFAULT, equalTo(getTomatDefaults()));
54-
}
55-
5642
@Test(expected = UnsupportedOperationException.class)
5743
public void defaultsCannotBeModified() throws Exception {
5844
MimeMappings.DEFAULT.add("foo", "foo/bar");
@@ -155,21 +141,4 @@ public void makeUnmodifiable() throws Exception {
155141
assertThat(unmodifiable.get("foo"), nullValue());
156142
}
157143

158-
private MimeMappings getTomatDefaults() {
159-
final MimeMappings mappings = new MimeMappings();
160-
Context ctx = mock(Context.class);
161-
Wrapper wrapper = mock(Wrapper.class);
162-
given(ctx.createWrapper()).willReturn(wrapper);
163-
willAnswer(new Answer<Object>() {
164-
@Override
165-
public Object answer(InvocationOnMock invocation) throws Throwable {
166-
Object[] args = invocation.getArguments();
167-
mappings.add((String) args[0], (String) args[1]);
168-
return null;
169-
}
170-
}).given(ctx).addMimeMapping(anyString(), anyString());
171-
Tomcat.initWebappDefaults(ctx);
172-
return mappings;
173-
}
174-
175144
}

spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.context.embedded.jetty;
1818

1919
import java.util.Arrays;
20+
import java.util.Map;
2021
import java.util.concurrent.TimeUnit;
2122

2223
import org.eclipse.jetty.server.Handler;
@@ -161,4 +162,11 @@ public void basicSslClasspathKeyStore() throws Exception {
161162
testBasicSslWithKeyStore("classpath:test.jks");
162163
}
163164

165+
@Override
166+
protected Map<String, String> getActualMimeMappings() {
167+
WebAppContext context = (WebAppContext) ((JettyEmbeddedServletContainer) this.container)
168+
.getServer().getHandler();
169+
return context.getMimeTypes().getMimeMap();
170+
}
171+
164172
}

spring-boot/src/test/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactoryTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
4040
import org.springframework.boot.context.embedded.Ssl;
41+
import org.springframework.test.util.ReflectionTestUtils;
4142
import org.springframework.util.SocketUtils;
4243

4344
import static org.hamcrest.Matchers.equalTo;
@@ -311,6 +312,15 @@ public void run() {
311312

312313
}
313314

315+
@SuppressWarnings("unchecked")
316+
@Override
317+
protected Map<String, String> getActualMimeMappings() {
318+
Context context = (Context) ((TomcatEmbeddedServletContainer) this.container)
319+
.getTomcat().getHost().findChildren()[0];
320+
return (Map<String, String>) ReflectionTestUtils.getField(context,
321+
"mimeMappings");
322+
}
323+
314324
private void assertTimeout(TomcatEmbeddedServletContainerFactory factory,
315325
int expected) {
316326
Tomcat tomcat = getTomcat(factory);

spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,26 @@
1717
package org.springframework.boot.context.embedded.undertow;
1818

1919
import java.util.Arrays;
20+
import java.util.Collection;
21+
import java.util.HashSet;
22+
import java.util.Map;
23+
import java.util.Set;
2024
import java.util.concurrent.atomic.AtomicReference;
2125

2226
import io.undertow.Undertow.Builder;
2327
import io.undertow.servlet.api.DeploymentInfo;
28+
import io.undertow.servlet.api.DeploymentManager;
2429
import org.junit.Test;
2530
import org.mockito.InOrder;
2631

2732
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory;
2833
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
2934
import org.springframework.boot.context.embedded.ErrorPage;
3035
import org.springframework.boot.context.embedded.ExampleServlet;
36+
import org.springframework.boot.context.embedded.MimeMappings.Mapping;
3137
import org.springframework.boot.context.embedded.ServletRegistrationBean;
3238
import org.springframework.http.HttpStatus;
39+
import org.springframework.test.util.ReflectionTestUtils;
3340

3441
import static org.hamcrest.Matchers.equalTo;
3542
import static org.junit.Assert.assertEquals;
@@ -149,4 +156,20 @@ public void customize(DeploymentInfo deploymentInfo) {
149156
assertEquals("/", contextPath.get());
150157
}
151158

159+
@Override
160+
protected Map<String, String> getActualMimeMappings() {
161+
return ((DeploymentManager) ReflectionTestUtils.getField(this.container,
162+
"manager")).getDeployment().getMimeExtensionMappings();
163+
}
164+
165+
@Override
166+
protected Collection<Mapping> getExpectedMimeMappings() {
167+
// Unlike Tomcat and Jetty, Undertow performs a case-sensitive match on file
168+
// extension so it has a mapping for "z" and "Z".
169+
Set<Mapping> expectedMappings = new HashSet<Mapping>(
170+
super.getExpectedMimeMappings());
171+
expectedMappings.add(new Mapping("Z", "application/x-compress"));
172+
return expectedMappings;
173+
}
174+
152175
}

0 commit comments

Comments
 (0)