Skip to content

Commit bf63309

Browse files
committed
Allow empty path in MockMvc
Closes gh-28823
1 parent 963fd75 commit bf63309

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -148,7 +148,7 @@ public class MockHttpServletRequestBuilder
148148

149149
private static URI initUri(String url, Object[] vars) {
150150
Assert.notNull(url, "'url' must not be null");
151-
Assert.isTrue(url.startsWith("/") || url.startsWith("http://") || url.startsWith("https://"), "" +
151+
Assert.isTrue(url.isEmpty() || url.startsWith("/") || url.startsWith("http://") || url.startsWith("https://"),
152152
"'url' should start with a path or be a complete HTTP URL: " + url);
153153
return UriComponentsBuilder.fromUriString(url).buildAndExpand(vars).encode().toUri();
154154
}

spring-test/src/test/java/org/springframework/test/web/reactive/server/DefaultControllerSpecTests.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -50,6 +50,15 @@ public void controller() {
5050
.expectBody(String.class).isEqualTo("Success");
5151
}
5252

53+
@Test
54+
public void controllerEmptyPath() {
55+
new DefaultControllerSpec(new MyController()).build()
56+
.get().uri("")
57+
.exchange()
58+
.expectStatus().isOk()
59+
.expectBody(String.class).isEqualTo("Success empty path");
60+
}
61+
5362
@Test
5463
public void controllerAdvice() {
5564
new DefaultControllerSpec(new MyController())
@@ -116,10 +125,15 @@ public void uriTemplate() {
116125
private static class MyController {
117126

118127
@GetMapping("/")
119-
public String handle() {
128+
public String handleRootPath() {
120129
return "Success";
121130
}
122131

132+
@GetMapping
133+
public String handleEmptyPath() {
134+
return "Success empty path";
135+
}
136+
123137
@GetMapping("/exception")
124138
public void handleWithError() {
125139
throw new IllegalStateException();

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -165,6 +165,17 @@ void contextPathServletPathInfo() {
165165
assertThat(request.getPathInfo()).isNull();
166166
}
167167

168+
@Test // gh-28823
169+
void emptyPath() {
170+
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "");
171+
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
172+
173+
assertThat(request.getRequestURI()).isEqualTo("");
174+
assertThat(request.getContextPath()).isEqualTo("");
175+
assertThat(request.getServletPath()).isEqualTo("");
176+
assertThat(request.getPathInfo()).isNull();
177+
}
178+
168179
@Test // SPR-16453
169180
void pathInfoIsDecoded() {
170181
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels 42");

0 commit comments

Comments
 (0)