Skip to content

Commit

Permalink
Provide access to root URI from TestRestTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
tinexw authored and wilkinsona committed Nov 12, 2017
1 parent e92e56d commit c1205c3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
* @author Dave Syer
* @author Phillip Webb
* @author Andy Wilkinson
* @author Kristine Jetzke
* @since 1.4.0
*/
public class TestRestTemplate {
Expand Down Expand Up @@ -165,6 +166,15 @@ public void setUriTemplateHandler(UriTemplateHandler handler) {
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
* @author Phillip Webb
* @author Stephane Nicoll
* @author Andy Wilkinson
* @author Kristine Jetzke
*/
public class TestRestTemplateTests {

Expand All @@ -81,6 +82,31 @@ public void simple() {
.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()
Expand Down

0 comments on commit c1205c3

Please sign in to comment.