|
| 1 | +/* |
| 2 | + * Copyright 2002-2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.web.servlet; |
| 18 | + |
| 19 | + |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.springframework.beans.BeansException; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 26 | +import org.springframework.context.annotation.Bean; |
| 27 | +import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.core.PriorityOrdered; |
| 29 | +import org.springframework.stereotype.Component; |
| 30 | +import org.springframework.stereotype.Controller; |
| 31 | +import org.springframework.test.context.ContextConfiguration; |
| 32 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 33 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 34 | +import org.springframework.ui.Model; |
| 35 | +import org.springframework.web.bind.annotation.PathVariable; |
| 36 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 37 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 38 | + |
| 39 | +import org.springframework.web.context.WebApplicationContext; |
| 40 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 41 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
| 42 | +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
| 43 | +import org.springframework.web.util.UriComponentsBuilder; |
| 44 | + |
| 45 | +import java.net.URI; |
| 46 | + |
| 47 | +import static org.hamcrest.core.Is.is; |
| 48 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 49 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; |
| 50 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 51 | +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; |
| 52 | + |
| 53 | +/** |
| 54 | + * Tests for SPR-11441 (MockMvc needs to accept prepared URI with encoded URI path variables). |
| 55 | + * |
| 56 | + * @author Sebastien Deleuze |
| 57 | + */ |
| 58 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 59 | +@WebAppConfiguration |
| 60 | +@ContextConfiguration |
| 61 | +public class Spr11441Tests { |
| 62 | + |
| 63 | + @Autowired |
| 64 | + private WebApplicationContext wac; |
| 65 | + |
| 66 | + private MockMvc mockMvc; |
| 67 | + |
| 68 | + @Before |
| 69 | + public void setup() { |
| 70 | + this.mockMvc = webAppContextSetup(this.wac).build(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void test() throws Exception { |
| 75 | + String id = "a/b"; |
| 76 | + URI url = UriComponentsBuilder.fromUriString("/circuit").pathSegment(id).build().encode().toUri(); |
| 77 | + ResultActions result = mockMvc.perform(get(url)); |
| 78 | + result.andExpect(status().isOk()).andExpect(model().attribute("receivedId", is(id))); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + @Configuration |
| 83 | + @EnableWebMvc |
| 84 | + static class WebConfig extends WebMvcConfigurerAdapter { |
| 85 | + |
| 86 | + @Bean |
| 87 | + public MyController myController() { |
| 88 | + return new MyController(); |
| 89 | + } |
| 90 | + |
| 91 | + @Bean |
| 92 | + public HandlerMappingConfigurer myHandlerMappingConfigurer() { |
| 93 | + return new HandlerMappingConfigurer(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Controller |
| 98 | + private static class MyController { |
| 99 | + |
| 100 | + @RequestMapping(value = "/circuit/{id}", method = RequestMethod.GET) |
| 101 | + public String getCircuit(@PathVariable String id, Model model) { |
| 102 | + model.addAttribute("receivedId", id); |
| 103 | + return "result"; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Component |
| 108 | + private static class HandlerMappingConfigurer implements BeanPostProcessor, PriorityOrdered { |
| 109 | + |
| 110 | + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| 111 | + if (bean instanceof RequestMappingHandlerMapping) { |
| 112 | + RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) bean; |
| 113 | + |
| 114 | + // URL decode after request mapping, not before. |
| 115 | + requestMappingHandlerMapping.setUrlDecode(false); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + return bean; |
| 120 | + } |
| 121 | + |
| 122 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 123 | + return bean; |
| 124 | + } |
| 125 | + |
| 126 | + public int getOrder() { |
| 127 | + return PriorityOrdered.HIGHEST_PRECEDENCE; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments