Skip to content

Commit 18896ac

Browse files
committed
Adapt empty path to "/" in MockMvc
Closes gh-29933
1 parent d7824c7 commit 18896ac

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -149,7 +149,8 @@ private static URI initUri(String url, Object[] vars) {
149149
Assert.notNull(url, "'url' must not be null");
150150
Assert.isTrue(url.isEmpty() || url.startsWith("/") || url.startsWith("http://") || url.startsWith("https://"),
151151
() -> "'url' should start with a path or be a complete HTTP URL: " + url);
152-
return UriComponentsBuilder.fromUriString(url).buildAndExpand(vars).encode().toUri();
152+
String uriString = (url.isEmpty() ? "/" : url);
153+
return UriComponentsBuilder.fromUriString(uriString).buildAndExpand(vars).encode().toUri();
153154
}
154155

155156
/**

spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ void contextPathServletPathInfo() {
167167
assertThat(request.getPathInfo()).isNull();
168168
}
169169

170-
@Test // gh-28823
170+
@Test // gh-28823, gh-29933
171171
void emptyPath() {
172172
this.builder = new MockHttpServletRequestBuilder(GET, "");
173173
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
174174

175-
assertThat(request.getRequestURI()).isEqualTo("");
175+
assertThat(request.getRequestURI()).isEqualTo("/");
176176
assertThat(request.getContextPath()).isEqualTo("");
177177
assertThat(request.getServletPath()).isEqualTo("");
178-
assertThat(request.getPathInfo()).isNull();
178+
assertThat(request.getPathInfo()).isEqualTo("/");
179179
}
180180

181181
@Test // SPR-16453

0 commit comments

Comments
 (0)