Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
c1a4f5c0
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,发现更多精彩内容 >>
提交
c1a4f5c0
编写于
1月 21, 2013
作者:
J
Juergen Hoeller
提交者:
unknown
1月 22, 2013
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MockHttpServletRequest's getParameter(Values) returns null for null parameter name
Issue: SPR-10192
上级
692ced80
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
54 addition
and
45 deletion
+54
-45
spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
.../org/springframework/mock/web/MockHttpServletRequest.java
+24
-19
spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java
...springframework/mock/web/MockHttpServletRequestTests.java
+9
-3
spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java
...springframework/mock/web/test/MockHttpServletRequest.java
+21
-23
未找到文件。
spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
浏览文件 @
c1a4f5c0
/*
* Copyright 2002-201
2
the original author or authors.
* Copyright 2002-201
3
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.
...
...
@@ -35,7 +35,6 @@ import java.util.List;
import
java.util.Locale
;
import
java.util.Map
;
import
java.util.Set
;
import
javax.servlet.RequestDispatcher
;
import
javax.servlet.ServletContext
;
import
javax.servlet.ServletException
;
...
...
@@ -97,8 +96,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
private
static
final
String
CHARSET_PREFIX
=
"charset="
;
private
boolean
active
=
true
;
// ---------------------------------------------------------------------
// ServletRequest properties
// ---------------------------------------------------------------------
...
...
@@ -140,6 +141,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
private
int
localPort
=
DEFAULT_SERVER_PORT
;
// ---------------------------------------------------------------------
// HttpServletRequest properties
// ---------------------------------------------------------------------
...
...
@@ -235,6 +237,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
this
.
locales
.
add
(
Locale
.
ENGLISH
);
}
// ---------------------------------------------------------------------
// Lifecycle methods
// ---------------------------------------------------------------------
...
...
@@ -279,6 +282,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
}
// ---------------------------------------------------------------------
// ServletRequest interface
// ---------------------------------------------------------------------
...
...
@@ -351,7 +355,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* parameter name, they will be replaced.
*/
public
void
setParameter
(
String
name
,
String
value
)
{
setParameter
(
name
,
new
String
[]
{
value
});
setParameter
(
name
,
new
String
[]
{
value
});
}
/**
...
...
@@ -373,7 +377,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
public
void
setParameters
(
Map
params
)
{
Assert
.
notNull
(
params
,
"Parameter map must not be null"
);
for
(
Object
key
:
params
.
keySet
())
{
Assert
.
isInstanceOf
(
String
.
class
,
key
,
"Parameter map key must be of type ["
+
String
.
class
.
getName
()
+
"]"
);
Assert
.
isInstanceOf
(
String
.
class
,
key
,
"Parameter map key must be of type ["
+
String
.
class
.
getName
()
+
"]"
);
Object
value
=
params
.
get
(
key
);
if
(
value
instanceof
String
)
{
this
.
setParameter
((
String
)
key
,
(
String
)
value
);
...
...
@@ -382,8 +387,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
this
.
setParameter
((
String
)
key
,
(
String
[])
value
);
}
else
{
throw
new
IllegalArgumentException
(
"Parameter map value must be single value "
+
" or array of type ["
+
String
.
class
.
getName
()
+
"]"
);
throw
new
IllegalArgumentException
(
"Parameter map value must be single value "
+
" or array of type ["
+
String
.
class
.
getName
()
+
"]"
);
}
}
}
...
...
@@ -394,7 +399,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* parameter name, the given value will be added to the end of the list.
*/
public
void
addParameter
(
String
name
,
String
value
)
{
addParameter
(
name
,
new
String
[]
{
value
});
addParameter
(
name
,
new
String
[]
{
value
});
}
/**
...
...
@@ -425,7 +430,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
public
void
addParameters
(
Map
params
)
{
Assert
.
notNull
(
params
,
"Parameter map must not be null"
);
for
(
Object
key
:
params
.
keySet
())
{
Assert
.
isInstanceOf
(
String
.
class
,
key
,
"Parameter map key must be of type ["
+
String
.
class
.
getName
()
+
"]"
);
Assert
.
isInstanceOf
(
String
.
class
,
key
,
"Parameter map key must be of type ["
+
String
.
class
.
getName
()
+
"]"
);
Object
value
=
params
.
get
(
key
);
if
(
value
instanceof
String
)
{
this
.
addParameter
((
String
)
key
,
(
String
)
value
);
...
...
@@ -434,8 +440,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
this
.
addParameter
((
String
)
key
,
(
String
[])
value
);
}
else
{
throw
new
IllegalArgumentException
(
"Parameter map value must be single value "
+
" or array of type ["
+
String
.
class
.
getName
()
+
"]"
);
throw
new
IllegalArgumentException
(
"Parameter map value must be single value "
+
" or array of type ["
+
String
.
class
.
getName
()
+
"]"
);
}
}
}
...
...
@@ -456,8 +462,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
public
String
getParameter
(
String
name
)
{
Assert
.
notNull
(
name
,
"Parameter name must not be null"
);
String
[]
arr
=
this
.
parameters
.
get
(
name
);
String
[]
arr
=
(
name
!=
null
?
this
.
parameters
.
get
(
name
)
:
null
);
return
(
arr
!=
null
&&
arr
.
length
>
0
?
arr
[
0
]
:
null
);
}
...
...
@@ -466,8 +471,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
public
String
[]
getParameterValues
(
String
name
)
{
Assert
.
notNull
(
name
,
"Parameter name must not be null"
);
return
this
.
parameters
.
get
(
name
);
return
(
name
!=
null
?
this
.
parameters
.
get
(
name
)
:
null
);
}
public
Map
<
String
,
String
[]>
getParameterMap
()
{
...
...
@@ -509,8 +513,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
public
BufferedReader
getReader
()
throws
UnsupportedEncodingException
{
if
(
this
.
content
!=
null
)
{
InputStream
sourceStream
=
new
ByteArrayInputStream
(
this
.
content
);
Reader
sourceReader
=
(
this
.
characterEncoding
!=
null
)
?
new
InputStreamReader
(
sourceStream
,
this
.
characterEncoding
)
:
new
InputStreamReader
(
sourceStream
);
Reader
sourceReader
=
(
this
.
characterEncoding
!=
null
)
?
new
InputStreamReader
(
sourceStream
,
this
.
characterEncoding
)
:
new
InputStreamReader
(
sourceStream
);
return
new
BufferedReader
(
sourceReader
);
}
else
{
...
...
@@ -574,7 +578,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* @since 3.2
*/
public
void
setPreferredLocales
(
List
<
Locale
>
locales
)
{
Assert
.
notEmpty
(
locales
,
"
preferred locales
list must not be empty"
);
Assert
.
notEmpty
(
locales
,
"
Locale
list must not be empty"
);
this
.
locales
.
clear
();
this
.
locales
.
addAll
(
locales
);
}
...
...
@@ -635,6 +639,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
return
this
.
localPort
;
}
// ---------------------------------------------------------------------
// HttpServletRequest interface
// ---------------------------------------------------------------------
...
...
@@ -797,8 +802,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
public
boolean
isUserInRole
(
String
role
)
{
return
(
this
.
userRoles
.
contains
(
role
)
||
(
this
.
servletContext
instanceof
MockServletContext
&&
((
MockServletContext
)
this
.
servletContext
).
getDeclaredRoles
().
contains
(
role
)));
return
(
this
.
userRoles
.
contains
(
role
)
||
(
this
.
servletContext
instanceof
MockServletContext
&&
((
MockServletContext
)
this
.
servletContext
).
getDeclaredRoles
().
contains
(
role
)));
}
public
void
setUserPrincipal
(
Principal
userPrincipal
)
{
...
...
spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java
浏览文件 @
c1a4f5c0
/*
* Copyright 2002-201
2
the original author or authors.
* Copyright 2002-201
3
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,8 +16,6 @@
package
org.springframework.mock.web
;
import
static
org
.
junit
.
Assert
.*;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Collections
;
...
...
@@ -29,6 +27,8 @@ import java.util.Map;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.*;
/**
* Unit tests for {@link MockHttpServletRequest}.
*
...
...
@@ -105,6 +105,12 @@ public class MockHttpServletRequestTests {
assertEquals
(
"HTTP header casing not being preserved"
,
headerName
,
requestHeaders
.
nextElement
());
}
@Test
public
void
nullParameterName
()
{
assertNull
(
request
.
getParameter
(
null
));
assertNull
(
request
.
getParameterValues
(
null
));
}
@Test
public
void
setMultipleParameters
()
{
request
.
setParameter
(
"key1"
,
"value1"
);
...
...
spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java
浏览文件 @
c1a4f5c0
/*
* Copyright 2002-201
2
the original author or authors.
* Copyright 2002-201
3
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.
...
...
@@ -36,7 +36,6 @@ import java.util.List;
import
java.util.Locale
;
import
java.util.Map
;
import
java.util.Set
;
import
javax.servlet.AsyncContext
;
import
javax.servlet.DispatcherType
;
import
javax.servlet.RequestDispatcher
;
...
...
@@ -110,9 +109,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
private
boolean
active
=
true
;
//---------------------------------------------------------------------
//
---------------------------------------------------------------------
// ServletRequest properties
//---------------------------------------------------------------------
//
---------------------------------------------------------------------
private
final
Map
<
String
,
Object
>
attributes
=
new
LinkedHashMap
<
String
,
Object
>();
...
...
@@ -151,11 +150,12 @@ public class MockHttpServletRequest implements HttpServletRequest {
private
int
localPort
=
DEFAULT_SERVER_PORT
;
private
Map
<
String
,
Part
>
parts
=
new
HashMap
<
String
,
Part
>();
private
final
Map
<
String
,
Part
>
parts
=
new
HashMap
<
String
,
Part
>();
//---------------------------------------------------------------------
// ---------------------------------------------------------------------
// HttpServletRequest properties
//---------------------------------------------------------------------
//
---------------------------------------------------------------------
private
String
authType
;
...
...
@@ -200,9 +200,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
private
DispatcherType
dispatcherType
=
DispatcherType
.
REQUEST
;
//---------------------------------------------------------------------
//
---------------------------------------------------------------------
// Constructors
//---------------------------------------------------------------------
//
---------------------------------------------------------------------
/**
* Create a new {@code MockHttpServletRequest} with a default
...
...
@@ -256,9 +256,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
this
.
locales
.
add
(
Locale
.
ENGLISH
);
}
//---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Lifecycle methods
//---------------------------------------------------------------------
//
---------------------------------------------------------------------
/**
* Return the ServletContext that this request is associated with. (Not
...
...
@@ -302,9 +303,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
//---------------------------------------------------------------------
//
---------------------------------------------------------------------
// ServletRequest interface
//---------------------------------------------------------------------
//
---------------------------------------------------------------------
@Override
public
Object
getAttribute
(
String
name
)
{
...
...
@@ -414,8 +415,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
else
{
throw
new
IllegalArgumentException
(
"Parameter map value must be single value "
+
" or array of type ["
+
String
.
class
.
getName
()
+
"]"
);
"Parameter map value must be single value "
+
" or array of type ["
+
String
.
class
.
getName
()
+
"]"
);
}
}
}
...
...
@@ -490,8 +490,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public
String
getParameter
(
String
name
)
{
Assert
.
notNull
(
name
,
"Parameter name must not be null"
);
String
[]
arr
=
this
.
parameters
.
get
(
name
);
String
[]
arr
=
(
name
!=
null
?
this
.
parameters
.
get
(
name
)
:
null
);
return
(
arr
!=
null
&&
arr
.
length
>
0
?
arr
[
0
]
:
null
);
}
...
...
@@ -502,8 +501,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public
String
[]
getParameterValues
(
String
name
)
{
Assert
.
notNull
(
name
,
"Parameter name must not be null"
);
return
this
.
parameters
.
get
(
name
);
return
(
name
!=
null
?
this
.
parameters
.
get
(
name
)
:
null
);
}
@Override
...
...
@@ -620,7 +618,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* @since 3.2
*/
public
void
setPreferredLocales
(
List
<
Locale
>
locales
)
{
Assert
.
notEmpty
(
locales
,
"
preferred locales
list must not be empty"
);
Assert
.
notEmpty
(
locales
,
"
Locale
list must not be empty"
);
this
.
locales
.
clear
();
this
.
locales
.
addAll
(
locales
);
}
...
...
@@ -779,7 +777,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public
String
getHeader
(
String
name
)
{
HeaderValueHolder
header
=
HeaderValueHolder
.
getByName
(
this
.
headers
,
name
);
return
(
header
!=
null
?
header
.
get
Value
().
toString
()
:
null
);
return
(
header
!=
null
?
header
.
get
StringValue
()
:
null
);
}
@Override
...
...
@@ -867,8 +865,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public
boolean
isUserInRole
(
String
role
)
{
return
(
this
.
userRoles
.
contains
(
role
)
||
(
this
.
servletContext
instanceof
MockServletContext
&&
((
MockServletContext
)
this
.
servletContext
).
getDeclaredRoles
().
contains
(
role
)));
return
(
this
.
userRoles
.
contains
(
role
)
||
(
this
.
servletContext
instanceof
MockServletContext
&&
((
MockServletContext
)
this
.
servletContext
).
getDeclaredRoles
().
contains
(
role
)));
}
public
void
setUserPrincipal
(
Principal
userPrincipal
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录