Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
18db77da
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,发现更多精彩内容 >>
提交
18db77da
编写于
1月 21, 2016
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #927 from ksokol-improvement/SPR-13719
上级
9e16cbda
df223eb1
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
82 addition
and
43 deletion
+82
-43
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java
...st/web/servlet/request/MockHttpServletRequestBuilder.java
+20
-10
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.java
...work/test/web/servlet/request/MockMvcRequestBuilders.java
+40
-29
spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java
...b/servlet/request/MockHttpServletRequestBuilderTests.java
+22
-4
未找到文件。
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java
浏览文件 @
18db77da
/*
* 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.
...
...
@@ -60,16 +60,20 @@ import org.springframework.web.util.UriUtils;
*
* <p>Application tests will typically access this builder through the static factory
* methods in {@link MockMvcRequestBuilders}.
* <p>Although this class cannot be extended, additional ways to initialize
* the {@code MockHttpServletRequest} can be plugged in via
* {@link #with(RequestPostProcessor)}.
*
* @author Rossen Stoyanchev
* @author Arjen Poutsma
* @author Sam Brannen
* @author Kamill Sokol
* @since 3.2
*/
public
class
MockHttpServletRequestBuilder
implements
ConfigurableSmartRequestBuilder
<
MockHttpServletRequestBuilder
>,
Mergeable
{
private
final
HttpMethod
method
;
private
final
String
method
;
private
final
URI
url
;
...
...
@@ -119,27 +123,33 @@ public class MockHttpServletRequestBuilder
* @param vars zero or more URL variables
*/
MockHttpServletRequestBuilder
(
HttpMethod
httpMethod
,
String
url
,
Object
...
vars
)
{
this
(
httpMethod
,
UriComponentsBuilder
.
fromUriString
(
url
).
buildAndExpand
(
vars
).
encode
().
toUri
());
this
(
httpMethod
.
name
()
,
UriComponentsBuilder
.
fromUriString
(
url
).
buildAndExpand
(
vars
).
encode
().
toUri
());
}
/**
* Package private constructor. To get an instance, use static factory
* methods in {@link MockMvcRequestBuilders}.
* <p>Although this class cannot be extended, additional ways to initialize
* the {@code MockHttpServletRequest} can be plugged in via
* {@link #with(RequestPostProcessor)}.
* Alternative to {@link #MockHttpServletRequestBuilder(HttpMethod, String, Object...)}
* with a pre-built URI.
* @param httpMethod the HTTP method (GET, POST, etc)
* @param url the URL
* @since 4.0.3
*/
MockHttpServletRequestBuilder
(
HttpMethod
httpMethod
,
URI
url
)
{
this
(
httpMethod
.
name
(),
url
);
}
/**
* Alternative constructor for custom HTTP methods.
* @param httpMethod the HTTP method (GET, POST, etc)
* @param url the URL
* @since 4.3
*/
MockHttpServletRequestBuilder
(
String
httpMethod
,
URI
url
)
{
Assert
.
notNull
(
httpMethod
,
"httpMethod is required"
);
Assert
.
notNull
(
url
,
"url is required"
);
this
.
method
=
httpMethod
;
this
.
url
=
url
;
}
/**
* Add a request parameter to the {@link MockHttpServletRequest}.
* <p>If called more than once, new values get added to existing ones.
...
...
@@ -585,7 +595,7 @@ public class MockHttpServletRequestBuilder
request
.
setServerPort
(
this
.
url
.
getPort
());
}
request
.
setMethod
(
this
.
method
.
name
()
);
request
.
setMethod
(
this
.
method
);
for
(
String
name
:
this
.
headers
.
keySet
())
{
for
(
Object
value
:
this
.
headers
.
get
(
name
))
{
...
...
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.java
浏览文件 @
18db77da
/*
* 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.
...
...
@@ -42,6 +42,7 @@ import org.springframework.test.web.servlet.RequestBuilder;
* @author Greg Turnquist
* @author Sebastien Deleuze
* @author Sam Brannen
* @author Kamill Sokol
* @since 3.2
*/
public
abstract
class
MockMvcRequestBuilders
{
...
...
@@ -49,10 +50,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a GET request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVar
iable
s zero or more URL variables
* @param urlVars zero or more URL variables
*/
public
static
MockHttpServletRequestBuilder
get
(
String
urlTemplate
,
Object
...
urlVar
iable
s
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
urlTemplate
,
urlVar
iable
s
);
public
static
MockHttpServletRequestBuilder
get
(
String
urlTemplate
,
Object
...
urlVars
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
GET
,
urlTemplate
,
urlVars
);
}
/**
...
...
@@ -67,10 +68,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a POST request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVar
iable
s zero or more URL variables
* @param urlVars zero or more URL variables
*/
public
static
MockHttpServletRequestBuilder
post
(
String
urlTemplate
,
Object
...
urlVar
iable
s
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
POST
,
urlTemplate
,
urlVar
iable
s
);
public
static
MockHttpServletRequestBuilder
post
(
String
urlTemplate
,
Object
...
urlVars
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
POST
,
urlTemplate
,
urlVars
);
}
/**
...
...
@@ -85,10 +86,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a PUT request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVar
iable
s zero or more URL variables
* @param urlVars zero or more URL variables
*/
public
static
MockHttpServletRequestBuilder
put
(
String
urlTemplate
,
Object
...
urlVar
iable
s
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
PUT
,
urlTemplate
,
urlVar
iable
s
);
public
static
MockHttpServletRequestBuilder
put
(
String
urlTemplate
,
Object
...
urlVars
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
PUT
,
urlTemplate
,
urlVars
);
}
/**
...
...
@@ -103,10 +104,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a PATCH request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVar
iable
s zero or more URL variables
* @param urlVars zero or more URL variables
*/
public
static
MockHttpServletRequestBuilder
patch
(
String
urlTemplate
,
Object
...
urlVar
iable
s
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
PATCH
,
urlTemplate
,
urlVar
iable
s
);
public
static
MockHttpServletRequestBuilder
patch
(
String
urlTemplate
,
Object
...
urlVars
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
PATCH
,
urlTemplate
,
urlVars
);
}
/**
...
...
@@ -121,10 +122,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a DELETE request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVar
iable
s zero or more URL variables
* @param urlVars zero or more URL variables
*/
public
static
MockHttpServletRequestBuilder
delete
(
String
urlTemplate
,
Object
...
urlVar
iable
s
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
DELETE
,
urlTemplate
,
urlVar
iable
s
);
public
static
MockHttpServletRequestBuilder
delete
(
String
urlTemplate
,
Object
...
urlVars
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
DELETE
,
urlTemplate
,
urlVars
);
}
/**
...
...
@@ -139,10 +140,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for an OPTIONS request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVar
iable
s zero or more URL variables
* @param urlVars zero or more URL variables
*/
public
static
MockHttpServletRequestBuilder
options
(
String
urlTemplate
,
Object
...
urlVar
iable
s
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
OPTIONS
,
urlTemplate
,
urlVar
iable
s
);
public
static
MockHttpServletRequestBuilder
options
(
String
urlTemplate
,
Object
...
urlVars
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
OPTIONS
,
urlTemplate
,
urlVars
);
}
/**
...
...
@@ -157,11 +158,11 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a HEAD request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVar
iable
s zero or more URL variables
* @param urlVars zero or more URL variables
* @since 4.1
*/
public
static
MockHttpServletRequestBuilder
head
(
String
urlTemplate
,
Object
...
urlVar
iable
s
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
HEAD
,
urlTemplate
,
urlVar
iable
s
);
public
static
MockHttpServletRequestBuilder
head
(
String
urlTemplate
,
Object
...
urlVars
)
{
return
new
MockHttpServletRequestBuilder
(
HttpMethod
.
HEAD
,
urlTemplate
,
urlVars
);
}
/**
...
...
@@ -175,12 +176,12 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP method.
* @param
httpMethod the HTTP method
* @param
method the HTTP method (GET, POST, etc)
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVar
iable
s zero or more URL variables
* @param urlVars zero or more URL variables
*/
public
static
MockHttpServletRequestBuilder
request
(
HttpMethod
httpMethod
,
String
urlTemplate
,
Object
...
urlVariable
s
)
{
return
new
MockHttpServletRequestBuilder
(
httpMethod
,
urlTemplate
,
urlVariable
s
);
public
static
MockHttpServletRequestBuilder
request
(
HttpMethod
method
,
String
urlTemplate
,
Object
...
urlVar
s
)
{
return
new
MockHttpServletRequestBuilder
(
method
,
urlTemplate
,
urlVar
s
);
}
/**
...
...
@@ -193,13 +194,23 @@ public abstract class MockMvcRequestBuilders {
return
new
MockHttpServletRequestBuilder
(
httpMethod
,
uri
);
}
/**
* Alternative factory method that allows for custom HTTP verbs (e.g. WebDAV).
* @param httpMethod the HTTP method
* @param uri the URL
* @since 4.3
*/
public
static
MockHttpServletRequestBuilder
request
(
String
httpMethod
,
URI
uri
)
{
return
new
MockHttpServletRequestBuilder
(
httpMethod
,
uri
);
}
/**
* Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVar
iable
s zero or more URL variables
* @param urlVars zero or more URL variables
*/
public
static
MockMultipartHttpServletRequestBuilder
fileUpload
(
String
urlTemplate
,
Object
...
urlVar
iable
s
)
{
return
new
MockMultipartHttpServletRequestBuilder
(
urlTemplate
,
urlVar
iable
s
);
public
static
MockMultipartHttpServletRequestBuilder
fileUpload
(
String
urlTemplate
,
Object
...
urlVars
)
{
return
new
MockMultipartHttpServletRequestBuilder
(
urlTemplate
,
urlVars
);
}
/**
...
...
spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java
浏览文件 @
18db77da
/*
* 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.
...
...
@@ -25,7 +25,6 @@ import java.util.HashMap;
import
java.util.List
;
import
java.util.Locale
;
import
java.util.Map
;
import
javax.servlet.ServletContext
;
import
javax.servlet.http.Cookie
;
...
...
@@ -43,8 +42,12 @@ import org.springframework.util.LinkedMultiValueMap;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.web.servlet.FlashMap
;
import
org.springframework.web.servlet.support.SessionFlashMapManager
;
import
org.springframework.web.util.UriComponentsBuilder
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
junit
.
Assert
.
assertArrayEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
/**
* Unit tests for building a {@link MockHttpServletRequest} with
...
...
@@ -429,7 +432,7 @@ public class MockHttpServletRequestBuilderTests {
@Test
public
void
sessionAttributes
()
{
Map
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>();
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"foo"
,
"bar"
);
this
.
builder
.
sessionAttrs
(
map
);
...
...
@@ -472,6 +475,7 @@ public class MockHttpServletRequestBuilderTests {
}
// SPR-12945
@Test
public
void
mergeInvokesDefaultRequestPostProcessorFirst
()
{
final
String
ATTR
=
"ATTR"
;
...
...
@@ -492,6 +496,20 @@ public class MockHttpServletRequestBuilderTests {
assertEquals
(
EXEPCTED
,
request
.
getAttribute
(
ATTR
));
}
// SPR-13719
@Test
public
void
arbitraryMethod
()
{
String
httpMethod
=
"REPort"
;
URI
url
=
UriComponentsBuilder
.
fromPath
(
"/foo/{bar}"
).
buildAndExpand
(
42
).
toUri
();
this
.
builder
=
new
MockHttpServletRequestBuilder
(
httpMethod
,
url
);
MockHttpServletRequest
request
=
this
.
builder
.
buildRequest
(
this
.
servletContext
);
assertEquals
(
httpMethod
,
request
.
getMethod
());
assertEquals
(
"/foo/42"
,
request
.
getPathInfo
());
}
private
final
class
User
implements
Principal
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录