From ef508ed6dc6579e43be2758514250734e3e4d783 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 8 Mar 2017 11:04:39 +0100 Subject: [PATCH] DATAREST-1019 - Consider base URI when resolving cross-origin configuration on repositories. We now consider the base URI when resolving CORS configuration from repository interfaces. The base URI is now stripped from the request. Previously the base URI was not stripped from the request and was used to determine an exported resource. --- .../webmvc/RepositoryRestHandlerMapping.java | 2 +- ...RepositoryRestHandlerMappingUnitTests.java | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java index 87d1fce7d..ad17edf2c 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java @@ -210,7 +210,7 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping { return corsConfiguration; } - CorsConfiguration repositoryCorsConfiguration = corsConfigurationAccessor.findCorsConfiguration(lookupPath); + CorsConfiguration repositoryCorsConfiguration = corsConfigurationAccessor.findCorsConfiguration(repositoryLookupPath); return corsConfiguration == null ? repositoryCorsConfiguration : corsConfiguration.combine(repositoryCorsConfiguration); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java index cd43863b0..7df5d40c4 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java @@ -20,6 +20,7 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; import java.lang.reflect.Method; +import java.util.Collections; import org.junit.Before; import org.junit.Test; @@ -28,11 +29,13 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.data.domain.Sort; import org.springframework.data.repository.support.Repositories; +import org.springframework.data.rest.core.Path; import org.springframework.data.rest.core.config.EnumTranslationConfiguration; import org.springframework.data.rest.core.config.MetadataConfiguration; import org.springframework.data.rest.core.config.ProjectionDefinitionConfiguration; import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.core.mapping.ResourceMappings; +import org.springframework.data.rest.core.mapping.ResourceMetadata; import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; import org.springframework.data.rest.webmvc.support.DefaultedPageable; import org.springframework.mock.web.MockHttpServletRequest; @@ -45,6 +48,7 @@ import org.springframework.web.method.HandlerMethod; * * @author Oliver Gierke * @author Greg Turnquist + * @author Mark Paluch */ @RunWith(MockitoJUnitRunner.class) public class RepositoryRestHandlerMappingUnitTests { @@ -58,6 +62,7 @@ public class RepositoryRestHandlerMappingUnitTests { } @Mock ResourceMappings mappings; + @Mock ResourceMetadata resourceMetadata; @Mock Repositories repositories; RepositoryRestConfiguration configuration; @@ -213,6 +218,43 @@ public class RepositoryRestHandlerMappingUnitTests { assertThat(handlerMapping.getHandler(mockRequest), is(nullValue())); } + @Test // DATAREST-1019 + public void resolvesCorsConfigurationFromRequestUri() { + + String uri = "/people"; + + when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true); + when(mappings.iterator()).thenReturn(Collections.singleton(resourceMetadata).iterator()); + when(resourceMetadata.getPath()).thenReturn(new Path("/people")); + + mockRequest = new MockHttpServletRequest("GET", uri); + mockRequest.setServletPath(uri); + + handlerMapping.getCorsConfiguration(uri, mockRequest); + + verify(mappings).exportsTopLevelResourceFor("/people"); + } + + @Test // DATAREST-1019 + public void stripsBaseUriForCorsConfigurationResolution() { + + String baseUri = "/foo"; + String uri = baseUri.concat("/people"); + + configuration.setBasePath(baseUri); + + when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true); + when(mappings.iterator()).thenReturn(Collections.singleton(resourceMetadata).iterator()); + when(resourceMetadata.getPath()).thenReturn(new Path("/people")); + + mockRequest = new MockHttpServletRequest("GET", uri); + mockRequest.setServletPath(uri); + + handlerMapping.getCorsConfiguration(uri, mockRequest); + + verify(mappings).exportsTopLevelResourceFor("/people"); + } + @Test // DATAREST-994 public void twoArgumentConstructorDoesNotThrowException() { new RepositoryRestHandlerMapping(mappings, configuration);