Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -26,6 +26,7 @@
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;
Expand All @@ -38,6 +39,9 @@
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;
Expand Down Expand Up @@ -1023,21 +1027,37 @@ public RestTemplate getRestTemplate() {
/**
* 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
* @since 1.4.1
*/
public TestRestTemplate withBasicAuth(String username, String password) {
RestTemplate restTemplate = new RestTemplateBuilder()
.requestFactory(getRequestFactorySupplier())
.messageConverters(getRestTemplate().getMessageConverters())
.interceptors(getRestTemplate().getInterceptors())
.uriTemplateHandler(getRestTemplate().getUriTemplateHandler()).build();
return new TestRestTemplate(restTemplate, username, password,
this.httpClientOptions);
}

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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -93,6 +93,20 @@ public void doNotReplaceCustomRequestFactory() {
.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";
Expand Down