Merge branch '7.0.x'

This commit is contained in:
rstoyanchev
2026-06-22 13:50:10 +01:00
17 changed files with 198 additions and 180 deletions
@@ -35,10 +35,11 @@ import static org.mockito.Mockito.mock;
*/
class ReactorUriHelperTests {
private final HttpServerRequest nettyRequest = mock();
@Test
void hostnameWithZoneId() throws URISyntaxException {
HttpServerRequest nettyRequest = mock();
given(nettyRequest.scheme()).willReturn("http");
given(nettyRequest.hostName()).willReturn("fe80::a%en1");
given(nettyRequest.hostPort()).willReturn(80);
@@ -50,7 +51,36 @@ class ReactorUriHelperTests {
.hasPort(-1)
.hasPath("/")
.hasToString("http://[fe80::a%25en1]/");
}
@Test
void requestUriWithScheme() throws URISyntaxException {
given(nettyRequest.scheme()).willReturn("http");
given(nettyRequest.hostName()).willReturn("example.org");
given(nettyRequest.hostPort()).willReturn(80);
given(nettyRequest.uri()).willReturn("http://example.org/path");
URI uri = ReactorUriHelper.createUri(nettyRequest);
assertThat(uri).hasScheme("http")
.hasHost("example.org")
.hasPort(-1)
.hasPath("/path")
.hasToString("http://example.org/path");
}
@Test
void requestUriWithoutLeadingSlash() throws URISyntaxException {
given(nettyRequest.scheme()).willReturn("http");
given(nettyRequest.hostName()).willReturn("example.org");
given(nettyRequest.hostPort()).willReturn(80);
given(nettyRequest.uri()).willReturn("foo/bar");
URI uri = ReactorUriHelper.createUri(nettyRequest);
assertThat(uri).hasScheme("http")
.hasHost("example.org")
.hasPort(-1)
.hasPath("/foo/bar")
.hasToString("http://example.org/foo/bar");
}
@ParameterizedTest(name = "{displayName}({arguments})")
@@ -61,8 +91,6 @@ class ReactorUriHelperTests {
"'' | /",
})
void forwardedPrefix(String forwardedPrefixHeader, String expectedPath) throws URISyntaxException {
HttpServerRequest nettyRequest = mock();
given(nettyRequest.scheme()).willReturn("https");
given(nettyRequest.hostName()).willReturn("localhost");
given(nettyRequest.hostPort()).willReturn(443);
@@ -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 resolveMediaTypesViaFactory() throws Exception {
Map<String, MediaType> mapping = null;
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("xml", mapping);
@@ -77,6 +59,34 @@ 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 resolveMediaTypesInvalidKey() {
Map<String, MediaType> mapping = null;
TestMappingContentNegotiationStrategy strategy = new TestMappingContentNegotiationStrategy("not.json", 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 +102,6 @@ class MappingContentNegotiationStrategyTests {
return this.extension;
}
@Override
protected MediaType handleNoMatch(NativeWebRequest request, String mappingKey) {
return "xml".equals(mappingKey) ? MediaType.APPLICATION_XML : null;
}
}
}
@@ -68,13 +68,17 @@ 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(map);
map.forEach(resolver::addMapping);
MappingMediaTypeFileExtensionResolver resolver = new MappingMediaTypeFileExtensionResolver(mappings);
assertThat(resolver.getAllFileExtensions()).containsExactly("json");
}
}