From 95eb24d642374b25ba66b04c34336c50743942a1 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 26 Mar 2019 18:27:11 +0100 Subject: [PATCH] Verify that CssLinkResourceTransformer handles empty url() links This commit introduces tests that verify that both CssLinkResourceTransformer implementations properly handle empty url() functions in CSS files. See gh-22602 --- .../CssLinkResourceTransformerTests.java | 24 ++++++++++++++++++- .../resource/test/empty_url_function.css | 3 +++ .../CssLinkResourceTransformerTests.java | 21 +++++++++++++--- .../resource/test/empty_url_function.css | 3 +++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 spring-webflux/src/test/resources/org/springframework/web/reactive/resource/test/empty_url_function.css create mode 100644 spring-webmvc/src/test/resources/org/springframework/web/servlet/resource/test/empty_url_function.css diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java index 2c755f1791..452d412aff 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,7 +37,9 @@ import static org.springframework.mock.http.server.reactive.test.MockServerHttpR /** * Unit tests for {@link CssLinkResourceTransformer}. + * * @author Rossen Stoyanchev + * @author Sam Brannen */ public class CssLinkResourceTransformerTests { @@ -159,6 +161,26 @@ public class CssLinkResourceTransformerTests { .verify(); } + @Test // https://github.com/spring-projects/spring-framework/issues/22602 + public void transformEmptyUrlFunction() throws Exception { + MockServerWebExchange exchange = MockServerWebExchange.from(get("/static/empty_url_function.css")); + Resource css = getResource("empty_url_function.css"); + String expected = + ".fooStyle {\n" + + "\tbackground: transparent url() no-repeat left top;\n" + + "}"; + + StepVerifier.create(this.transformerChain.transform(exchange, css) + .cast(TransformedResource.class)) + .consumeNextWith(transformedResource -> { + String result = new String(transformedResource.getByteArray(), StandardCharsets.UTF_8); + result = StringUtils.deleteAny(result, "\r"); + assertEquals(expected, result); + }) + .expectComplete() + .verify(); + } + private Resource getResource(String filePath) { return new ClassPathResource("test/" + filePath, getClass()); } diff --git a/spring-webflux/src/test/resources/org/springframework/web/reactive/resource/test/empty_url_function.css b/spring-webflux/src/test/resources/org/springframework/web/reactive/resource/test/empty_url_function.css new file mode 100644 index 0000000000..52a92ab2d4 --- /dev/null +++ b/spring-webflux/src/test/resources/org/springframework/web/reactive/resource/test/empty_url_function.css @@ -0,0 +1,3 @@ +.fooStyle { + background: transparent url() no-repeat left top; +} \ No newline at end of file diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CssLinkResourceTransformerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CssLinkResourceTransformerTests.java index b68ba0a253..8abcfbdc4c 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CssLinkResourceTransformerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CssLinkResourceTransformerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,11 +34,11 @@ import org.springframework.web.servlet.resource.EncodedResourceResolver.EncodedR import static org.junit.Assert.*; /** - * Unit tests for - * {@link org.springframework.web.servlet.resource.CssLinkResourceTransformer}. + * Unit tests for {@link CssLinkResourceTransformer}. * * @author Rossen Stoyanchev * @author Brian Clozel + * @author Sam Brannen * @since 4.1 */ public class CssLinkResourceTransformerTests { @@ -149,6 +149,21 @@ public class CssLinkResourceTransformerTests { assertSame(gzipped, actual); } + @Test // https://github.com/spring-projects/spring-framework/issues/22602 + public void transformEmptyUrlFunction() throws Exception { + this.request = new MockHttpServletRequest("GET", "/static/empty_url_function.css"); + Resource css = getResource("empty_url_function.css"); + String expected = + ".fooStyle {\n" + + "\tbackground: transparent url() no-repeat left top;\n" + + "}"; + + TransformedResource actual = (TransformedResource) this.transformerChain.transform(this.request, css); + String result = new String(actual.getByteArray(), StandardCharsets.UTF_8); + result = StringUtils.deleteAny(result, "\r"); + assertEquals(expected, result); + } + private Resource getResource(String filePath) { return new ClassPathResource("test/" + filePath, getClass()); } diff --git a/spring-webmvc/src/test/resources/org/springframework/web/servlet/resource/test/empty_url_function.css b/spring-webmvc/src/test/resources/org/springframework/web/servlet/resource/test/empty_url_function.css new file mode 100644 index 0000000000..52a92ab2d4 --- /dev/null +++ b/spring-webmvc/src/test/resources/org/springframework/web/servlet/resource/test/empty_url_function.css @@ -0,0 +1,3 @@ +.fooStyle { + background: transparent url() no-repeat left top; +} \ No newline at end of file