Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
0611eb49
Commit
0611eb49
authored
Mar 19, 2019
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.1.x'
parents
da03d53a
85a476fa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
3 deletions
+64
-3
TestRestTemplate.java
...pringframework/boot/test/web/client/TestRestTemplate.java
+20
-2
TestRestTemplateTests.java
...framework/boot/test/web/client/TestRestTemplateTests.java
+44
-1
No files found.
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java
View file @
0611eb49
/*
/*
* Copyright 2012-201
8
the original author or authors.
* Copyright 2012-201
9
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -26,6 +26,7 @@ import java.util.HashSet;
...
@@ -26,6 +26,7 @@ import java.util.HashSet;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.Set
;
import
java.util.function.Supplier
;
import
org.apache.http.client.HttpClient
;
import
org.apache.http.client.HttpClient
;
import
org.apache.http.client.config.CookieSpecs
;
import
org.apache.http.client.config.CookieSpecs
;
...
@@ -38,6 +39,9 @@ import org.apache.http.impl.client.HttpClients;
...
@@ -38,6 +39,9 @@ import org.apache.http.impl.client.HttpClients;
import
org.apache.http.protocol.HttpContext
;
import
org.apache.http.protocol.HttpContext
;
import
org.apache.http.ssl.SSLContextBuilder
;
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.RestTemplateBuilder
;
import
org.springframework.boot.web.client.RootUriTemplateHandler
;
import
org.springframework.boot.web.client.RootUriTemplateHandler
;
import
org.springframework.core.ParameterizedTypeReference
;
import
org.springframework.core.ParameterizedTypeReference
;
...
@@ -1023,7 +1027,8 @@ public class TestRestTemplate {
...
@@ -1023,7 +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}.
* {@code username} and {@code password}. The request factory used is a new instance
* of the underlying {@link RestTemplate}'s request factory type (when possible).
* @param username the username
* @param username the username
* @param password the password
* @param password the password
* @return the new template
* @return the new template
...
@@ -1031,6 +1036,7 @@ public class TestRestTemplate {
...
@@ -1031,6 +1036,7 @@ public class TestRestTemplate {
*/
*/
public
TestRestTemplate
withBasicAuth
(
String
username
,
String
password
)
{
public
TestRestTemplate
withBasicAuth
(
String
username
,
String
password
)
{
RestTemplate
restTemplate
=
new
RestTemplateBuilder
()
RestTemplate
restTemplate
=
new
RestTemplateBuilder
()
.
requestFactory
(
getRequestFactorySupplier
())
.
messageConverters
(
getRestTemplate
().
getMessageConverters
())
.
messageConverters
(
getRestTemplate
().
getMessageConverters
())
.
interceptors
(
getRestTemplate
().
getInterceptors
())
.
interceptors
(
getRestTemplate
().
getInterceptors
())
.
uriTemplateHandler
(
getRestTemplate
().
getUriTemplateHandler
()).
build
();
.
uriTemplateHandler
(
getRestTemplate
().
getUriTemplateHandler
()).
build
();
...
@@ -1038,6 +1044,18 @@ public class TestRestTemplate {
...
@@ -1038,6 +1044,18 @@ public class TestRestTemplate {
this
.
httpClientOptions
);
this
.
httpClientOptions
);
}
}
private
Supplier
<
ClientHttpRequestFactory
>
getRequestFactorySupplier
()
{
return
()
->
{
try
{
return
BeanUtils
.
instantiateClass
(
getRequestFactoryClass
(
getRestTemplate
()));
}
catch
(
BeanInstantiationException
ex
)
{
return
new
ClientHttpRequestFactorySupplier
().
get
();
}
};
}
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
private
RequestEntity
<?>
createRequestEntityWithRootAppliedUri
(
private
RequestEntity
<?>
createRequestEntityWithRootAppliedUri
(
RequestEntity
<?>
requestEntity
)
{
RequestEntity
<?>
requestEntity
)
{
...
...
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java
View file @
0611eb49
/*
/*
* Copyright 2012-201
8
the original author or authors.
* Copyright 2012-201
9
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -33,6 +33,7 @@ import org.springframework.http.HttpEntity;
...
@@ -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
;
...
@@ -93,6 +94,35 @@ public class TestRestTemplateTests {
...
@@ -93,6 +94,35 @@ public class TestRestTemplateTests {
.
isInstanceOf
(
OkHttp3ClientHttpRequestFactory
.
class
);
.
isInstanceOf
(
OkHttp3ClientHttpRequestFactory
.
class
);
}
}
@Test
public
void
useTheSameRequestFactoryClassWithBasicAuth
()
{
OkHttp3ClientHttpRequestFactory
customFactory
=
new
OkHttp3ClientHttpRequestFactory
();
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
)
.
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"
;
...
@@ -384,4 +414,17 @@ public class TestRestTemplateTests {
...
@@ -384,4 +414,17 @@ public class TestRestTemplateTests {
}
}
static
class
TestClientHttpRequestFactory
implements
ClientHttpRequestFactory
{
TestClientHttpRequestFactory
(
String
value
)
{
}
@Override
public
ClientHttpRequest
createRequest
(
URI
uri
,
HttpMethod
httpMethod
)
throws
IOException
{
return
null
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment