Fix request factory used with TestRestTemplate withBasicAuth
This commit updates the behavior of withBasicAuth on TestRestTemplate by trying to use the same request factory type as the underlying restTemplate. If creation of a new instance of the configured request factory class fails, it falls back to the `ClientHttpRequestFactorySupplier`. See gh-15982
This commit is contained in:
committed by
Madhura Bhave
parent
988ba3ff78
commit
756bd890eb
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -26,6 +26,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.config.CookieSpecs;
|
||||
@@ -38,6 +39,9 @@ import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.ssl.SSLContextBuilder;
|
||||
|
||||
import org.springframework.beans.BeanInstantiationException;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.boot.web.client.ClientHttpRequestFactorySupplier;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.boot.web.client.RootUriTemplateHandler;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
@@ -1023,7 +1027,10 @@ public class TestRestTemplate {
|
||||
/**
|
||||
* 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}.
|
||||
* {@code username} and {@code password}. Note, that a new instance of
|
||||
* {@link ClientHttpRequestFactory} will be created (if possible) based on the current
|
||||
* factory class, otherwise {@link ClientHttpRequestFactorySupplier} will be used to
|
||||
* instantiate a {@link ClientHttpRequestFactory}.
|
||||
* @param username the username
|
||||
* @param password the password
|
||||
* @return the new template
|
||||
@@ -1031,6 +1038,7 @@ public class TestRestTemplate {
|
||||
*/
|
||||
public TestRestTemplate withBasicAuth(String username, String password) {
|
||||
RestTemplate restTemplate = new RestTemplateBuilder()
|
||||
.requestFactory(getRequestFactorySupplier())
|
||||
.messageConverters(getRestTemplate().getMessageConverters())
|
||||
.interceptors(getRestTemplate().getInterceptors())
|
||||
.uriTemplateHandler(getRestTemplate().getUriTemplateHandler()).build();
|
||||
@@ -1041,6 +1049,18 @@ public class TestRestTemplate {
|
||||
return testRestTemplate;
|
||||
}
|
||||
|
||||
private Supplier<ClientHttpRequestFactory> getRequestFactorySupplier() {
|
||||
return () -> {
|
||||
try {
|
||||
return BeanUtils
|
||||
.instantiateClass(getRequestFactoryClass(getRestTemplate()));
|
||||
}
|
||||
catch (BeanInstantiationException ex) {
|
||||
return new ClientHttpRequestFactorySupplier().get();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private RequestEntity<?> createRequestEntityWithRootAppliedUri(
|
||||
RequestEntity<?> requestEntity) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -93,6 +93,20 @@ public class TestRestTemplateTests {
|
||||
.isInstanceOf(OkHttp3ClientHttpRequestFactory.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useTheSameRequestFactoryClassWithBasicAuth() {
|
||||
OkHttp3ClientHttpRequestFactory customFactory = new OkHttp3ClientHttpRequestFactory();
|
||||
RestTemplateBuilder builder = new RestTemplateBuilder()
|
||||
.requestFactory(OkHttp3ClientHttpRequestFactory::new);
|
||||
TestRestTemplate testRestTemplate = new TestRestTemplate(builder)
|
||||
.withBasicAuth("test", "test");
|
||||
RestTemplate restTemplate = testRestTemplate.getRestTemplate();
|
||||
Object requestFactory = ReflectionTestUtils
|
||||
.getField(restTemplate.getRequestFactory(), "requestFactory");
|
||||
assertThat(requestFactory).isNotEqualTo(customFactory)
|
||||
.hasSameClassAs(customFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRootUriRootUriSetViaRestTemplateBuilder() {
|
||||
String rootUri = "http://example.com";
|
||||
|
||||
Reference in New Issue
Block a user