diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java index ef178c394b..e315c948a8 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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,14 @@ public class MockHttpServletRequestBuilder * @param vars zero or more URI variables */ MockHttpServletRequestBuilder(HttpMethod httpMethod, String url, Object... vars) { - this(httpMethod.name(), UriComponentsBuilder.fromUriString(url).buildAndExpand(vars).encode().toUri()); + this(httpMethod.name(), initUri(url, vars)); + } + + private static URI initUri(String url, Object[] vars) { + Assert.notNull(url, "'url' must not be null"); + Assert.isTrue(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(); } /** diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java index 17fb123ace..47f51e1046 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -49,6 +49,7 @@ import org.springframework.web.servlet.support.SessionFlashMapManager; import org.springframework.web.util.UriComponentsBuilder; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * Unit tests for building a {@link MockHttpServletRequest} with @@ -88,7 +89,8 @@ public class MockHttpServletRequestBuilderTests { assertThat(request.getServerName()).isEqualTo("java.sun.com"); assertThat(request.getServerPort()).isEqualTo(8080); assertThat(request.getRequestURI()).isEqualTo("/javase/6/docs/api/java/util/BitSet.html"); - assertThat(request.getRequestURL().toString()).isEqualTo("https://java.sun.com:8080/javase/6/docs/api/java/util/BitSet.html"); + assertThat(request.getRequestURL().toString()) + .isEqualTo("https://java.sun.com:8080/javase/6/docs/api/java/util/BitSet.html"); } @Test @@ -107,6 +109,12 @@ public class MockHttpServletRequestBuilderTests { assertThat(request.getRequestURI()).isEqualTo("/test//currentlyValid/0"); } + @Test // gh-24556 + public void requestUriWithoutScheme() { + assertThatIllegalArgumentException().isThrownBy(() -> MockMvcRequestBuilders.get("localhost:8080/path")) + .withMessage("'url' should start with a path or be a complete HTTP URL: localhost:8080/path"); + } + @Test public void contextPathEmpty() { this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/foo");