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
b9b0cdb3
Commit
b9b0cdb3
authored
Feb 02, 2017
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.4.x' into 1.5.x
parents
9b952dd3
2f50d515
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
29 deletions
+42
-29
TestRestTemplate.java
...pringframework/boot/test/web/client/TestRestTemplate.java
+35
-22
TestRestTemplateTests.java
...framework/boot/test/web/client/TestRestTemplateTests.java
+7
-7
No files found.
spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java
View file @
b9b0cdb3
...
...
@@ -39,6 +39,7 @@ import org.apache.http.protocol.HttpContext;
import
org.apache.http.ssl.SSLContextBuilder
;
import
org.springframework.boot.web.client.RestTemplateBuilder
;
import
org.springframework.boot.web.client.RootUriTemplateHandler
;
import
org.springframework.core.ParameterizedTypeReference
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
...
...
@@ -217,7 +218,7 @@ public class TestRestTemplate {
* @see RestTemplate#getForObject(java.net.URI, java.lang.Class)
*/
public
<
T
>
T
getForObject
(
URI
url
,
Class
<
T
>
responseType
)
throws
RestClientException
{
return
this
.
restTemplate
.
getForObject
(
url
.
toString
(
),
responseType
);
return
this
.
restTemplate
.
getForObject
(
applyRootUriIfNecessary
(
url
),
responseType
);
}
/**
...
...
@@ -269,7 +270,7 @@ public class TestRestTemplate {
*/
public
<
T
>
ResponseEntity
<
T
>
getForEntity
(
URI
url
,
Class
<
T
>
responseType
)
throws
RestClientException
{
return
this
.
restTemplate
.
getForEntity
(
url
.
toString
(
),
responseType
);
return
this
.
restTemplate
.
getForEntity
(
applyRootUriIfNecessary
(
url
),
responseType
);
}
/**
...
...
@@ -310,7 +311,7 @@ public class TestRestTemplate {
* @see RestTemplate#headForHeaders(java.net.URI)
*/
public
HttpHeaders
headForHeaders
(
URI
url
)
throws
RestClientException
{
return
this
.
restTemplate
.
headForHeaders
(
url
.
toString
(
));
return
this
.
restTemplate
.
headForHeaders
(
applyRootUriIfNecessary
(
url
));
}
/**
...
...
@@ -374,7 +375,7 @@ public class TestRestTemplate {
* @see RestTemplate#postForLocation(java.net.URI, java.lang.Object)
*/
public
URI
postForLocation
(
URI
url
,
Object
request
)
throws
RestClientException
{
return
this
.
restTemplate
.
postForLocation
(
url
.
toString
(
),
request
);
return
this
.
restTemplate
.
postForLocation
(
applyRootUriIfNecessary
(
url
),
request
);
}
/**
...
...
@@ -442,7 +443,8 @@ public class TestRestTemplate {
*/
public
<
T
>
T
postForObject
(
URI
url
,
Object
request
,
Class
<
T
>
responseType
)
throws
RestClientException
{
return
this
.
restTemplate
.
postForObject
(
url
.
toString
(),
request
,
responseType
);
return
this
.
restTemplate
.
postForObject
(
applyRootUriIfNecessary
(
url
),
request
,
responseType
);
}
/**
...
...
@@ -511,7 +513,8 @@ public class TestRestTemplate {
*/
public
<
T
>
ResponseEntity
<
T
>
postForEntity
(
URI
url
,
Object
request
,
Class
<
T
>
responseType
)
throws
RestClientException
{
return
this
.
restTemplate
.
postForEntity
(
url
.
toString
(),
request
,
responseType
);
return
this
.
restTemplate
.
postForEntity
(
applyRootUriIfNecessary
(
url
),
request
,
responseType
);
}
/**
...
...
@@ -564,7 +567,7 @@ public class TestRestTemplate {
* @see RestTemplate#put(java.net.URI, java.lang.Object)
*/
public
void
put
(
URI
url
,
Object
request
)
throws
RestClientException
{
this
.
restTemplate
.
put
(
url
.
toString
(
),
request
);
this
.
restTemplate
.
put
(
applyRootUriIfNecessary
(
url
),
request
);
}
/**
...
...
@@ -630,7 +633,8 @@ public class TestRestTemplate {
*/
public
<
T
>
T
patchForObject
(
URI
url
,
Object
request
,
Class
<
T
>
responseType
)
throws
RestClientException
{
return
this
.
restTemplate
.
patchForObject
(
url
.
toString
(),
request
,
responseType
);
return
this
.
restTemplate
.
patchForObject
(
applyRootUriIfNecessary
(
url
),
request
,
responseType
);
}
...
...
@@ -668,7 +672,7 @@ public class TestRestTemplate {
* @see RestTemplate#delete(java.net.URI)
*/
public
void
delete
(
URI
url
)
throws
RestClientException
{
this
.
restTemplate
.
delete
(
url
.
toString
(
));
this
.
restTemplate
.
delete
(
applyRootUriIfNecessary
(
url
));
}
/**
...
...
@@ -709,7 +713,7 @@ public class TestRestTemplate {
* @see RestTemplate#optionsForAllow(java.net.URI)
*/
public
Set
<
HttpMethod
>
optionsForAllow
(
URI
url
)
throws
RestClientException
{
return
this
.
restTemplate
.
optionsForAllow
(
url
.
toString
(
));
return
this
.
restTemplate
.
optionsForAllow
(
applyRootUriIfNecessary
(
url
));
}
/**
...
...
@@ -777,8 +781,8 @@ public class TestRestTemplate {
public
<
T
>
ResponseEntity
<
T
>
exchange
(
URI
url
,
HttpMethod
method
,
HttpEntity
<?>
requestEntity
,
Class
<
T
>
responseType
)
throws
RestClientException
{
return
this
.
restTemplate
.
exchange
(
url
.
toString
(),
method
,
requestEntity
,
responseType
);
return
this
.
restTemplate
.
exchange
(
applyRootUriIfNecessary
(
url
),
method
,
re
questEntity
,
re
sponseType
);
}
/**
...
...
@@ -860,8 +864,8 @@ public class TestRestTemplate {
public
<
T
>
ResponseEntity
<
T
>
exchange
(
URI
url
,
HttpMethod
method
,
HttpEntity
<?>
requestEntity
,
ParameterizedTypeReference
<
T
>
responseType
)
throws
RestClientException
{
return
this
.
restTemplate
.
exchange
(
url
.
toString
(),
method
,
requestEntity
,
responseType
);
return
this
.
restTemplate
.
exchange
(
applyRootUriIfNecessary
(
url
),
method
,
re
questEntity
,
re
sponseType
);
}
/**
...
...
@@ -882,7 +886,7 @@ public class TestRestTemplate {
public
<
T
>
ResponseEntity
<
T
>
exchange
(
RequestEntity
<?>
requestEntity
,
Class
<
T
>
responseType
)
throws
RestClientException
{
return
this
.
restTemplate
.
exchange
(
createRequestEntityWith
Expand
edUri
(
requestEntity
),
responseType
);
createRequestEntityWith
RootAppli
edUri
(
requestEntity
),
responseType
);
}
/**
...
...
@@ -905,7 +909,7 @@ public class TestRestTemplate {
public
<
T
>
ResponseEntity
<
T
>
exchange
(
RequestEntity
<?>
requestEntity
,
ParameterizedTypeReference
<
T
>
responseType
)
throws
RestClientException
{
return
this
.
restTemplate
.
exchange
(
createRequestEntityWith
Expand
edUri
(
requestEntity
),
responseType
);
createRequestEntityWith
RootAppli
edUri
(
requestEntity
),
responseType
);
}
/**
...
...
@@ -972,8 +976,8 @@ public class TestRestTemplate {
*/
public
<
T
>
T
execute
(
URI
url
,
HttpMethod
method
,
RequestCallback
requestCallback
,
ResponseExtractor
<
T
>
responseExtractor
)
throws
RestClientException
{
return
this
.
restTemplate
.
execute
(
url
.
toString
(),
method
,
requestCallback
,
responseExtractor
);
return
this
.
restTemplate
.
execute
(
applyRootUriIfNecessary
(
url
),
method
,
re
questCallback
,
re
sponseExtractor
);
}
/**
...
...
@@ -1008,12 +1012,21 @@ public class TestRestTemplate {
}
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
private
RequestEntity
<?>
createRequestEntityWith
Expand
edUri
(
private
RequestEntity
<?>
createRequestEntityWith
RootAppli
edUri
(
RequestEntity
<?>
requestEntity
)
{
URI
expandedUri
=
this
.
restTemplate
.
getUriTemplateHandler
()
.
expand
(
requestEntity
.
getUrl
().
toString
());
return
new
RequestEntity
(
requestEntity
.
getBody
(),
requestEntity
.
getHeaders
(),
requestEntity
.
getMethod
(),
expandedUri
,
requestEntity
.
getType
());
requestEntity
.
getMethod
(),
applyRootUriIfNecessary
(
requestEntity
.
getUrl
()),
requestEntity
.
getType
());
}
private
URI
applyRootUriIfNecessary
(
URI
uri
)
{
UriTemplateHandler
uriTemplateHandler
=
this
.
restTemplate
.
getUriTemplateHandler
();
if
((
uriTemplateHandler
instanceof
RootUriTemplateHandler
)
&&
uri
.
toString
().
startsWith
(
"/"
))
{
return
URI
.
create
(((
RootUriTemplateHandler
)
uriTemplateHandler
).
getRootUri
()
+
uri
.
toString
());
}
return
uri
;
}
/**
...
...
spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java
View file @
b9b0cdb3
...
...
@@ -38,6 +38,7 @@ import org.springframework.http.client.ClientHttpRequestInterceptor;
import
org.springframework.http.client.HttpComponentsClientHttpRequestFactory
;
import
org.springframework.http.client.InterceptingClientHttpRequestFactory
;
import
org.springframework.http.client.support.BasicAuthorizationInterceptor
;
import
org.springframework.mock.env.MockEnvironment
;
import
org.springframework.mock.http.client.MockClientHttpRequest
;
import
org.springframework.mock.http.client.MockClientHttpResponse
;
import
org.springframework.test.util.ReflectionTestUtils
;
...
...
@@ -47,7 +48,6 @@ import org.springframework.web.client.ResponseErrorHandler;
import
org.springframework.web.client.RestOperations
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.util.DefaultUriTemplateHandler
;
import
org.springframework.web.util.UriTemplateHandler
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
BDDMockito
.
given
;
...
...
@@ -427,18 +427,18 @@ public class TestRestTemplateTests {
ClientHttpRequestFactory
requestFactory
=
mock
(
ClientHttpRequestFactory
.
class
);
MockClientHttpRequest
request
=
new
MockClientHttpRequest
();
request
.
setResponse
(
new
MockClientHttpResponse
(
new
byte
[
0
],
HttpStatus
.
OK
));
URI
relativeUri
=
URI
.
create
(
"a/b/c.txt"
);
URI
absoluteUri
=
URI
.
create
(
"http://localhost:8080/"
+
relativeUri
.
toString
()
);
URI
absoluteUri
=
URI
.
create
(
"http://localhost:8080/a/b/c.txt?param=%7Bsomething%7D"
);
given
(
requestFactory
.
createRequest
(
eq
(
absoluteUri
),
(
HttpMethod
)
any
()))
.
willReturn
(
request
);
RestTemplate
delegate
=
new
RestTemplate
();
TestRestTemplate
template
=
new
TestRestTemplate
(
delegate
);
delegate
.
setRequestFactory
(
requestFactory
);
UriTemplateHandler
uriTemplateHandler
=
mock
(
UriTemplateHandler
.
class
);
given
(
uriTemplateHandler
.
expand
(
relativeUri
.
toString
(),
new
Object
[
0
]))
.
willReturn
(
absoluteUri
);
LocalHostUriTemplateHandler
uriTemplateHandler
=
new
LocalHostUriTemplateHandler
(
new
MockEnvironment
());
template
.
setUriTemplateHandler
(
uriTemplateHandler
);
callback
.
doWithTestRestTemplate
(
template
,
relativeUri
);
callback
.
doWithTestRestTemplate
(
template
,
URI
.
create
(
"/a/b/c.txt?param=%7Bsomething%7D"
));
verify
(
requestFactory
).
createRequest
(
eq
(
absoluteUri
),
(
HttpMethod
)
any
());
}
...
...
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