Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
带水的鱼儿
spring-framework
提交
96ee8a3b
S
spring-framework
项目概览
带水的鱼儿
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
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,发现更多精彩内容 >>
提交
96ee8a3b
编写于
7月 28, 2021
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Ensure characterEncoding in MockHttpServletResponse is non-null
Closes gh-27219
上级
915f1027
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
53 addition
and
58 deletion
+53
-58
spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java
...org/springframework/mock/web/MockHttpServletResponse.java
+23
-21
spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java
...mework/test/web/servlet/result/PrintingResultHandler.java
+2
-5
spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java
...pringframework/mock/web/MockHttpServletResponseTests.java
+4
-0
spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java
...k/test/web/servlet/result/PrintingResultHandlerTests.java
+1
-11
spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java
...work/web/testfixture/servlet/MockHttpServletResponse.java
+23
-21
未找到文件。
spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java
浏览文件 @
96ee8a3b
...
...
@@ -80,7 +80,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
private
boolean
writerAccessAllowed
=
true
;
@Nullable
private
String
characterEncoding
=
WebUtils
.
DEFAULT_CHARACTER_ENCODING
;
/**
...
...
@@ -171,8 +170,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
* Determine whether the character encoding has been explicitly set through
* {@link HttpServletResponse} methods or through a {@code charset} parameter
* on the {@code Content-Type}.
* <p>If {@code false}, {@link #getCharacterEncoding()} will return
a
default
*
encoding value
.
* <p>If {@code false}, {@link #getCharacterEncoding()} will return
the
default
*
character encoding
.
*/
public
boolean
isCharset
()
{
return
this
.
characterEncodingSet
;
...
...
@@ -180,16 +179,21 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Override
public
void
setCharacterEncoding
(
String
characterEncoding
)
{
setExplicitCharacterEncoding
(
characterEncoding
);
updateContentTypePropertyAndHeader
();
}
private
void
setExplicitCharacterEncoding
(
String
characterEncoding
)
{
Assert
.
notNull
(
characterEncoding
,
"'characterEncoding' must not be null"
);
this
.
characterEncoding
=
characterEncoding
;
this
.
characterEncodingSet
=
true
;
updateContentTypePropertyAndHeader
();
}
private
void
updateContentTypePropertyAndHeader
()
{
if
(
this
.
contentType
!=
null
)
{
String
value
=
this
.
contentType
;
if
(
this
.
characterEncodingSet
&&
!
this
.
contentTyp
e
.
toLowerCase
().
contains
(
CHARSET_PREFIX
))
{
value
=
value
+
';'
+
CHARSET_PREFIX
+
this
.
characterEncoding
;
if
(
this
.
characterEncodingSet
&&
!
valu
e
.
toLowerCase
().
contains
(
CHARSET_PREFIX
))
{
value
+=
';'
+
CHARSET_PREFIX
+
getCharacterEncoding
()
;
this
.
contentType
=
value
;
}
doAddHeaderValue
(
HttpHeaders
.
CONTENT_TYPE
,
value
,
true
);
...
...
@@ -197,7 +201,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
}
@Override
@Nullable
public
String
getCharacterEncoding
()
{
return
this
.
characterEncoding
;
}
...
...
@@ -212,9 +215,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
public
PrintWriter
getWriter
()
throws
UnsupportedEncodingException
{
Assert
.
state
(
this
.
writerAccessAllowed
,
"Writer access not allowed"
);
if
(
this
.
writer
==
null
)
{
Writer
targetWriter
=
(
this
.
characterEncoding
!=
null
?
new
OutputStreamWriter
(
this
.
content
,
this
.
characterEncoding
)
:
new
OutputStreamWriter
(
this
.
content
));
Writer
targetWriter
=
new
OutputStreamWriter
(
this
.
content
,
getCharacterEncoding
());
this
.
writer
=
new
ResponsePrintWriter
(
targetWriter
);
}
return
this
.
writer
;
...
...
@@ -228,14 +229,16 @@ public class MockHttpServletResponse implements HttpServletResponse {
* Get the content of the response body as a {@code String}, using the charset
* specified for the response by the application, either through
* {@link HttpServletResponse} methods or through a charset parameter on the
* {@code Content-Type}.
* {@code Content-Type}. If no charset has been explicitly defined, the default
* character encoding will be used.
* @return the content as a {@code String}
* @throws UnsupportedEncodingException if the character encoding is not supported
* @see #getContentAsString(Charset)
* @see #setCharacterEncoding(String)
* @see #setContentType(String)
*/
public
String
getContentAsString
()
throws
UnsupportedEncodingException
{
return
(
this
.
characterEncoding
!=
null
?
this
.
content
.
toString
(
this
.
characterEncoding
)
:
this
.
content
.
toString
());
return
this
.
content
.
toString
(
getCharacterEncoding
());
}
/**
...
...
@@ -248,11 +251,12 @@ public class MockHttpServletResponse implements HttpServletResponse {
* @throws UnsupportedEncodingException if the character encoding is not supported
* @since 5.2
* @see #getContentAsString()
* @see #setCharacterEncoding(String)
* @see #setContentType(String)
*/
public
String
getContentAsString
(
Charset
fallbackCharset
)
throws
UnsupportedEncodingException
{
return
(
isCharset
()
&&
this
.
characterEncoding
!=
null
?
this
.
content
.
toString
(
this
.
characterEncoding
)
:
this
.
content
.
toString
(
fallbackCharset
.
name
()));
String
charsetName
=
(
this
.
characterEncodingSet
?
getCharacterEncoding
()
:
fallbackCharset
.
name
());
return
this
.
content
.
toString
(
charsetName
);
}
@Override
...
...
@@ -282,16 +286,14 @@ public class MockHttpServletResponse implements HttpServletResponse {
try
{
MediaType
mediaType
=
MediaType
.
parseMediaType
(
contentType
);
if
(
mediaType
.
getCharset
()
!=
null
)
{
this
.
characterEncoding
=
mediaType
.
getCharset
().
name
();
this
.
characterEncodingSet
=
true
;
setExplicitCharacterEncoding
(
mediaType
.
getCharset
().
name
());
}
}
catch
(
Exception
ex
)
{
// Try to get charset value anyway
int
charsetIndex
=
contentType
.
toLowerCase
().
indexOf
(
CHARSET_PREFIX
);
if
(
charsetIndex
!=
-
1
)
{
this
.
characterEncoding
=
contentType
.
substring
(
charsetIndex
+
CHARSET_PREFIX
.
length
());
this
.
characterEncodingSet
=
true
;
setExplicitCharacterEncoding
(
contentType
.
substring
(
charsetIndex
+
CHARSET_PREFIX
.
length
()));
}
}
updateContentTypePropertyAndHeader
();
...
...
@@ -344,7 +346,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Override
public
void
reset
()
{
resetBuffer
();
this
.
characterEncoding
=
null
;
this
.
characterEncoding
=
WebUtils
.
DEFAULT_CHARACTER_ENCODING
;
this
.
characterEncodingSet
=
false
;
this
.
contentLength
=
0
;
this
.
contentType
=
null
;
...
...
spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java
浏览文件 @
96ee8a3b
/*
* Copyright 2002-20
18
the original author or authors.
* Copyright 2002-20
21
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.
...
...
@@ -248,14 +248,11 @@ public class PrintingResultHandler implements ResultHandler {
* Print the response.
*/
protected
void
printResponse
(
MockHttpServletResponse
response
)
throws
Exception
{
String
body
=
(
response
.
getCharacterEncoding
()
!=
null
?
response
.
getContentAsString
()
:
MISSING_CHARACTER_ENCODING
);
this
.
printer
.
printValue
(
"Status"
,
response
.
getStatus
());
this
.
printer
.
printValue
(
"Error message"
,
response
.
getErrorMessage
());
this
.
printer
.
printValue
(
"Headers"
,
getResponseHeaders
(
response
));
this
.
printer
.
printValue
(
"Content type"
,
response
.
getContentType
());
this
.
printer
.
printValue
(
"Body"
,
body
);
this
.
printer
.
printValue
(
"Body"
,
response
.
getContentAsString
()
);
this
.
printer
.
printValue
(
"Forwarded URL"
,
response
.
getForwardedUrl
());
this
.
printer
.
printValue
(
"Redirected URL"
,
response
.
getRedirectedUrl
());
printCookies
(
response
.
getCookies
());
...
...
spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java
浏览文件 @
96ee8a3b
...
...
@@ -551,6 +551,8 @@ class MockHttpServletResponseTests {
@Test
// gh-25501
void
resetResetsCharset
()
{
assertThat
(
response
.
getContentType
()).
isNull
();
assertThat
(
response
.
getCharacterEncoding
()).
isEqualTo
(
"ISO-8859-1"
);
assertThat
(
response
.
isCharset
()).
isFalse
();
response
.
setCharacterEncoding
(
"UTF-8"
);
assertThat
(
response
.
isCharset
()).
isTrue
();
...
...
@@ -562,6 +564,8 @@ class MockHttpServletResponseTests {
response
.
reset
();
assertThat
(
response
.
getContentType
()).
isNull
();
assertThat
(
response
.
getCharacterEncoding
()).
isEqualTo
(
"ISO-8859-1"
);
assertThat
(
response
.
isCharset
()).
isFalse
();
// Do not invoke setCharacterEncoding() since that sets the charset flag to true.
// response.setCharacterEncoding("UTF-8");
...
...
spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java
浏览文件 @
96ee8a3b
/*
* Copyright 2002-20
19
the original author or authors.
* Copyright 2002-20
21
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.
...
...
@@ -238,16 +238,6 @@ public class PrintingResultHandlerTests {
assertValue
(
"MockHttpServletResponse"
,
"Body"
,
"text"
);
}
@Test
public
void
printResponseWithoutCharacterEncoding
()
throws
Exception
{
this
.
response
.
setCharacterEncoding
(
null
);
this
.
response
.
getWriter
().
print
(
"text"
);
this
.
handler
.
handle
(
this
.
mvcResult
);
assertValue
(
"MockHttpServletResponse"
,
"Body"
,
"<no character encoding set>"
);
}
@Test
public
void
printHandlerNull
()
throws
Exception
{
StubMvcResult
mvcResult
=
new
StubMvcResult
(
this
.
request
,
null
,
null
,
null
,
null
,
null
,
this
.
response
);
...
...
spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java
浏览文件 @
96ee8a3b
...
...
@@ -80,7 +80,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
private
boolean
writerAccessAllowed
=
true
;
@Nullable
private
String
characterEncoding
=
WebUtils
.
DEFAULT_CHARACTER_ENCODING
;
/**
...
...
@@ -171,8 +170,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
* Determine whether the character encoding has been explicitly set through
* {@link HttpServletResponse} methods or through a {@code charset} parameter
* on the {@code Content-Type}.
* <p>If {@code false}, {@link #getCharacterEncoding()} will return
a
default
*
encoding value
.
* <p>If {@code false}, {@link #getCharacterEncoding()} will return
the
default
*
character encoding
.
*/
public
boolean
isCharset
()
{
return
this
.
characterEncodingSet
;
...
...
@@ -180,16 +179,21 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Override
public
void
setCharacterEncoding
(
String
characterEncoding
)
{
setExplicitCharacterEncoding
(
characterEncoding
);
updateContentTypePropertyAndHeader
();
}
private
void
setExplicitCharacterEncoding
(
String
characterEncoding
)
{
Assert
.
notNull
(
characterEncoding
,
"'characterEncoding' must not be null"
);
this
.
characterEncoding
=
characterEncoding
;
this
.
characterEncodingSet
=
true
;
updateContentTypePropertyAndHeader
();
}
private
void
updateContentTypePropertyAndHeader
()
{
if
(
this
.
contentType
!=
null
)
{
String
value
=
this
.
contentType
;
if
(
this
.
characterEncodingSet
&&
!
this
.
contentTyp
e
.
toLowerCase
().
contains
(
CHARSET_PREFIX
))
{
value
=
value
+
';'
+
CHARSET_PREFIX
+
this
.
characterEncoding
;
if
(
this
.
characterEncodingSet
&&
!
valu
e
.
toLowerCase
().
contains
(
CHARSET_PREFIX
))
{
value
+=
';'
+
CHARSET_PREFIX
+
getCharacterEncoding
()
;
this
.
contentType
=
value
;
}
doAddHeaderValue
(
HttpHeaders
.
CONTENT_TYPE
,
value
,
true
);
...
...
@@ -197,7 +201,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
}
@Override
@Nullable
public
String
getCharacterEncoding
()
{
return
this
.
characterEncoding
;
}
...
...
@@ -212,9 +215,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
public
PrintWriter
getWriter
()
throws
UnsupportedEncodingException
{
Assert
.
state
(
this
.
writerAccessAllowed
,
"Writer access not allowed"
);
if
(
this
.
writer
==
null
)
{
Writer
targetWriter
=
(
this
.
characterEncoding
!=
null
?
new
OutputStreamWriter
(
this
.
content
,
this
.
characterEncoding
)
:
new
OutputStreamWriter
(
this
.
content
));
Writer
targetWriter
=
new
OutputStreamWriter
(
this
.
content
,
getCharacterEncoding
());
this
.
writer
=
new
ResponsePrintWriter
(
targetWriter
);
}
return
this
.
writer
;
...
...
@@ -228,14 +229,16 @@ public class MockHttpServletResponse implements HttpServletResponse {
* Get the content of the response body as a {@code String}, using the charset
* specified for the response by the application, either through
* {@link HttpServletResponse} methods or through a charset parameter on the
* {@code Content-Type}.
* {@code Content-Type}. If no charset has been explicitly defined, the default
* character encoding will be used.
* @return the content as a {@code String}
* @throws UnsupportedEncodingException if the character encoding is not supported
* @see #getContentAsString(Charset)
* @see #setCharacterEncoding(String)
* @see #setContentType(String)
*/
public
String
getContentAsString
()
throws
UnsupportedEncodingException
{
return
(
this
.
characterEncoding
!=
null
?
this
.
content
.
toString
(
this
.
characterEncoding
)
:
this
.
content
.
toString
());
return
this
.
content
.
toString
(
getCharacterEncoding
());
}
/**
...
...
@@ -248,11 +251,12 @@ public class MockHttpServletResponse implements HttpServletResponse {
* @throws UnsupportedEncodingException if the character encoding is not supported
* @since 5.2
* @see #getContentAsString()
* @see #setCharacterEncoding(String)
* @see #setContentType(String)
*/
public
String
getContentAsString
(
Charset
fallbackCharset
)
throws
UnsupportedEncodingException
{
return
(
isCharset
()
&&
this
.
characterEncoding
!=
null
?
this
.
content
.
toString
(
this
.
characterEncoding
)
:
this
.
content
.
toString
(
fallbackCharset
.
name
()));
String
charsetName
=
(
this
.
characterEncodingSet
?
getCharacterEncoding
()
:
fallbackCharset
.
name
());
return
this
.
content
.
toString
(
charsetName
);
}
@Override
...
...
@@ -282,16 +286,14 @@ public class MockHttpServletResponse implements HttpServletResponse {
try
{
MediaType
mediaType
=
MediaType
.
parseMediaType
(
contentType
);
if
(
mediaType
.
getCharset
()
!=
null
)
{
this
.
characterEncoding
=
mediaType
.
getCharset
().
name
();
this
.
characterEncodingSet
=
true
;
setExplicitCharacterEncoding
(
mediaType
.
getCharset
().
name
());
}
}
catch
(
Exception
ex
)
{
// Try to get charset value anyway
int
charsetIndex
=
contentType
.
toLowerCase
().
indexOf
(
CHARSET_PREFIX
);
if
(
charsetIndex
!=
-
1
)
{
this
.
characterEncoding
=
contentType
.
substring
(
charsetIndex
+
CHARSET_PREFIX
.
length
());
this
.
characterEncodingSet
=
true
;
setExplicitCharacterEncoding
(
contentType
.
substring
(
charsetIndex
+
CHARSET_PREFIX
.
length
()));
}
}
updateContentTypePropertyAndHeader
();
...
...
@@ -344,7 +346,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
@Override
public
void
reset
()
{
resetBuffer
();
this
.
characterEncoding
=
null
;
this
.
characterEncoding
=
WebUtils
.
DEFAULT_CHARACTER_ENCODING
;
this
.
characterEncodingSet
=
false
;
this
.
contentLength
=
0
;
this
.
contentType
=
null
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录