mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-22 22:19:03 +00:00
Polishing in MappingContentNegotiationStrategy hierarchy and tests
See gh-36925
This commit is contained in:
+23
-26
@@ -23,9 +23,11 @@ import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
/**
|
||||
* A test fixture with a test subclass of AbstractMappingContentNegotiationStrategy.
|
||||
@@ -36,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
class MappingContentNegotiationStrategyTests {
|
||||
|
||||
@Test
|
||||
void resolveMediaTypes() throws Exception {
|
||||
void resolveMediaTypesFromRegisteredExtensions() throws Exception {
|
||||
Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
|
||||
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("json", mapping);
|
||||
|
||||
@@ -47,27 +49,7 @@ class MappingContentNegotiationStrategyTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveMediaTypesNoMatch() throws Exception {
|
||||
Map<String, MediaType> mapping = null;
|
||||
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("blah", mapping);
|
||||
|
||||
List<MediaType> mediaTypes = strategy.resolveMediaTypes(null);
|
||||
|
||||
assertThat(mediaTypes).isEqualTo(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveMediaTypesNoKey() throws Exception {
|
||||
Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
|
||||
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy(null, mapping);
|
||||
|
||||
List<MediaType> mediaTypes = strategy.resolveMediaTypes(null);
|
||||
|
||||
assertThat(mediaTypes).isEqualTo(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveMediaTypesHandleNoMatch() throws Exception {
|
||||
void resolveMediaTypesViaMediaTypeFactory() throws Exception {
|
||||
Map<String, MediaType> mapping = null;
|
||||
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("xml", mapping);
|
||||
|
||||
@@ -77,6 +59,25 @@ class MappingContentNegotiationStrategyTests {
|
||||
assertThat(mediaTypes.get(0).toString()).isEqualTo("application/xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveMediaTypesUnknownKey() {
|
||||
Map<String, MediaType> mapping = null;
|
||||
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("blah", mapping);
|
||||
|
||||
assertThatThrownBy(() -> strategy.resolveMediaTypes(null))
|
||||
.isInstanceOf(HttpMediaTypeNotAcceptableException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveMediaTypesNullKey() throws Exception {
|
||||
Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
|
||||
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy(null, mapping);
|
||||
|
||||
List<MediaType> mediaTypes = strategy.resolveMediaTypes(null);
|
||||
|
||||
assertThat(mediaTypes).isEqualTo(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST);
|
||||
}
|
||||
|
||||
|
||||
private static class TestMappingContentNegotiationStrategy extends AbstractMappingContentNegotiationStrategy {
|
||||
|
||||
@@ -92,10 +93,6 @@ class MappingContentNegotiationStrategyTests {
|
||||
return this.extension;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MediaType handleNoMatch(NativeWebRequest request, String mappingKey) {
|
||||
return "xml".equals(mappingKey) ? MediaType.APPLICATION_XML : null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+8
-6
@@ -68,13 +68,15 @@ class MappingMediaTypeFileExtensionResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void allFileExtensions() {
|
||||
Map<String, MediaType> mappings = new HashMap<>();
|
||||
mappings.put("json", MediaType.APPLICATION_JSON);
|
||||
mappings.put("JsOn", MediaType.APPLICATION_JSON);
|
||||
mappings.put("jSoN", MediaType.APPLICATION_JSON);
|
||||
void mappingsAreCaseInsensitive() {
|
||||
Map<String, MediaType> map = new HashMap<>();
|
||||
map.put("json", MediaType.APPLICATION_JSON);
|
||||
map.put("JsOn", MediaType.APPLICATION_JSON);
|
||||
map.put("jSoN", MediaType.APPLICATION_JSON);
|
||||
|
||||
MappingMediaTypeFileExtensionResolver resolver = new MappingMediaTypeFileExtensionResolver(mappings);
|
||||
MappingMediaTypeFileExtensionResolver resolver = new MappingMediaTypeFileExtensionResolver(map);
|
||||
assertThat(resolver.getAllFileExtensions()).containsExactly("json");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user