Empty path mapping behaves consistently
An empty path mapping in an @RequestMapping now consistently matches to empty paths regardless of whether there are both type and method level, annotations, or method-level only. Closes gh-22543
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -28,7 +28,7 @@ public class ForwardController {
|
||||
|
||||
@RequestMapping("/forward")
|
||||
public String forward() {
|
||||
return "forward:/";
|
||||
return "forward:/a";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -28,7 +28,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RestController
|
||||
public class HelloController {
|
||||
|
||||
@RequestMapping
|
||||
@RequestMapping("/a")
|
||||
public String header(HttpServletRequest request) {
|
||||
return "hello";
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -151,7 +151,7 @@ public class MockMvcConnectionBuilderSupportTests {
|
||||
@RestController
|
||||
static class ContextPathController {
|
||||
|
||||
@RequestMapping
|
||||
@RequestMapping("/def")
|
||||
public String contextPath(HttpServletRequest request) {
|
||||
return request.getContextPath();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -157,7 +157,7 @@ public class MockMvcWebClientBuilderTests {
|
||||
@RestController
|
||||
static class ContextPathController {
|
||||
|
||||
@RequestMapping
|
||||
@RequestMapping("/test")
|
||||
public String contextPath(HttpServletRequest request) {
|
||||
return "mvc";
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.test.web.servlet.htmlunit;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
|
||||
import com.gargoylesoftware.htmlunit.Page;
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import org.junit.Test;
|
||||
@@ -44,7 +45,7 @@ public class MockMvcWebConnectionTests {
|
||||
|
||||
@Test
|
||||
public void contextPathNull() throws IOException {
|
||||
this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, this.webClient));
|
||||
this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, this.webClient, null));
|
||||
Page page = this.webClient.getPage("http://localhost/context/a");
|
||||
assertThat(page.getWebResponse().getStatusCode(), equalTo(200));
|
||||
}
|
||||
@@ -59,8 +60,21 @@ public class MockMvcWebConnectionTests {
|
||||
@Test
|
||||
public void contextPathEmpty() throws IOException {
|
||||
this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, this.webClient, ""));
|
||||
Page page = this.webClient.getPage("http://localhost/context/a");
|
||||
assertThat(page.getWebResponse().getStatusCode(), equalTo(200));
|
||||
try {
|
||||
this.webClient.getPage("http://localhost/context/a");
|
||||
fail("Empty context path (root context) should not match to a URL with a context path");
|
||||
}
|
||||
catch (FailingHttpStatusCodeException ex) {
|
||||
assertEquals(404, ex.getStatusCode());
|
||||
}
|
||||
this.webClient.setWebConnection(new MockMvcWebConnection(this.mockMvc, this.webClient));
|
||||
try {
|
||||
this.webClient.getPage("http://localhost/context/a");
|
||||
fail("No context is the same providing an empty context path");
|
||||
}
|
||||
catch (FailingHttpStatusCodeException ex) {
|
||||
assertEquals(404, ex.getStatusCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -145,7 +145,7 @@ public class MockMvcHtmlUnitDriverBuilderTests {
|
||||
@RestController
|
||||
static class ContextPathController {
|
||||
|
||||
@RequestMapping
|
||||
@RequestMapping("/test")
|
||||
public String contextPath(HttpServletRequest request) {
|
||||
return EXPECTED_BODY;
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ public class RedirectTests {
|
||||
return "persons/index";
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@PostMapping("/persons")
|
||||
public String save(@Valid Person person, Errors errors, RedirectAttributes redirectAttrs) {
|
||||
if (errors.hasErrors()) {
|
||||
return "persons/add";
|
||||
|
||||
Reference in New Issue
Block a user