Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dk131072
spring-framework
提交
1416c2a0
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,发现更多精彩内容 >>
提交
1416c2a0
编写于
1月 21, 2016
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ContentRequestMatchers asserts request with form data
Issue: SPR-13044
上级
2f6d86b7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
58 addition
and
3 deletion
+58
-3
spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java
...amework/test/web/client/match/ContentRequestMatchers.java
+32
-1
spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java
...rk/test/web/client/match/ContentRequestMatchersTests.java
+24
-1
src/asciidoc/whats-new.adoc
src/asciidoc/whats-new.adoc
+2
-1
未找到文件。
spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java
浏览文件 @
1416c2a0
/*
* Copyright 2002-201
5
the original author or authors.
* Copyright 2002-201
6
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.
...
...
@@ -16,22 +16,29 @@
package
org.springframework.test.web.client.match
;
import
java.io.ByteArrayInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
javax.xml.transform.Source
;
import
javax.xml.transform.dom.DOMSource
;
import
org.hamcrest.Matcher
;
import
org.w3c.dom.Node
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpInputMessage
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.client.ClientHttpRequest
;
import
org.springframework.http.converter.FormHttpMessageConverter
;
import
org.springframework.mock.http.client.MockClientHttpRequest
;
import
org.springframework.test.util.XmlExpectationsHelper
;
import
org.springframework.test.web.client.RequestMatcher
;
import
org.springframework.util.MultiValueMap
;
import
static
org
.
hamcrest
.
MatcherAssert
.*;
import
static
org
.
springframework
.
test
.
util
.
AssertionErrors
.*;
/**
* Factory for request content {@code RequestMatcher}'s. An instance of this
* class is typically accessed via {@link MockRestRequestMatchers#content()}.
...
...
@@ -136,6 +143,30 @@ public class ContentRequestMatchers {
};
}
/**
* Parse the body as form data and compare to the given {@code MultiValueMap}.
*/
public
RequestMatcher
formData
(
final
MultiValueMap
<
String
,
String
>
expectedContent
)
{
return
new
RequestMatcher
()
{
@Override
public
void
match
(
ClientHttpRequest
request
)
throws
IOException
,
AssertionError
{
HttpInputMessage
inputMessage
=
new
HttpInputMessage
()
{
@Override
public
InputStream
getBody
()
throws
IOException
{
MockClientHttpRequest
mockRequest
=
(
MockClientHttpRequest
)
request
;
return
new
ByteArrayInputStream
(
mockRequest
.
getBodyAsBytes
());
}
@Override
public
HttpHeaders
getHeaders
()
{
return
request
.
getHeaders
();
}
};
FormHttpMessageConverter
converter
=
new
FormHttpMessageConverter
();
assertEquals
(
"Request content"
,
expectedContent
,
converter
.
read
(
null
,
inputMessage
));
}
};
}
/**
* Parse the request body and the given String as XML and assert that the
* two are "similar" - i.e. they contain the same elements and attributes
...
...
spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java
浏览文件 @
1416c2a0
/*
* Copyright 2002-201
2
the original author or authors.
* Copyright 2002-201
6
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.
...
...
@@ -15,14 +15,19 @@
*/
package
org.springframework.test.web.client.match
;
import
java.nio.charset.Charset
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.http.MediaType
;
import
org.springframework.mock.http.client.MockClientHttpRequest
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
static
org
.
hamcrest
.
Matchers
.*;
/**
* Unit tests for {@link ContentRequestMatchers}.
*
...
...
@@ -32,11 +37,13 @@ public class ContentRequestMatchersTests {
private
MockClientHttpRequest
request
;
@Before
public
void
setUp
()
{
this
.
request
=
new
MockClientHttpRequest
();
}
@Test
public
void
testContentType
()
throws
Exception
{
this
.
request
.
getHeaders
().
setContentType
(
MediaType
.
APPLICATION_JSON
);
...
...
@@ -88,6 +95,22 @@ public class ContentRequestMatchersTests {
MockRestRequestMatchers
.
content
().
bytes
(
"Test"
.
getBytes
()).
match
(
this
.
request
);
}
@Test
public
void
testFormData
()
throws
Exception
{
String
contentType
=
"application/x-www-form-urlencoded;charset=UTF-8"
;
String
body
=
"name+1=value+1&name+2=value+A&name+2=value+B&name+3"
;
this
.
request
.
getHeaders
().
setContentType
(
MediaType
.
parseMediaType
(
contentType
));
this
.
request
.
getBody
().
write
(
body
.
getBytes
(
Charset
.
forName
(
"UTF-8"
)));
MultiValueMap
<
String
,
String
>
map
=
new
LinkedMultiValueMap
<>();
map
.
add
(
"name 1"
,
"value 1"
);
map
.
add
(
"name 2"
,
"value A"
);
map
.
add
(
"name 2"
,
"value B"
);
map
.
add
(
"name 3"
,
null
);
MockRestRequestMatchers
.
content
().
formData
(
map
).
match
(
this
.
request
);
}
@Test
public
void
testXml
()
throws
Exception
{
String
content
=
"<foo><bar>baz</bar><bar>bazz</bar></foo>"
;
...
...
src/asciidoc/whats-new.adoc
浏览文件 @
1416c2a0
...
...
@@ -672,4 +672,5 @@ 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
* Client-side Spring MVC Test supports expectations for form data in the request body.
* Server-side Spring MVC Test supports expectations on response headers with multiple values.
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录