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

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

Introduction

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

Prototype

public TestRestTemplate withBasicAuth(String username, String password) 

Source Link

Document

Creates a new TestRestTemplate with the same configuration as this one, except that it will send basic authorization headers using the given username and password .

Usage

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 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 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);
}