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:
Dmytro Nosan
2019-03-18 16:00:51 +02:00
committed by Madhura Bhave
parent 988ba3ff78
commit 756bd890eb
2 changed files with 37 additions and 3 deletions

View File

@@ -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";