Browse Source

Improve checks on URI string in MockMvc request builder

Closes gh-24556
pull/24572/head
Rossen Stoyanchev 5 years ago
parent
commit
a134e92e7f
  1. 11
      spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java
  2. 12
      spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java

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

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

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

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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 { @@ -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");

Loading…
Cancel
Save