mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Provide access to root URI from TestRestTemplate
Closes gh-10641
This commit is contained in:
parent
e92e56dda5
commit
c1205c3243
@ -78,6 +78,7 @@ import org.springframework.web.util.UriTemplateHandler;
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @author Kristine Jetzke
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public class TestRestTemplate {
|
||||
@ -165,6 +166,15 @@ public class TestRestTemplate {
|
||||
this.restTemplate.setUriTemplateHandler(handler);
|
||||
}
|
||||
|
||||
public String getRootUri() {
|
||||
UriTemplateHandler uriTemplateHandler = this.restTemplate.getUriTemplateHandler();
|
||||
if (RootUriTemplateHandler.class
|
||||
.isAssignableFrom(uriTemplateHandler.getClass())) {
|
||||
return ((RootUriTemplateHandler) uriTemplateHandler).getRootUri();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a representation by doing a GET on the specified URL. The response (if
|
||||
* any) is converted and returned.
|
||||
|
@ -63,6 +63,7 @@ import static org.mockito.Mockito.verify;
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
* @author Andy Wilkinson
|
||||
* @author Kristine Jetzke
|
||||
*/
|
||||
public class TestRestTemplateTests {
|
||||
|
||||
@ -81,6 +82,31 @@ public class TestRestTemplateTests {
|
||||
.isInstanceOf(HttpComponentsClientHttpRequestFactory.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRootUriRootUriSetViaRestTemplateBuilder() {
|
||||
String rootUri = "http://example.com";
|
||||
RestTemplate delegate = new RestTemplateBuilder().rootUri(rootUri).build();
|
||||
|
||||
assertThat(new TestRestTemplate(delegate).getRootUri()).isEqualTo(rootUri);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRootUriRootUriSetViaLocalHostUriTemplateHandler() {
|
||||
String rootUri = "http://example.com";
|
||||
TestRestTemplate template = new TestRestTemplate();
|
||||
LocalHostUriTemplateHandler templateHandler = mock(
|
||||
LocalHostUriTemplateHandler.class);
|
||||
given(templateHandler.getRootUri()).willReturn(rootUri);
|
||||
template.setUriTemplateHandler(templateHandler);
|
||||
|
||||
assertThat(template.getRootUri()).isEqualTo(rootUri);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRootUriRootUriNotSet() {
|
||||
assertThat(new TestRestTemplate().getRootUri()).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticated() {
|
||||
assertThat(new TestRestTemplate("user", "password").getRestTemplate()
|
||||
|
Loading…
Reference in New Issue
Block a user