File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed
main/java/org/springframework/web/servlet/resource
test/java/org/springframework/web/servlet/resource Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 3434import org .springframework .lang .Nullable ;
3535import org .springframework .util .AntPathMatcher ;
3636import org .springframework .util .PathMatcher ;
37+ import org .springframework .util .StringUtils ;
3738import org .springframework .web .servlet .handler .SimpleUrlHandlerMapping ;
3839import org .springframework .web .util .UrlPathHelper ;
3940
@@ -217,6 +218,14 @@ private int getEndPathIndex(String lookupPath) {
217218 */
218219 @ Nullable
219220 public final String getForLookupPath (String lookupPath ) {
221+
222+ // Clean duplicate slashes or pathWithinPattern won't match lookupPath
223+ String previous ;
224+ do {
225+ previous = lookupPath ;
226+ lookupPath = StringUtils .replace (lookupPath , "//" , "/" );
227+ } while (!lookupPath .equals (previous ));
228+
220229 if (logger .isTraceEnabled ()) {
221230 logger .trace ("Getting resource URL for lookup path \" " + lookupPath + "\" " );
222231 }
Original file line number Diff line number Diff line change 3535import org .springframework .web .servlet .handler .SimpleUrlHandlerMapping ;
3636
3737import static org .junit .Assert .*;
38+ import static org .mockito .ArgumentMatchers .any ;
39+ import static org .mockito .Mockito .mock ;
40+ import static org .mockito .Mockito .when ;
3841
3942
4043/**
@@ -136,6 +139,25 @@ public void initializeOnce() throws Exception {
136139 assertFalse (urlProviderBean .isAutodetect ());
137140 }
138141
142+ @ Test // SPR-16296
143+ public void getForLookupPathShouldNotFailIfPathContainsDoubleSlashes () {
144+ // given
145+ ResourceResolver mockResourceResolver = mock (ResourceResolver .class );
146+ when (mockResourceResolver .resolveUrlPath (any (), any (), any ())).thenReturn ("some-path" );
147+
148+ ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler ();
149+ handler .getResourceResolvers ().add (mockResourceResolver );
150+
151+ ResourceUrlProvider provider = new ResourceUrlProvider ();
152+ provider .getHandlerMap ().put ("/some-pattern/**" , handler );
153+
154+ // when
155+ String lookupForPath = provider .getForLookupPath ("/some-pattern/some-lib//some-resource" );
156+
157+ // then
158+ assertEquals ("/some-pattern/some-path" , lookupForPath );
159+ }
160+
139161
140162 @ Configuration
141163 @ SuppressWarnings ({"unused" , "WeakerAccess" })
You can’t perform that action at this time.
0 commit comments