ResourceUrlProviderJavaConfigTests.java 4.5 KB
Newer Older
1
/*
2
 * Copyright 2002-2018 the original author or authors.
3 4 5 6 7
 *
 * 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
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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.web.servlet.resource;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Before;
import org.junit.Test;
25

26 27 28 29 30 31
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.test.MockFilterChain;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.mock.web.test.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
32 33
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
34 35 36 37 38 39

import static org.junit.Assert.*;


/**
 * Integration tests using {@link ResourceUrlEncodingFilter} and
40
 * {@link ResourceUrlProvider} with the latter configured in Spring MVC Java config.
41 42 43
 *
 * @author Rossen Stoyanchev
 */
44
public class ResourceUrlProviderJavaConfigTests {
45

46
	private final TestServlet servlet = new TestServlet();
47

48
	private MockFilterChain filterChain;
49 50 51

	private MockHttpServletRequest request;

R
Rossen Stoyanchev 已提交
52 53
	private MockHttpServletResponse response;

54 55

	@Before
56
	@SuppressWarnings("resource")
57
	public void setup() throws Exception {
R
Rossen Stoyanchev 已提交
58 59 60 61
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.setServletContext(new MockServletContext());
		context.register(WebConfig.class);
		context.refresh();
62 63

		this.request = new MockHttpServletRequest("GET", "/");
R
Rossen Stoyanchev 已提交
64 65 66
		this.request.setContextPath("/myapp");
		this.response = new MockHttpServletResponse();

67 68 69 70 71 72 73
		this.filterChain = new MockFilterChain(this.servlet,
				new ResourceUrlEncodingFilter(),
				(request, response, chain) -> {
					Object urlProvider = context.getBean(ResourceUrlProvider.class);
					request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);
					chain.doFilter(request, response);
				});
74 75 76
	}

	@Test
77
	public void resolvePathWithServletMappedAsRoot() throws Exception {
R
Rossen Stoyanchev 已提交
78 79 80
		this.request.setRequestURI("/myapp/index");
		this.request.setServletPath("/index");
		this.filterChain.doFilter(this.request, this.response);
81

R
Rossen Stoyanchev 已提交
82 83
		assertEquals("/myapp/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css",
				resolvePublicResourceUrlPath("/myapp/resources/foo.css"));
84 85 86
	}

	@Test
87
	public void resolvePathWithServletMappedByPrefix() throws Exception {
R
Rossen Stoyanchev 已提交
88
		this.request.setRequestURI("/myapp/myservlet/index");
89
		this.request.setServletPath("/myservlet");
R
Rossen Stoyanchev 已提交
90
		this.filterChain.doFilter(this.request, this.response);
91

R
Rossen Stoyanchev 已提交
92 93
		assertEquals("/myapp/myservlet/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css",
				resolvePublicResourceUrlPath("/myapp/myservlet/resources/foo.css"));
94 95
	}

96 97 98 99 100 101 102 103 104 105
	@Test
	public void resolvePathNoMatch() throws Exception {
		this.request.setRequestURI("/myapp/myservlet/index");
		this.request.setServletPath("/myservlet");
		this.filterChain.doFilter(this.request, this.response);

		assertEquals("/myapp/myservlet/index", resolvePublicResourceUrlPath("/myapp/myservlet/index"));
	}


R
Rossen Stoyanchev 已提交
106 107
	private String resolvePublicResourceUrlPath(String path) {
		return this.servlet.wrappedResponse.encodeURL(path);
108 109 110 111 112 113 114 115 116 117
	}


	@Configuration
	static class WebConfig extends WebMvcConfigurationSupport {

		@Override
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
			registry.addResourceHandler("/resources/**")
				.addResourceLocations("classpath:org/springframework/web/servlet/resource/test/")
118
				.resourceChain(true).addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
119 120 121 122 123 124
		}
	}

	@SuppressWarnings("serial")
	private static class TestServlet extends HttpServlet {

R
Rossen Stoyanchev 已提交
125
		private HttpServletResponse wrappedResponse;
126 127 128

		@Override
		protected void doGet(HttpServletRequest request, HttpServletResponse response) {
R
Rossen Stoyanchev 已提交
129
			this.wrappedResponse = response;
130 131 132 133
		}
	}

}