Example usage for org.springframework.boot.test.web.client TestRestTemplate getRestTemplate

List of usage examples for org.springframework.boot.test.web.client TestRestTemplate getRestTemplate

Introduction

In this page you can find the example usage for org.springframework.boot.test.web.client TestRestTemplate getRestTemplate.

Prototype

public RestTemplate getRestTemplate() 

Source Link

Document

Returns the underlying RestTemplate that is actually used to perform the REST operations.

Usage

From source file:io.syndesis.runtime.BaseITCase.java

@Autowired
public void setRestTemplate(TestRestTemplate testRestTemplate) {
    List<HttpMessageConverter<?>> messageConverters = testRestTemplate.getRestTemplate().getMessageConverters();
    messageConverters.add(0, new YamlJackson2HttpMessageConverter());
    messageConverters.add(0, new JsonJackson2HttpMessageConverter());
    this.restTemplate = testRestTemplate;
}

From source file:org.springframework.boot.test.web.client.TestRestTemplateTests.java

@Test
public void withBasicAuthReplacesBasicAuthInterceptorWhenAlreadyPresent() {
    TestRestTemplate original = new TestRestTemplate("foo", "bar").withBasicAuth("replace", "replace");
    TestRestTemplate basicAuth = original.withBasicAuth("user", "password");
    assertThat(basicAuth.getRestTemplate().getMessageConverters())
            .containsExactlyElementsOf(original.getRestTemplate().getMessageConverters());
    assertThat(basicAuth.getRestTemplate().getRequestFactory())
            .isInstanceOf(InterceptingClientHttpRequestFactory.class);
    assertThat(ReflectionTestUtils.getField(basicAuth.getRestTemplate().getRequestFactory(), "requestFactory"))
            .isInstanceOf(CustomHttpComponentsClientHttpRequestFactory.class);
    assertThat(basicAuth.getRestTemplate().getUriTemplateHandler())
            .isSameAs(original.getRestTemplate().getUriTemplateHandler());
    assertThat(basicAuth.getRestTemplate().getInterceptors()).hasSize(1);
    assertBasicAuthorizationInterceptorCredentials(basicAuth, "user", "password");
}

From source file:org.springframework.boot.test.web.client.TestRestTemplateTests.java

@Test
public void withBasicAuthAddsBasicAuthInterceptorWhenNotAlreadyPresent() {
    TestRestTemplate originalTemplate = new TestRestTemplate();
    TestRestTemplate basicAuthTemplate = originalTemplate.withBasicAuth("user", "password");
    assertThat(basicAuthTemplate.getRestTemplate().getMessageConverters())
            .containsExactlyElementsOf(originalTemplate.getRestTemplate().getMessageConverters());
    assertThat(basicAuthTemplate.getRestTemplate().getRequestFactory())
            .isInstanceOf(InterceptingClientHttpRequestFactory.class);
    assertThat(ReflectionTestUtils.getField(basicAuthTemplate.getRestTemplate().getRequestFactory(),
            "requestFactory")).isInstanceOf(CustomHttpComponentsClientHttpRequestFactory.class);
    assertThat(basicAuthTemplate.getRestTemplate().getUriTemplateHandler())
            .isSameAs(originalTemplate.getRestTemplate().getUriTemplateHandler());
    assertThat(basicAuthTemplate.getRestTemplate().getInterceptors()).hasSize(1);
    assertBasicAuthorizationInterceptorCredentials(basicAuthTemplate, "user", "password");
}

From source file:org.springframework.boot.test.web.client.TestRestTemplateTests.java

@Test
public void withBasicAuthDoesNotResetErrorHandler() throws Exception {
    TestRestTemplate originalTemplate = new TestRestTemplate("foo", "bar");
    ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class);
    originalTemplate.getRestTemplate().setErrorHandler(errorHandler);
    TestRestTemplate basicAuthTemplate = originalTemplate.withBasicAuth("user", "password");
    assertThat(basicAuthTemplate.getRestTemplate().getErrorHandler()).isSameAs(errorHandler);
}

From source file:org.springframework.boot.test.web.client.TestRestTemplateTests.java

@Test
public void options() throws Exception {
    TestRestTemplate template = new TestRestTemplate(HttpClientOption.ENABLE_REDIRECTS);
    CustomHttpComponentsClientHttpRequestFactory factory = (CustomHttpComponentsClientHttpRequestFactory) template
            .getRestTemplate().getRequestFactory();
    RequestConfig config = factory.getRequestConfig();
    assertThat(config.isRedirectsEnabled()).isTrue();
}

From source file:org.springframework.boot.test.web.client.TestRestTemplateTests.java

private void assertBasicAuthorizationInterceptorCredentials(TestRestTemplate testRestTemplate, String username,
        String password) {/*from   w w  w .  j a v a 2s  .  c  o  m*/
    @SuppressWarnings("unchecked")
    List<ClientHttpRequestInterceptor> requestFactoryInterceptors = (List<ClientHttpRequestInterceptor>) ReflectionTestUtils
            .getField(testRestTemplate.getRestTemplate().getRequestFactory(), "interceptors");
    assertThat(requestFactoryInterceptors).hasSize(1);
    ClientHttpRequestInterceptor interceptor = requestFactoryInterceptors.get(0);
    assertThat(interceptor).isInstanceOf(BasicAuthorizationInterceptor.class);
    assertThat(ReflectionTestUtils.getField(interceptor, "username")).isEqualTo(username);
    assertThat(ReflectionTestUtils.getField(interceptor, "password")).isEqualTo(password);

}

From source file:org.springframework.boot.test.web.client.TestRestTemplate.java

/**
 * Creates a new {@code TestRestTemplate} with the same configuration as this one,
 * except that it will send basic authorization headers using the given
 * {@code username} and {@code password}.
 * @param username the username/*from   w ww  .  ja  v a2 s  .  c o  m*/
 * @param password the password
 * @return the new template
 * @since 1.4.1
 */
public TestRestTemplate withBasicAuth(String username, String password) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setMessageConverters(getRestTemplate().getMessageConverters());
    restTemplate.setInterceptors(getRestTemplate().getInterceptors());
    restTemplate.setRequestFactory(getRestTemplate().getRequestFactory());
    restTemplate.setUriTemplateHandler(getRestTemplate().getUriTemplateHandler());
    TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, username, password,
            this.httpClientOptions);
    testRestTemplate.getRestTemplate().setErrorHandler(getRestTemplate().getErrorHandler());
    return testRestTemplate;
}

From source file:org.springframework.cloud.netflix.zuul.filters.route.support.ZuulProxyTestBase.java

@SuppressWarnings("deprecation")
@Test/*from  www .  j  a  va2  s .c  om*/
public void javascriptEncodedFormParams() {
    TestRestTemplate testRestTemplate = new TestRestTemplate();
    ArrayList<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters
            .addAll(Arrays.asList(new StringHttpMessageConverter(), new NoEncodingFormHttpMessageConverter()));
    testRestTemplate.getRestTemplate().setMessageConverters(converters);

    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
    map.add("foo", "(bar)");
    ResponseEntity<String> result = testRestTemplate
            .postForEntity("http://localhost:" + this.port + "/simple/local", map, String.class);
    assertEquals(HttpStatus.OK, result.getStatusCode());
    assertEquals("Posted [(bar)] and Content-Length was: 13!", result.getBody());
}