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
4ec059ea
Commit
4ec059ea
authored
Apr 18, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure authentication is added to customized request factory
Fixes gh-692
parent
7401f7c6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
8 deletions
+5
-8
TestRestTemplate.java
.../java/org/springframework/boot/test/TestRestTemplate.java
+5
-8
No files found.
spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java
View file @
4ec059ea
...
...
@@ -29,12 +29,10 @@ import org.apache.http.protocol.HttpContext;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpRequest
;
import
org.springframework.http.client.ClientHttpRequestExecution
;
import
org.springframework.http.client.ClientHttpRequestFactory
;
import
org.springframework.http.client.ClientHttpRequestInterceptor
;
import
org.springframework.http.client.ClientHttpResponse
;
import
org.springframework.http.client.HttpComponentsClientHttpRequestFactory
;
import
org.springframework.http.client.InterceptingClientHttpRequestFactory
;
import
org.springframework.http.client.SimpleClientHttpRequestFactory
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.web.client.DefaultResponseErrorHandler
;
import
org.springframework.web.client.RestTemplate
;
...
...
@@ -63,10 +61,10 @@ public class TestRestTemplate extends RestTemplate {
* @param password the password (or {@code null})
*/
public
TestRestTemplate
(
String
username
,
String
password
)
{
super
(
getRequestFactory
(
username
,
password
));
if
(
ClassUtils
.
isPresent
(
"org.apache.http.client.config.RequestConfig"
,
null
))
{
new
HttpComponentsCustomizer
().
customize
(
this
);
}
addAuthentication
(
username
,
password
);
setErrorHandler
(
new
DefaultResponseErrorHandler
()
{
@Override
public
void
handleError
(
ClientHttpResponse
response
)
throws
IOException
{
...
...
@@ -75,16 +73,15 @@ public class TestRestTemplate extends RestTemplate {
}
private
static
ClientHttpRequestFactory
getRequestFactory
(
String
username
,
String
password
)
{
SimpleClientHttpRequestFactory
factory
=
new
SimpleClientHttpRequestFactory
();
private
void
addAuthentication
(
String
username
,
String
password
)
{
if
(
username
==
null
)
{
return
factory
;
return
;
}
List
<
ClientHttpRequestInterceptor
>
interceptors
=
Collections
.<
ClientHttpRequestInterceptor
>
singletonList
(
new
BasicAuthorizationInterceptor
(
username
,
password
));
return
new
InterceptingClientHttpRequestFactory
(
factory
,
interceptors
);
setRequestFactory
(
new
InterceptingClientHttpRequestFactory
(
getRequestFactory
(),
interceptors
));
}
private
static
class
BasicAuthorizationInterceptor
implements
...
...
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