Browse Source

Adapt empty path to "/" in MockMvc

Closes gh-29933
pull/29969/head
rstoyanchev 3 years ago
parent
commit
18896aceca
  1. 5
      spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java
  2. 6
      spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java

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

@ -1,5 +1,5 @@ @@ -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 @@ -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();
}
/**

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

@ -167,15 +167,15 @@ class MockHttpServletRequestBuilderTests { @@ -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

Loading…
Cancel
Save