Improve checks on URI string in MockMvc request builder
Closes gh-24556
This commit is contained in:
parent
4882eb278d
commit
a134e92e7f
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in New Issue