Browse Source

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
pull/22823/head
Sam Brannen 7 years ago
parent
commit
bcdc6e0581
  1. 28
      spring-webflux/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java
  2. 3
      spring-webflux/src/test/resources/org/springframework/web/reactive/resource/test/empty_url_function.css
  3. 25
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CssLinkResourceTransformerTests.java
  4. 3
      spring-webmvc/src/test/resources/org/springframework/web/servlet/resource/test/empty_url_function.css

28
spring-webflux/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,7 +41,9 @@ import static org.junit.Assert.assertSame;
/** /**
* Unit tests for {@link CssLinkResourceTransformer}. * Unit tests for {@link CssLinkResourceTransformer}.
*
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen
*/ */
public class CssLinkResourceTransformerTests { public class CssLinkResourceTransformerTests {
@ -160,4 +162,28 @@ public class CssLinkResourceTransformerTests {
copy.toFile().deleteOnExit(); copy.toFile().deleteOnExit();
} }
@Test // https://github.com/spring-projects/spring-framework/issues/22602
public void transformEmptyUrlFunction() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.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());
}
} }

3
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;
}

25
spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CssLinkResourceTransformerTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -37,11 +37,11 @@ import org.springframework.util.StringUtils;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
* Unit tests for * Unit tests for {@link CssLinkResourceTransformer}.
* {@link org.springframework.web.servlet.resource.CssLinkResourceTransformer}.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Brian Clozel * @author Brian Clozel
* @author Sam Brannen
* @since 4.1 * @since 4.1
*/ */
public class CssLinkResourceTransformerTests { public class CssLinkResourceTransformerTests {
@ -158,4 +158,23 @@ public class CssLinkResourceTransformerTests {
copy.toFile().deleteOnExit(); copy.toFile().deleteOnExit();
} }
@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());
}
} }

3
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;
}
Loading…
Cancel
Save