ResourceUrlProviderJavaConfigTests.java 4.3 KB
Newer Older
1
/*
2
 * Copyright 2002-2014 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * 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.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;
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;
import org.springframework.web.servlet.config.annotation.*;

import static org.junit.Assert.*;


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

44
	private final TestServlet servlet = new TestServlet();
45

46
	private MockFilterChain filterChain;
47 48 49

	private MockHttpServletRequest request;

R
Rossen Stoyanchev 已提交
50 51
	private MockHttpServletResponse response;

52 53

	@Before
54
	@SuppressWarnings("resource")
55 56 57
	public void setup() throws Exception {
		this.filterChain = new MockFilterChain(this.servlet, new ResourceUrlEncodingFilter());

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 67 68
		this.request.setContextPath("/myapp");
		this.response = new MockHttpServletResponse();

		Object urlProvider = context.getBean(ResourceUrlProvider.class);
		this.request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);
69 70 71
	}

	@Test
72
	public void resolvePathWithServletMappedAsRoot() throws Exception {
R
Rossen Stoyanchev 已提交
73 74 75
		this.request.setRequestURI("/myapp/index");
		this.request.setServletPath("/index");
		this.filterChain.doFilter(this.request, this.response);
76

R
Rossen Stoyanchev 已提交
77 78
		assertEquals("/myapp/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css",
				resolvePublicResourceUrlPath("/myapp/resources/foo.css"));
79 80 81
	}

	@Test
82
	public void resolvePathWithServletMappedByPrefix() throws Exception {
R
Rossen Stoyanchev 已提交
83
		this.request.setRequestURI("/myapp/myservlet/index");
84
		this.request.setServletPath("/myservlet");
R
Rossen Stoyanchev 已提交
85
		this.filterChain.doFilter(this.request, this.response);
86

R
Rossen Stoyanchev 已提交
87 88
		assertEquals("/myapp/myservlet/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css",
				resolvePublicResourceUrlPath("/myapp/myservlet/resources/foo.css"));
89 90
	}

91 92 93 94 95 96 97 98 99 100
	@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 已提交
101 102
	private String resolvePublicResourceUrlPath(String path) {
		return this.servlet.wrappedResponse.encodeURL(path);
103 104 105 106 107 108 109 110 111 112
	}


	@Configuration
	static class WebConfig extends WebMvcConfigurationSupport {

		@Override
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
			registry.addResourceHandler("/resources/**")
				.addResourceLocations("classpath:org/springframework/web/servlet/resource/test/")
113
				.resourceChain(true).addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
114 115 116 117 118 119
		}
	}

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

R
Rossen Stoyanchev 已提交
120
		private HttpServletResponse wrappedResponse;
121 122 123

		@Override
		protected void doGet(HttpServletRequest request, HttpServletResponse response) {
R
Rossen Stoyanchev 已提交
124
			this.wrappedResponse = response;
125 126 127 128
		}
	}

}