Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
7e3e79ca
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7e3e79ca
编写于
4月 09, 2009
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-5653: RestTemplate handles redirects incorrectly
上级
859497b1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
73 addition
and
14 deletion
+73
-14
org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java
...framework/http/client/SimpleClientHttpRequestFactory.java
+12
-4
org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java
...ework/http/client/AbstractHttpRequestFactoryTestCase.java
+61
-10
未找到文件。
org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java
浏览文件 @
7e3e79ca
...
...
@@ -28,9 +28,9 @@ import org.springframework.util.Assert;
* {@link ClientHttpRequestFactory} implementation that uses standard J2SE facilities.
*
* @author Arjen Poutsma
* @since 3.0
* @see java.net.HttpURLConnection
* @see CommonsClientHttpRequestFactory
* @since 3.0
*/
public
class
SimpleClientHttpRequestFactory
implements
ClientHttpRequestFactory
{
...
...
@@ -43,17 +43,25 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory
}
/**
* Template method for preparing the given {@link HttpURLConnection}.
* <p>The default implementation prepares the connection for input and output, and sets the HTTP method.
* Template method for preparing the given {@link HttpURLConnection}. <p>The default implementation prepares the
* connection for input and output, and sets the HTTP method.
*
* @param connection the connection to prepare
* @param httpMethod the HTTP request method ({@code GET}, {@code POST}, etc.)
* @throws IOException in case of I/O errors
*/
protected
void
prepareConnection
(
HttpURLConnection
connection
,
String
httpMethod
)
throws
IOException
{
connection
.
setDoInput
(
true
);
if
(
"GET"
.
equals
(
httpMethod
))
{
connection
.
setInstanceFollowRedirects
(
true
);
}
else
{
connection
.
setInstanceFollowRedirects
(
false
);
}
if
(
"PUT"
.
equals
(
httpMethod
)
||
"POST"
.
equals
(
httpMethod
))
{
connection
.
setDoOutput
(
true
);
}
else
{
}
else
{
connection
.
setDoOutput
(
false
);
}
connection
.
setRequestMethod
(
httpMethod
);
...
...
org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java
浏览文件 @
7e3e79ca
...
...
@@ -48,18 +48,22 @@ public abstract class AbstractHttpRequestFactoryTestCase {
private
static
Server
jettyServer
;
private
static
final
String
BASE_URL
=
"http://localhost:8889"
;
@BeforeClass
public
static
void
startJettyServer
()
throws
Exception
{
jettyServer
=
new
Server
(
8889
);
Context
jettyContext
=
new
Context
(
jettyServer
,
"/"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
EchoServlet
()),
"/echo"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
ErrorServlet
(
404
)),
"/errors/notfound"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
StatusServlet
(
200
)),
"/status/ok"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
StatusServlet
(
404
)),
"/status/notfound"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
MethodServlet
(
"DELETE"
)),
"/methods/delete"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
MethodServlet
(
"GET"
)),
"/methods/get"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
MethodServlet
(
"HEAD"
)),
"/methods/head"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
MethodServlet
(
"OPTIONS"
)),
"/methods/options"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
MethodServlet
(
"POST"
)),
"/methods/post"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
MethodServlet
(
"PUT"
)),
"/methods/put"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
RedirectServlet
(
"/status/ok"
)),
"/redirect"
);
jettyServer
.
start
();
}
...
...
@@ -80,7 +84,7 @@ public abstract class AbstractHttpRequestFactoryTestCase {
@Test
public
void
status
()
throws
Exception
{
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
"http://localhost:8889/error
s/notfound"
),
HttpMethod
.
GET
);
factory
.
createRequest
(
new
URI
(
BASE_URL
+
"/statu
s/notfound"
),
HttpMethod
.
GET
);
assertEquals
(
"Invalid HTTP method"
,
HttpMethod
.
GET
,
request
.
getMethod
());
ClientHttpResponse
response
=
request
.
execute
();
assertEquals
(
"Invalid status code"
,
HttpStatus
.
NOT_FOUND
,
response
.
getStatusCode
());
...
...
@@ -88,7 +92,7 @@ public abstract class AbstractHttpRequestFactoryTestCase {
@Test
public
void
echo
()
throws
Exception
{
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
"http://localhost:8889
/echo"
),
HttpMethod
.
PUT
);
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
BASE_URL
+
"
/echo"
),
HttpMethod
.
PUT
);
assertEquals
(
"Invalid HTTP method"
,
HttpMethod
.
PUT
,
request
.
getMethod
());
String
headerName
=
"MyHeader"
;
String
headerValue1
=
"value1"
;
...
...
@@ -108,7 +112,7 @@ public abstract class AbstractHttpRequestFactoryTestCase {
@Test
(
expected
=
IllegalStateException
.
class
)
public
void
multipleWrites
()
throws
Exception
{
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
"http://localhost:8889
/echo"
),
HttpMethod
.
POST
);
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
BASE_URL
+
"
/echo"
),
HttpMethod
.
POST
);
byte
[]
body
=
"Hello World"
.
getBytes
(
"UTF-8"
);
FileCopyUtils
.
copy
(
body
,
request
.
getBody
());
ClientHttpResponse
response
=
request
.
execute
();
...
...
@@ -122,7 +126,7 @@ public abstract class AbstractHttpRequestFactoryTestCase {
@Test
(
expected
=
IllegalStateException
.
class
)
public
void
headersAfterExecute
()
throws
Exception
{
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
"http://localhost:8889
/echo"
),
HttpMethod
.
POST
);
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
BASE_URL
+
"
/echo"
),
HttpMethod
.
POST
);
request
.
getHeaders
().
add
(
"MyHeader"
,
"value"
);
byte
[]
body
=
"Hello World"
.
getBytes
(
"UTF-8"
);
FileCopyUtils
.
copy
(
body
,
request
.
getBody
());
...
...
@@ -148,7 +152,7 @@ public abstract class AbstractHttpRequestFactoryTestCase {
private
void
assertHttpMethod
(
String
path
,
HttpMethod
method
)
throws
Exception
{
ClientHttpResponse
response
=
null
;
try
{
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
"http://localhost:8889
/methods/"
+
path
),
method
);
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
BASE_URL
+
"
/methods/"
+
path
),
method
);
response
=
request
.
execute
();
assertEquals
(
"Invalid method"
,
path
.
toUpperCase
(
Locale
.
ENGLISH
),
request
.
getMethod
().
name
());
}
...
...
@@ -158,19 +162,45 @@ public abstract class AbstractHttpRequestFactoryTestCase {
}
}
}
@Test
public
void
redirect
()
throws
Exception
{
ClientHttpResponse
response
=
null
;
try
{
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
BASE_URL
+
"/redirect"
),
HttpMethod
.
PUT
);
response
=
request
.
execute
();
assertEquals
(
"Invalid Location value"
,
new
URI
(
BASE_URL
+
"/status/ok"
),
response
.
getHeaders
().
getLocation
());
/** Servlet that returns and error message for a given status code. */
private
static
class
ErrorServlet
extends
GenericServlet
{
}
finally
{
if
(
response
!=
null
)
{
response
.
close
();
response
=
null
;
}
}
try
{
ClientHttpRequest
request
=
factory
.
createRequest
(
new
URI
(
BASE_URL
+
"/redirect"
),
HttpMethod
.
GET
);
response
=
request
.
execute
();
assertNull
(
"Invalid Location value"
,
response
.
getHeaders
().
getLocation
());
}
finally
{
if
(
response
!=
null
)
{
response
.
close
();
}
}
}
/** Servlet that sets a given status code. */
private
static
class
StatusServlet
extends
GenericServlet
{
private
final
int
sc
;
private
Error
Servlet
(
int
sc
)
{
private
Status
Servlet
(
int
sc
)
{
this
.
sc
=
sc
;
}
@Override
public
void
service
(
ServletRequest
request
,
ServletResponse
response
)
throws
ServletException
,
IOException
{
((
HttpServletResponse
)
response
).
se
ndError
(
sc
);
((
HttpServletResponse
)
response
).
se
tStatus
(
sc
);
}
}
...
...
@@ -216,4 +246,25 @@ public abstract class AbstractHttpRequestFactoryTestCase {
}
}
private
static
class
RedirectServlet
extends
GenericServlet
{
private
final
String
location
;
private
RedirectServlet
(
String
location
)
{
this
.
location
=
location
;
}
@Override
public
void
service
(
ServletRequest
req
,
ServletResponse
res
)
throws
ServletException
,
IOException
{
HttpServletRequest
request
=
(
HttpServletRequest
)
req
;
HttpServletResponse
response
=
(
HttpServletResponse
)
res
;
response
.
setStatus
(
HttpServletResponse
.
SC_SEE_OTHER
);
StringBuilder
builder
=
new
StringBuilder
();
builder
.
append
(
request
.
getScheme
()).
append
(
"://"
);
builder
.
append
(
request
.
getServerName
()).
append
(
':'
).
append
(
request
.
getServerPort
());
builder
.
append
(
location
);
response
.
addHeader
(
"Location"
,
builder
.
toString
());
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录