1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
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 }