Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
chenpeng_fei
spring-framework
提交
bba38b88
S
spring-framework
项目概览
chenpeng_fei
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
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,发现更多精彩内容 >>
提交
bba38b88
编写于
11月 01, 2014
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MockHttpServletRequestBuilder allows for specifying content type as String value
Issue: SPR-12405
上级
78459504
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
62 addition
and
37 deletion
+62
-37
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java
...st/web/servlet/request/MockHttpServletRequestBuilder.java
+15
-4
spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java
...b/servlet/request/MockHttpServletRequestBuilderTests.java
+47
-33
未找到文件。
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java
浏览文件 @
bba38b88
...
...
@@ -186,11 +186,22 @@ public class MockHttpServletRequestBuilder
/**
* Set the 'Content-Type' header of the request.
* @param
media
Type the content type
* @param
content
Type the content type
*/
public
MockHttpServletRequestBuilder
contentType
(
MediaType
mediaType
)
{
Assert
.
notNull
(
mediaType
,
"'contentType' must not be null"
);
this
.
contentType
=
mediaType
.
toString
();
public
MockHttpServletRequestBuilder
contentType
(
MediaType
contentType
)
{
Assert
.
notNull
(
contentType
,
"'contentType' must not be null"
);
this
.
contentType
=
contentType
.
toString
();
this
.
headers
.
set
(
"Content-Type"
,
this
.
contentType
);
return
this
;
}
/**
* Set the 'Content-Type' header of the request.
* @param contentType the content type
* @since 4.1.2
*/
public
MockHttpServletRequestBuilder
contentType
(
String
contentType
)
{
this
.
contentType
=
MediaType
.
parseMediaType
(
contentType
).
toString
();
this
.
headers
.
set
(
"Content-Type"
,
this
.
contentType
);
return
this
;
}
...
...
spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java
浏览文件 @
bba38b88
...
...
@@ -15,6 +15,7 @@
*/
package
org.springframework.test.web.servlet.request
;
import
java.io.IOException
;
import
java.security.Principal
;
import
java.util.Arrays
;
import
java.util.Collections
;
...
...
@@ -53,7 +54,7 @@ public class MockHttpServletRequestBuilderTests {
@Before
public
void
setUp
()
throws
Exception
{
public
void
setUp
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/foo/bar"
);
servletContext
=
new
MockServletContext
();
}
...
...
@@ -66,7 +67,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
uri
()
throws
Exception
{
public
void
uri
()
{
String
uri
=
"https://java.sun.com:8080/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)"
;
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
uri
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
@@ -81,7 +82,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
requestUriWithEncoding
()
throws
Exception
{
public
void
requestUriWithEncoding
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/foo bar"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
@@ -89,7 +90,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
contextPathEmpty
()
throws
Exception
{
public
void
contextPathEmpty
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/foo"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
@@ -100,7 +101,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
contextPathServletPathEmpty
()
throws
Exception
{
public
void
contextPathServletPathEmpty
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/travel/hotels/42"
);
this
.
builder
.
contextPath
(
"/travel"
);
...
...
@@ -112,7 +113,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
contextPathServletPath
()
throws
Exception
{
public
void
contextPathServletPath
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/travel/main/hotels/42"
);
this
.
builder
.
contextPath
(
"/travel"
);
this
.
builder
.
servletPath
(
"/main"
);
...
...
@@ -125,7 +126,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
contextPathServletPathInfoEmpty
()
throws
Exception
{
public
void
contextPathServletPathInfoEmpty
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/travel/hotels/42"
);
this
.
builder
.
contextPath
(
"/travel"
);
...
...
@@ -139,7 +140,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
contextPathServletPathInfo
()
throws
Exception
{
public
void
contextPathServletPathInfo
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/"
);
this
.
builder
.
servletPath
(
"/index.html"
);
this
.
builder
.
pathInfo
(
null
);
...
...
@@ -152,7 +153,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
contextPathServletPathInvalid
()
throws
Exception
{
public
void
contextPathServletPathInvalid
()
{
testContextPathServletPathInvalid
(
"/Foo"
,
""
,
"requestURI [/foo/bar] does not start with contextPath [/Foo]"
);
testContextPathServletPathInvalid
(
"foo"
,
""
,
"Context path must start with a '/'"
);
...
...
@@ -175,7 +176,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
requestUriAndFragment
()
throws
Exception
{
public
void
requestUriAndFragment
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/foo#bar"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
@@ -189,22 +190,22 @@ public class MockHttpServletRequestBuilderTests {
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
Map
<
String
,
String
[]>
parameterMap
=
request
.
getParameterMap
();
assertArrayEquals
(
new
String
[]{
"bar"
,
"baz"
},
parameterMap
.
get
(
"foo"
));
assertArrayEquals
(
new
String
[]
{
"bar"
,
"baz"
},
parameterMap
.
get
(
"foo"
));
}
@Test
public
void
requestParameterFromQuery
()
throws
Exception
{
public
void
requestParameterFromQuery
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/?foo=bar&foo=baz"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
Map
<
String
,
String
[]>
parameterMap
=
request
.
getParameterMap
();
assertArrayEquals
(
new
String
[]{
"bar"
,
"baz"
},
parameterMap
.
get
(
"foo"
));
assertArrayEquals
(
new
String
[]
{
"bar"
,
"baz"
},
parameterMap
.
get
(
"foo"
));
assertEquals
(
"foo=bar&foo=baz"
,
request
.
getQueryString
());
}
@Test
public
void
requestParameterFromQueryList
()
throws
Exception
{
public
void
requestParameterFromQueryList
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/?foo[0]=bar&foo[1]=baz"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
@@ -215,7 +216,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
requestParameterFromQueryWithEncoding
()
throws
Exception
{
public
void
requestParameterFromQueryWithEncoding
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/?foo={value}"
,
"bar=baz"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
@@ -227,18 +228,18 @@ public class MockHttpServletRequestBuilderTests {
// SPR-11043
@Test
public
void
requestParameterFromQueryNull
()
throws
Exception
{
public
void
requestParameterFromQueryNull
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/?foo"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
Map
<
String
,
String
[]>
parameterMap
=
request
.
getParameterMap
();
assertArrayEquals
(
new
String
[]{
null
},
parameterMap
.
get
(
"foo"
));
assertArrayEquals
(
new
String
[]
{
null
},
parameterMap
.
get
(
"foo"
));
assertEquals
(
"foo"
,
request
.
getQueryString
());
}
@Test
public
void
acceptHeader
()
throws
Exception
{
public
void
acceptHeader
()
{
this
.
builder
.
accept
(
MediaType
.
TEXT_HTML
,
MediaType
.
APPLICATION_XML
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
@@ -251,7 +252,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
contentType
()
throws
Exception
{
public
void
contentType
()
{
this
.
builder
.
contentType
(
MediaType
.
TEXT_HTML
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
@@ -263,10 +264,23 @@ public class MockHttpServletRequestBuilderTests {
assertEquals
(
"text/html"
,
contentTypes
.
get
(
0
));
}
@Test
public
void
contentTypeViaString
()
{
this
.
builder
.
contentType
(
"text/html"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
String
contentType
=
request
.
getContentType
();
List
<
String
>
contentTypes
=
Collections
.
list
(
request
.
getHeaders
(
"Content-Type"
));
assertEquals
(
"text/html"
,
contentType
);
assertEquals
(
1
,
contentTypes
.
size
());
assertEquals
(
"text/html"
,
contentTypes
.
get
(
0
));
}
// SPR-11308
@Test
public
void
contentTypeViaHeader
()
throws
Exception
{
public
void
contentTypeViaHeader
()
{
this
.
builder
.
header
(
"Content-Type"
,
MediaType
.
TEXT_HTML_VALUE
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
String
contentType
=
request
.
getContentType
();
...
...
@@ -277,7 +291,7 @@ public class MockHttpServletRequestBuilderTests {
// SPR-11308
@Test
public
void
contentTypeViaMultipleHeaderValues
()
throws
Exception
{
public
void
contentTypeViaMultipleHeaderValues
()
{
this
.
builder
.
header
(
"Content-Type"
,
MediaType
.
TEXT_HTML_VALUE
,
MediaType
.
ALL_VALUE
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
String
contentType
=
request
.
getContentType
();
...
...
@@ -286,7 +300,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
body
()
throws
Exception
{
public
void
body
()
throws
IO
Exception
{
byte
[]
body
=
"Hello World"
.
getBytes
(
"UTF-8"
);
this
.
builder
.
content
(
body
);
...
...
@@ -297,7 +311,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
header
()
throws
Exception
{
public
void
header
()
{
this
.
builder
.
header
(
"foo"
,
"bar"
,
"baz"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
@@ -309,7 +323,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
headers
()
throws
Exception
{
public
void
headers
()
{
HttpHeaders
httpHeaders
=
new
HttpHeaders
();
httpHeaders
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
httpHeaders
.
put
(
"foo"
,
Arrays
.
asList
(
"bar"
,
"baz"
));
...
...
@@ -325,7 +339,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
cookie
()
throws
Exception
{
public
void
cookie
()
{
Cookie
cookie1
=
new
Cookie
(
"foo"
,
"bar"
);
Cookie
cookie2
=
new
Cookie
(
"baz"
,
"qux"
);
this
.
builder
.
cookie
(
cookie1
,
cookie2
);
...
...
@@ -341,7 +355,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
locale
()
throws
Exception
{
public
void
locale
()
{
Locale
locale
=
new
Locale
(
"nl"
,
"nl"
);
this
.
builder
.
locale
(
locale
);
...
...
@@ -351,7 +365,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
characterEncoding
()
throws
Exception
{
public
void
characterEncoding
()
{
String
encoding
=
"UTF-8"
;
this
.
builder
.
characterEncoding
(
encoding
);
...
...
@@ -361,7 +375,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
requestAttribute
()
throws
Exception
{
public
void
requestAttribute
()
{
this
.
builder
.
requestAttr
(
"foo"
,
"bar"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
@@ -369,7 +383,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
sessionAttribute
()
throws
Exception
{
public
void
sessionAttribute
()
{
this
.
builder
.
sessionAttr
(
"foo"
,
"bar"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
@@ -377,7 +391,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
sessionAttributes
()
throws
Exception
{
public
void
sessionAttributes
()
{
Map
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>();
map
.
put
(
"foo"
,
"bar"
);
this
.
builder
.
sessionAttrs
(
map
);
...
...
@@ -388,7 +402,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
session
()
throws
Exception
{
public
void
session
()
{
MockHttpSession
session
=
new
MockHttpSession
(
this
.
servletContext
);
session
.
setAttribute
(
"foo"
,
"bar"
);
this
.
builder
.
session
(
session
);
...
...
@@ -402,7 +416,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
flashAttribute
()
throws
Exception
{
public
void
flashAttribute
()
{
this
.
builder
.
flashAttr
(
"foo"
,
"bar"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
@@ -412,7 +426,7 @@ public class MockHttpServletRequestBuilderTests {
}
@Test
public
void
principal
()
throws
Exception
{
public
void
principal
()
{
User
user
=
new
User
();
this
.
builder
.
principal
(
user
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录