|
| 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 | +package org.springframework.web.servlet.resource; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import org.springframework.core.io.ClassPathResource; |
| 28 | +import org.springframework.core.io.Resource; |
| 29 | +import org.springframework.mock.web.test.MockHttpServletRequest; |
| 30 | +import org.springframework.web.servlet.HandlerMapping; |
| 31 | + |
| 32 | +import javax.servlet.http.HttpServletRequest; |
| 33 | + |
| 34 | +/** |
| 35 | + * Unit tests for {@code LinkRewriteTransformer} |
| 36 | + * |
| 37 | + * @author Brian Clozel |
| 38 | + * @author Rossen Stoyanchev |
| 39 | + */ |
| 40 | +public class LinkRewriteTransformerTests { |
| 41 | + |
| 42 | + private ResourceTransformerChain transformerChain; |
| 43 | + |
| 44 | + private TestTransformer transformer; |
| 45 | + |
| 46 | + private MockHttpServletRequest request; |
| 47 | + |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() { |
| 51 | + VersionResourceResolver versionResolver = new VersionResourceResolver(); |
| 52 | + versionResolver.setStrategyMap(Collections.singletonMap("/**", new ContentVersionStrategy())); |
| 53 | + |
| 54 | + List<ResourceResolver> resolvers = new ArrayList<>(); |
| 55 | + resolvers.add(versionResolver); |
| 56 | + resolvers.add(new PathResourceResolver()); |
| 57 | + this.transformerChain = new DefaultResourceTransformerChain(new DefaultResourceResolverChain(resolvers), null); |
| 58 | + |
| 59 | + List<Resource> locations = new ArrayList<>(); |
| 60 | + locations.add(new ClassPathResource("test/", getClass())); |
| 61 | + |
| 62 | + ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler(); |
| 63 | + handler.setLocations(locations); |
| 64 | + handler.setResourceResolvers(resolvers); |
| 65 | + |
| 66 | + ResourceUrlProvider urlProvider = new ResourceUrlProvider(); |
| 67 | + urlProvider.setHandlerMap(Collections.singletonMap("/resources/**", handler)); |
| 68 | + |
| 69 | + this.transformer = new TestTransformer(); |
| 70 | + this.transformer.setResourceUrlProvider(urlProvider); |
| 71 | + |
| 72 | + this.request = new MockHttpServletRequest(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void rewriteAbsolutePath() throws Exception { |
| 77 | + this.request.setRequestURI("/servlet/context/resources/main.css"); |
| 78 | + this.request.setMethod("GET"); |
| 79 | + this.request.setServletPath("/servlet"); |
| 80 | + this.request.setContextPath("/context"); |
| 81 | + this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/resources/main.css"); |
| 82 | + |
| 83 | + String resourcePath = "/servlet/context/resources/bar.css"; |
| 84 | + Resource mainCss = new ClassPathResource("test/main.css", getClass()); |
| 85 | + String actual = this.transformer.resolveUrlPath(resourcePath, this.request, mainCss, this.transformerChain); |
| 86 | + assertEquals("/servlet/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", actual); |
| 87 | + |
| 88 | + actual = this.transformer.resolveUrlPath("bar.css", this.request, mainCss, this.transformerChain); |
| 89 | + assertEquals("bar-11e16cf79faee7ac698c805cf28248d2.css", actual); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void rewriteRelativePath() throws Exception { |
| 94 | + this.request.setRequestURI("/servlet/context/resources/main.css"); |
| 95 | + this.request.setMethod("GET"); |
| 96 | + this.request.setServletPath("/servlet"); |
| 97 | + this.request.setContextPath("/context"); |
| 98 | + this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/resources/main.css"); |
| 99 | + |
| 100 | + Resource mainCss = new ClassPathResource("test/main.css", getClass()); |
| 101 | + String actual = this.transformer.resolveUrlPath("bar.css", this.request, mainCss, this.transformerChain); |
| 102 | + assertEquals("bar-11e16cf79faee7ac698c805cf28248d2.css", actual); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void rewriteRelativePathUpperLevel() throws Exception { |
| 107 | + this.request.setRequestURI("/servlet/context/resources/images/image.png"); |
| 108 | + this.request.setMethod("GET"); |
| 109 | + this.request.setServletPath("/servlet"); |
| 110 | + this.request.setContextPath("/context"); |
| 111 | + this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/resources/images/image.png"); |
| 112 | + |
| 113 | + Resource imagePng = new ClassPathResource("test/images/image.png", getClass()); |
| 114 | + String actual = this.transformer.resolveUrlPath("../bar.css", this.request, imagePng, this.transformerChain); |
| 115 | + assertEquals("../bar-11e16cf79faee7ac698c805cf28248d2.css", actual); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + private static class TestTransformer extends ResourceTransformerSupport { |
| 120 | + |
| 121 | + @Override |
| 122 | + public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain chain) { |
| 123 | + throw new IllegalStateException("Should never be called"); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments