Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
a0cdbf51
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,体验更适合开发者的 AI 搜索 >>
提交
a0cdbf51
编写于
11月 12, 2019
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #23980
Closes gh-23980
上级
a26d37b4
16c7a40c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
109 addition
and
7 deletion
+109
-7
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java
...st/web/servlet/request/MockHttpServletRequestBuilder.java
+67
-7
spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java
...b/servlet/request/MockHttpServletRequestBuilderTests.java
+42
-0
未找到文件。
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java
浏览文件 @
a0cdbf51
...
...
@@ -119,6 +119,8 @@ public class MockHttpServletRequestBuilder
private
final
MultiValueMap
<
String
,
String
>
parameters
=
new
LinkedMultiValueMap
<>();
private
final
MultiValueMap
<
String
,
String
>
queryParams
=
new
LinkedMultiValueMap
<>();
private
final
List
<
Cookie
>
cookies
=
new
ArrayList
<>();
private
final
List
<
Locale
>
locales
=
new
ArrayList
<>();
...
...
@@ -248,6 +250,10 @@ public class MockHttpServletRequestBuilder
/**
* Set the request body.
* <p>If content is provided and {@link #contentType(MediaType)} is set to
* {@code application/x-www-form-urlencoded}, the content will be parsed
* and used to populate the {@link #param(String, String...) request
* parameters} map.
* @param content the body content
*/
public
MockHttpServletRequestBuilder
content
(
byte
[]
content
)
{
...
...
@@ -257,6 +263,10 @@ public class MockHttpServletRequestBuilder
/**
* Set the request body as a UTF-8 String.
* <p>If content is provided and {@link #contentType(MediaType)} is set to
* {@code application/x-www-form-urlencoded}, the content will be parsed
* and used to populate the {@link #param(String, String...) request
* parameters} map.
* @param content the body content
*/
public
MockHttpServletRequestBuilder
content
(
String
content
)
{
...
...
@@ -266,6 +276,10 @@ public class MockHttpServletRequestBuilder
/**
* Set the 'Content-Type' header of the request.
* <p>If content is provided and {@code contentType} is set to
* {@code application/x-www-form-urlencoded}, the content will be parsed
* and used to populate the {@link #param(String, String...) request
* parameters} map.
* @param contentType the content type
*/
public
MockHttpServletRequestBuilder
contentType
(
MediaType
contentType
)
{
...
...
@@ -328,8 +342,18 @@ public class MockHttpServletRequestBuilder
}
/**
* Add a request parameter to the {@link MockHttpServletRequest}.
* <p>If called more than once, new values get added to existing ones.
* Add a request parameter to {@link MockHttpServletRequest#getParameterMap()}.
* <p>In the Servlet API, a request parameter may be parsed from the query
* string and/or from the body of an {@code application/x-www-form-urlencoded}
* request. This method simply adds to the request parameter map. You may
* also use add Servlet request parameters by specifying the query or form
* data through one of the following:
* <ul>
* <li>Supply a URL with a query to {@link MockMvcRequestBuilders}.
* <li>Add query params via {@link #queryParam} or {@link #queryParams}.
* <li>Provide {@link #content} with {@link #contentType}
* {@code application/x-www-form-urlencoded}.
* </ul>
* @param name the parameter name
* @param values one or more values
*/
...
...
@@ -339,9 +363,7 @@ public class MockHttpServletRequestBuilder
}
/**
* Add a map of request parameters to the {@link MockHttpServletRequest},
* for example when testing a form submission.
* <p>If called more than once, new values get added to existing ones.
* Variant of {@link #param(String, String...)} with a {@link MultiValueMap}.
* @param params the parameters to add
* @since 4.2.4
*/
...
...
@@ -354,6 +376,33 @@ public class MockHttpServletRequestBuilder
return
this
;
}
/**
* Append to the query string and also add to the
* {@link #param(String, String...) request parameters} map. The parameter
* name and value are encoded when they are added to the query string.
* @param name the parameter name
* @param values one or more values
* @since 5.2.2
*/
public
MockHttpServletRequestBuilder
queryParam
(
String
name
,
String
...
values
)
{
param
(
name
,
values
);
this
.
queryParams
.
addAll
(
name
,
Arrays
.
asList
(
values
));
return
this
;
}
/**
* Append to the query string and also add to the
* {@link #params(MultiValueMap)} request parameters} map. The parameter
* name and value are encoded when they are added to the query string.
* @param params the parameters to add
* @since 5.2.2
*/
public
MockHttpServletRequestBuilder
queryParams
(
MultiValueMap
<
String
,
String
>
params
)
{
params
(
params
);
this
.
queryParams
.
addAll
(
params
);
return
this
;
}
/**
* Add the given cookies to the request. Cookies are always added.
* @param cookies the cookies to add
...
...
@@ -543,6 +592,12 @@ public class MockHttpServletRequestBuilder
this
.
parameters
.
put
(
paramName
,
entry
.
getValue
());
}
}
for
(
Map
.
Entry
<
String
,
List
<
String
>>
entry
:
parentBuilder
.
queryParams
.
entrySet
())
{
String
paramName
=
entry
.
getKey
();
if
(!
this
.
queryParams
.
containsKey
(
paramName
))
{
this
.
queryParams
.
put
(
paramName
,
entry
.
getValue
());
}
}
for
(
Cookie
cookie
:
parentBuilder
.
cookies
)
{
if
(!
containsCookie
(
cookie
))
{
this
.
cookies
.
add
(
cookie
);
...
...
@@ -632,8 +687,13 @@ public class MockHttpServletRequestBuilder
}
});
if
(
this
.
url
.
getRawQuery
()
!=
null
)
{
request
.
setQueryString
(
this
.
url
.
getRawQuery
());
String
query
=
this
.
url
.
getRawQuery
();
if
(!
this
.
queryParams
.
isEmpty
())
{
String
s
=
UriComponentsBuilder
.
newInstance
().
queryParams
(
this
.
queryParams
).
build
().
encode
().
getQuery
();
query
=
StringUtils
.
isEmpty
(
query
)
?
s
:
query
+
"&"
+
s
;
}
if
(
query
!=
null
)
{
request
.
setQueryString
(
query
);
}
addRequestParams
(
request
,
UriComponentsBuilder
.
fromUri
(
this
.
url
).
build
().
getQueryParams
());
...
...
spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java
浏览文件 @
a0cdbf51
...
...
@@ -21,6 +21,7 @@ import java.net.URI;
import
java.net.URISyntaxException
;
import
java.nio.charset.StandardCharsets
;
import
java.security.Principal
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.HashMap
;
...
...
@@ -234,6 +235,47 @@ public class MockHttpServletRequestBuilderTests {
assertThat
(
request
.
getParameter
(
"foo[1]"
)).
isEqualTo
(
"baz"
);
}
@Test
public
void
queryParameter
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/"
);
this
.
builder
.
queryParam
(
"foo"
,
"bar"
);
this
.
builder
.
queryParam
(
"foo"
,
"baz"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
assertThat
(
request
.
getParameterMap
().
get
(
"foo"
)).
isEqualTo
(
new
String
[]
{
"bar"
,
"baz"
});
assertThat
(
request
.
getQueryString
()).
isEqualTo
(
"foo=bar&foo=baz"
);
}
@Test
public
void
queryParameterMap
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/"
);
MultiValueMap
<
String
,
String
>
queryParams
=
new
LinkedMultiValueMap
<>();
List
<
String
>
values
=
new
ArrayList
<>();
values
.
add
(
"bar"
);
values
.
add
(
"baz"
);
queryParams
.
put
(
"foo"
,
values
);
this
.
builder
.
queryParams
(
queryParams
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
assertThat
(
request
.
getParameterMap
().
get
(
"foo"
)).
isEqualTo
(
new
String
[]
{
"bar"
,
"baz"
});
assertThat
(
request
.
getQueryString
()).
isEqualTo
(
"foo=bar&foo=baz"
);
}
@Test
public
void
queryParameterList
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/"
);
this
.
builder
.
queryParam
(
"foo[0]"
,
"bar"
);
this
.
builder
.
queryParam
(
"foo[1]"
,
"baz"
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
assertThat
(
request
.
getQueryString
()).
isEqualTo
(
"foo%5B0%5D=bar&foo%5B1%5D=baz"
);
assertThat
(
request
.
getParameter
(
"foo[0]"
)).
isEqualTo
(
"bar"
);
assertThat
(
request
.
getParameter
(
"foo[1]"
)).
isEqualTo
(
"baz"
);
}
@Test
public
void
requestParameterFromQueryWithEncoding
()
{
this
.
builder
=
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
"/?foo={value}"
,
"bar=baz"
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录