Ensure that custom static resource locations end with /
Closes gh-9360
This commit is contained in:
parent
5be5b13775
commit
ad629055fa
|
|
@ -86,7 +86,16 @@ public class ResourceProperties implements ResourceLoaderAware {
|
|||
}
|
||||
|
||||
public void setStaticLocations(String[] staticLocations) {
|
||||
this.staticLocations = staticLocations;
|
||||
this.staticLocations = appendSlashIfNecessary(staticLocations);
|
||||
}
|
||||
|
||||
private String[] appendSlashIfNecessary(String[] staticLocations) {
|
||||
String[] normalized = new String[staticLocations.length];
|
||||
for (int i = 0; i < staticLocations.length; i++) {
|
||||
String location = staticLocations[i];
|
||||
normalized[i] = location.endsWith("/") ? location : location + "/";
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
public Resource getWelcomePage() {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
|
|
@ -18,7 +18,10 @@ package org.springframework.boot.autoconfigure.web;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.testutil.Matched;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.endsWith;
|
||||
|
||||
/**
|
||||
* Tests for {@link ResourceProperties}.
|
||||
|
|
@ -52,4 +55,17 @@ public class ResourcePropertiesTests {
|
|||
assertThat(this.properties.getChain().getEnabled()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultStaticLocationsAllEndWithTrailingSlash() {
|
||||
assertThat(this.properties.getStaticLocations()).are(Matched.by(endsWith("/")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customStaticLocationsAreNormalizedToEndWithTrailingSlash() {
|
||||
this.properties.setStaticLocations(new String[] { "/foo", "/bar", "/baz/" });
|
||||
assertThat(this.properties.getStaticLocations()).containsExactly("/foo/", "/bar/",
|
||||
"/baz/");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue