Adapt empty path to "/" in MockMvc

Closes gh-29933
This commit is contained in:
rstoyanchev 2023-02-14 12:01:42 +00:00
parent d7824c7831
commit 18896aceca
2 changed files with 6 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -149,7 +149,8 @@ public class MockHttpServletRequestBuilder
Assert.notNull(url, "'url' must not be null");
Assert.isTrue(url.isEmpty() || url.startsWith("/") || url.startsWith("http://") || url.startsWith("https://"),
() -> "'url' should start with a path or be a complete HTTP URL: " + url);
return UriComponentsBuilder.fromUriString(url).buildAndExpand(vars).encode().toUri();
String uriString = (url.isEmpty() ? "/" : url);
return UriComponentsBuilder.fromUriString(uriString).buildAndExpand(vars).encode().toUri();
}
/**

View File

@ -167,15 +167,15 @@ class MockHttpServletRequestBuilderTests {
assertThat(request.getPathInfo()).isNull();
}
@Test // gh-28823
@Test // gh-28823, gh-29933
void emptyPath() {
this.builder = new MockHttpServletRequestBuilder(GET, "");
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
assertThat(request.getRequestURI()).isEqualTo("");
assertThat(request.getRequestURI()).isEqualTo("/");
assertThat(request.getContextPath()).isEqualTo("");
assertThat(request.getServletPath()).isEqualTo("");
assertThat(request.getPathInfo()).isNull();
assertThat(request.getPathInfo()).isEqualTo("/");
}
@Test // SPR-16453