Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dk131072
spring-framework
提交
952a3170
S
spring-framework
项目概览
dk131072
/
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,发现更多精彩内容 >>
提交
952a3170
编写于
1月 21, 2016
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
HeaderResultMatchers supports multi-value headers
Issue: SPR-10087
上级
ae224bed
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
61 addition
and
12 deletion
+61
-12
spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java
...amework/test/web/servlet/result/HeaderResultMatchers.java
+43
-11
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java
...mples/standalone/resultmatchers/HeaderAssertionTests.java
+17
-1
src/asciidoc/whats-new.adoc
src/asciidoc/whats-new.adoc
+1
-0
未找到文件。
spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java
浏览文件 @
952a3170
...
...
@@ -16,19 +16,22 @@
package
org.springframework.test.web.servlet.result
;
import
java.text.SimpleDateFormat
;
import
java.util.Arrays
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Locale
;
import
java.util.TimeZone
;
import
org.hamcrest.Matcher
;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.test.web.servlet.MvcResult
;
import
org.springframework.test.web.servlet.ResultMatcher
;
import
static
org
.
hamcrest
.
MatcherAssert
.*;
import
static
org
.
springframework
.
test
.
util
.
AssertionErrors
.*;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.Locale
;
import
java.util.TimeZone
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
springframework
.
test
.
util
.
AssertionErrors
.
assertEquals
;
import
static
org
.
springframework
.
test
.
util
.
AssertionErrors
.
assertTrue
;
/**
* Factory for response header assertions.
...
...
@@ -52,8 +55,8 @@ public class HeaderResultMatchers {
/**
* Assert the primary value of the
named response header with the given
*
Hamcrest {@link
Matcher}.
* Assert the primary value of the
response header with the given Hamcrest
*
String {@code
Matcher}.
*/
public
ResultMatcher
string
(
final
String
name
,
final
Matcher
<?
super
String
>
matcher
)
{
return
new
ResultMatcher
()
{
...
...
@@ -65,13 +68,42 @@ public class HeaderResultMatchers {
}
/**
* Assert the primary value of the named response header as a {@link String}.
* Assert the values of the response header with the given Hamcrest
* Iterable {@link Matcher}.
* @since 4.3
*/
public
<
T
>
ResultMatcher
stringValues
(
final
String
name
,
final
Matcher
<
Iterable
<
String
>>
matcher
)
{
return
new
ResultMatcher
()
{
@Override
public
void
match
(
MvcResult
result
)
{
List
<
String
>
values
=
result
.
getResponse
().
getHeaders
(
name
);
assertThat
(
"Response header "
+
name
,
values
,
matcher
);
}
};
}
/**
* Assert the primary value of the response header as a String value.
*/
public
ResultMatcher
string
(
final
String
name
,
final
String
value
)
{
return
new
ResultMatcher
()
{
@Override
public
void
match
(
MvcResult
result
)
{
assertEquals
(
"Response header "
+
name
,
value
,
result
.
getResponse
().
getHeader
(
name
));
assertEquals
(
"Response header "
+
name
,
result
.
getResponse
().
getHeader
(
name
),
value
);
}
};
}
/**
* Assert the values of the response header as String values.
* @since 4.3
*/
public
ResultMatcher
stringValues
(
final
String
name
,
final
String
...
values
)
{
return
new
ResultMatcher
()
{
@Override
public
void
match
(
MvcResult
result
)
{
List
<
Object
>
actual
=
result
.
getResponse
().
getHeaderValues
(
name
);
assertEquals
(
"Response header "
+
name
,
Arrays
.
asList
(
values
),
actual
);
}
};
}
...
...
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java
浏览文件 @
952a3170
...
...
@@ -33,13 +33,17 @@ import org.springframework.web.bind.annotation.PathVariable;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.context.request.WebRequest
;
import
static
org
.
hamcrest
.
CoreMatchers
.
containsString
;
import
static
org
.
hamcrest
.
CoreMatchers
.
equalTo
;
import
static
org
.
hamcrest
.
CoreMatchers
.
hasItems
;
import
static
org
.
hamcrest
.
CoreMatchers
.
nullValue
;
import
static
org
.
hamcrest
.
CoreMatchers
.
startsWith
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
fail
;
import
static
org
.
springframework
.
http
.
HttpHeaders
.
IF_MODIFIED_SINCE
;
import
static
org
.
springframework
.
http
.
HttpHeaders
.
LAST_MODIFIED
;
import
static
org
.
springframework
.
http
.
HttpHeaders
.
VARY
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
header
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
...
...
@@ -95,6 +99,18 @@ public class HeaderAssertionTests {
.
andExpect
(
header
().
string
(
LAST_MODIFIED
,
equalTo
(
now
)));
}
@Test
public
void
multiStringHeaderValue
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/persons/1"
)).
andExpect
(
header
().
stringValues
(
VARY
,
"foo"
,
"bar"
));
}
@SuppressWarnings
(
"unchecked"
)
@Test
public
void
multiStringHeaderValueWithMatchers
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/persons/1"
))
.
andExpect
(
header
().
stringValues
(
VARY
,
hasItems
(
containsString
(
"foo"
),
startsWith
(
"bar"
))));
}
@Test
public
void
dateValueWithCorrectResponseHeaderValue
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/persons/1"
).
header
(
IF_MODIFIED_SINCE
,
minuteAgo
))
...
...
@@ -111,7 +127,7 @@ public class HeaderAssertionTests {
public
void
stringWithMissingResponseHeader
()
throws
Exception
{
this
.
mockMvc
.
perform
(
get
(
"/persons/1"
).
header
(
IF_MODIFIED_SINCE
,
now
))
.
andExpect
(
status
().
isNotModified
())
.
andExpect
(
header
().
string
(
"X-Custom-Header"
,
(
String
)
null
));
.
andExpect
(
header
().
string
Values
(
"X-Custom-Header"
));
}
@Test
...
...
src/asciidoc/whats-new.adoc
浏览文件 @
952a3170
...
...
@@ -672,3 +672,4 @@ Spring 4.3 also improves the caching abstraction as follows:
=== Testing Improvements
* The JUnit support in the _Spring TestContext Framework_ now requires JUnit 4.12 or higher.
* HeaderResultMatchers in Spring MVC Test supports expectations on multiple header values
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录