Polish "Fix request factory used with withBasicAuth"
Fixes gh-15982
This commit is contained in:
parent
756bd890eb
commit
7ea8770524
|
|
@ -1027,10 +1027,8 @@ public class TestRestTemplate {
|
||||||
/**
|
/**
|
||||||
* Creates a new {@code TestRestTemplate} with the same configuration as this one,
|
* Creates a new {@code TestRestTemplate} with the same configuration as this one,
|
||||||
* except that it will send basic authorization headers using the given
|
* except that it will send basic authorization headers using the given
|
||||||
* {@code username} and {@code password}. Note, that a new instance of
|
* {@code username} and {@code password}. The request factory used is a new instance
|
||||||
* {@link ClientHttpRequestFactory} will be created (if possible) based on the current
|
* of the underlying {@link RestTemplate}'s request factory type (when possible).
|
||||||
* factory class, otherwise {@link ClientHttpRequestFactorySupplier} will be used to
|
|
||||||
* instantiate a {@link ClientHttpRequestFactory}.
|
|
||||||
* @param username the username
|
* @param username the username
|
||||||
* @param password the password
|
* @param password the password
|
||||||
* @return the new template
|
* @return the new template
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ import org.springframework.http.HttpEntity;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.RequestEntity;
|
import org.springframework.http.RequestEntity;
|
||||||
|
import org.springframework.http.client.ClientHttpRequest;
|
||||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
|
|
@ -97,7 +98,7 @@ public class TestRestTemplateTests {
|
||||||
public void useTheSameRequestFactoryClassWithBasicAuth() {
|
public void useTheSameRequestFactoryClassWithBasicAuth() {
|
||||||
OkHttp3ClientHttpRequestFactory customFactory = new OkHttp3ClientHttpRequestFactory();
|
OkHttp3ClientHttpRequestFactory customFactory = new OkHttp3ClientHttpRequestFactory();
|
||||||
RestTemplateBuilder builder = new RestTemplateBuilder()
|
RestTemplateBuilder builder = new RestTemplateBuilder()
|
||||||
.requestFactory(OkHttp3ClientHttpRequestFactory::new);
|
.requestFactory(() -> customFactory);
|
||||||
TestRestTemplate testRestTemplate = new TestRestTemplate(builder)
|
TestRestTemplate testRestTemplate = new TestRestTemplate(builder)
|
||||||
.withBasicAuth("test", "test");
|
.withBasicAuth("test", "test");
|
||||||
RestTemplate restTemplate = testRestTemplate.getRestTemplate();
|
RestTemplate restTemplate = testRestTemplate.getRestTemplate();
|
||||||
|
|
@ -107,6 +108,21 @@ public class TestRestTemplateTests {
|
||||||
.hasSameClassAs(customFactory);
|
.hasSameClassAs(customFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void withBasicAuthWhenRequestFactoryTypeCannotBeInstantiatedShouldFallback() {
|
||||||
|
TestClientHttpRequestFactory customFactory = new TestClientHttpRequestFactory(
|
||||||
|
"my-request-factory");
|
||||||
|
RestTemplateBuilder builder = new RestTemplateBuilder()
|
||||||
|
.requestFactory(() -> customFactory);
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate(builder)
|
||||||
|
.withBasicAuth("test", "test");
|
||||||
|
RestTemplate restTemplate = testRestTemplate.getRestTemplate();
|
||||||
|
Object requestFactory = ReflectionTestUtils
|
||||||
|
.getField(restTemplate.getRequestFactory(), "requestFactory");
|
||||||
|
assertThat(requestFactory).isNotEqualTo(customFactory)
|
||||||
|
.isInstanceOf(CustomHttpComponentsClientHttpRequestFactory.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getRootUriRootUriSetViaRestTemplateBuilder() {
|
public void getRootUriRootUriSetViaRestTemplateBuilder() {
|
||||||
String rootUri = "http://example.com";
|
String rootUri = "http://example.com";
|
||||||
|
|
@ -397,4 +413,17 @@ public class TestRestTemplateTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class TestClientHttpRequestFactory implements ClientHttpRequestFactory {
|
||||||
|
|
||||||
|
TestClientHttpRequestFactory(String value) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod)
|
||||||
|
throws IOException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue