View Javadoc
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.web.servlet.resource;
18  
19  import javax.servlet.http.HttpServlet;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  import org.springframework.context.annotation.Configuration;
27  import org.springframework.mock.web.test.MockFilterChain;
28  import org.springframework.mock.web.test.MockHttpServletRequest;
29  import org.springframework.mock.web.test.MockHttpServletResponse;
30  import org.springframework.mock.web.test.MockServletContext;
31  import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
32  import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
33  import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
34  
35  import static org.junit.Assert.*;
36  
37  
38  /**
39   * Integration tests using {@link ResourceUrlEncodingFilter} and
40   * {@link ResourceUrlProvider} with the latter configured in Spring MVC Java config.
41   *
42   * @author Rossen Stoyanchev
43   */
44  public class ResourceUrlProviderJavaConfigTests {
45  
46  	private final TestServlet servlet = new TestServlet();
47  
48  	private MockFilterChain filterChain;
49  
50  	private MockHttpServletRequest request;
51  
52  	private MockHttpServletResponse response;
53  
54  
55  	@Before
56  	@SuppressWarnings("resource")
57  	public void setup() throws Exception {
58  		this.filterChain = new MockFilterChain(this.servlet, new ResourceUrlEncodingFilter());
59  
60  		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
61  		context.setServletContext(new MockServletContext());
62  		context.register(WebConfig.class);
63  		context.refresh();
64  
65  		this.request = new MockHttpServletRequest("GET", "/");
66  		this.request.setContextPath("/myapp");
67  		this.response = new MockHttpServletResponse();
68  
69  		Object urlProvider = context.getBean(ResourceUrlProvider.class);
70  		this.request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);
71  	}
72  
73  	@Test
74  	public void resolvePathWithServletMappedAsRoot() throws Exception {
75  		this.request.setRequestURI("/myapp/index");
76  		this.request.setServletPath("/index");
77  		this.filterChain.doFilter(this.request, this.response);
78  
79  		assertEquals("/myapp/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css",
80  				resolvePublicResourceUrlPath("/myapp/resources/foo.css"));
81  	}
82  
83  	@Test
84  	public void resolvePathWithServletMappedByPrefix() throws Exception {
85  		this.request.setRequestURI("/myapp/myservlet/index");
86  		this.request.setServletPath("/myservlet");
87  		this.filterChain.doFilter(this.request, this.response);
88  
89  		assertEquals("/myapp/myservlet/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css",
90  				resolvePublicResourceUrlPath("/myapp/myservlet/resources/foo.css"));
91  	}
92  
93  	@Test
94  	public void resolvePathNoMatch() throws Exception {
95  		this.request.setRequestURI("/myapp/myservlet/index");
96  		this.request.setServletPath("/myservlet");
97  		this.filterChain.doFilter(this.request, this.response);
98  
99  		assertEquals("/myapp/myservlet/index", resolvePublicResourceUrlPath("/myapp/myservlet/index"));
100 	}
101 
102 
103 	private String resolvePublicResourceUrlPath(String path) {
104 		return this.servlet.wrappedResponse.encodeURL(path);
105 	}
106 
107 
108 	@Configuration
109 	static class WebConfig extends WebMvcConfigurationSupport {
110 
111 		@Override
112 		public void addResourceHandlers(ResourceHandlerRegistry registry) {
113 			registry.addResourceHandler("/resources/**")
114 				.addResourceLocations("classpath:org/springframework/web/servlet/resource/test/")
115 				.resourceChain(true).addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
116 		}
117 	}
118 
119 	@SuppressWarnings("serial")
120 	private static class TestServlet extends HttpServlet {
121 
122 		private HttpServletResponse wrappedResponse;
123 
124 		@Override
125 		protected void doGet(HttpServletRequest request, HttpServletResponse response) {
126 			this.wrappedResponse = response;
127 		}
128 	}
129 
130 }