From 5fc2b1af666aba90166f3b048f1f7978fe88a7a3 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 2 Mar 2015 09:03:59 +0000 Subject: [PATCH] Fix @RequestMapping so that git label can contain periods Spring MVC lops off the "extension" from request paths by default (causing no end of other problems), but in this case making it impossible, or unintuitive at best, to use version-like labels in git repositories (e.g. 1.0.0). This change makes the mapping a regex match which includes the whole path, not just the stem. Fixes gh-94 --- .../config/server/EnvironmentController.java | 2 +- ...EnvironmentControllerIntegrationTests.java | 67 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/EnvironmentControllerIntegrationTests.java diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java index ac520218..318b63d5 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java @@ -56,7 +56,7 @@ public class EnvironmentController { return labelled(name, profiles, defaultLabel); } - @RequestMapping("/{name}/{profiles}/{label}") + @RequestMapping("/{name}/{profiles}/{label:.*}") public Environment labelled(@PathVariable String name, @PathVariable String profiles, @PathVariable String label) { Environment environment = encryption.decrypt(repository.findOne(name, profiles, diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/EnvironmentControllerIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/EnvironmentControllerIntegrationTests.java new file mode 100644 index 00000000..02295c83 --- /dev/null +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/EnvironmentControllerIntegrationTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2014-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.config.server; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.springframework.cloud.config.environment.Environment; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +/** + * @author Dave Syer + * + */ +public class EnvironmentControllerIntegrationTests { + + private MockMvc mvc; + private EnvironmentRepository repository = Mockito.mock(EnvironmentRepository.class);; + + @Before + public void init() { + mvc = MockMvcBuilders.standaloneSetup( + new EnvironmentController(repository, new EncryptionController())) + .build(); + } + + @Test + public void environmentNoLabel() throws Exception { + Mockito.when(repository.findOne("foo", "default", "master")).thenReturn( + new Environment("foo", "default")); + mvc.perform(MockMvcRequestBuilders.get("/foo/default")).andExpect( + MockMvcResultMatchers.status().isOk()); + } + + @Test + public void environmentWithLabel() throws Exception { + Mockito.when(repository.findOne("foo", "default", "awesome")).thenReturn( + new Environment("foo", "default")); + mvc.perform(MockMvcRequestBuilders.get("/foo/default/awesome")).andExpect( + MockMvcResultMatchers.status().isOk()); + } + @Test + public void environmentWithLabelContainingPeriod() throws Exception { + Mockito.when(repository.findOne("foo", "default", "1.0.0")).thenReturn( + new Environment("foo", "default")); + mvc.perform(MockMvcRequestBuilders.get("/foo/default/1.0.0")).andExpect( + MockMvcResultMatchers.status().isOk()); + } + +}