Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Meiracle
spring-framework
提交
584dabbb
S
spring-framework
项目概览
Meiracle
/
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,发现更多精彩内容 >>
提交
584dabbb
编写于
1月 19, 2021
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add all non-file parts as request parameters
Closes gh-26400
上级
4779323d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
26 addition
and
32 deletion
+26
-32
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMultipartHttpServletRequestBuilder.java
...rvlet/request/MockMultipartHttpServletRequestBuilder.java
+22
-29
spring-test/src/test/java/org/springframework/test/web/servlet/request/MockMultipartHttpServletRequestBuilderTests.java
.../request/MockMultipartHttpServletRequestBuilderTests.java
+4
-3
未找到文件。
spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMultipartHttpServletRequestBuilder.java
浏览文件 @
584dabbb
/*
* Copyright 2002-202
0
the original author or authors.
* Copyright 2002-202
1
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.
...
...
@@ -17,6 +17,7 @@
package
org.springframework.test.web.servlet.request
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.net.URI
;
import
java.nio.charset.Charset
;
...
...
@@ -38,7 +39,6 @@ import org.springframework.util.Assert;
import
org.springframework.util.FileCopyUtils
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.web.multipart.MultipartFile
;
/**
* Default builder for {@link MockMultipartHttpServletRequest}.
...
...
@@ -144,47 +144,40 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
@Override
protected
final
MockHttpServletRequest
createServletRequest
(
ServletContext
servletContext
)
{
MockMultipartHttpServletRequest
request
=
new
MockMultipartHttpServletRequest
(
servletContext
);
Charset
defaultCharset
=
(
request
.
getCharacterEncoding
()
!=
null
?
Charset
.
forName
(
request
.
getCharacterEncoding
())
:
StandardCharsets
.
UTF_8
);
this
.
files
.
forEach
(
request:
:
addFile
);
this
.
parts
.
values
().
stream
().
flatMap
(
Collection:
:
stream
).
forEach
(
part
->
{
request
.
addPart
(
part
);
try
{
MultipartFile
file
=
asMultipartFile
(
part
);
if
(
file
!=
null
)
{
request
.
addFile
(
file
);
return
;
String
name
=
part
.
getName
();
String
filename
=
part
.
getSubmittedFileName
();
InputStream
is
=
part
.
getInputStream
();
if
(
filename
!=
null
)
{
request
.
addFile
(
new
MockMultipartFile
(
name
,
filename
,
part
.
getContentType
(),
is
));
}
String
value
=
toParameterValue
(
part
);
if
(
value
!=
null
)
{
request
.
addParameter
(
part
.
getName
(),
toParameterValue
(
part
));
else
{
InputStreamReader
reader
=
new
InputStreamReader
(
is
,
getCharsetOrDefault
(
part
,
defaultCharset
));
String
value
=
FileCopyUtils
.
copyToString
(
reader
);
request
.
addParameter
(
part
.
getName
(),
value
);
}
}
catch
(
IOException
ex
)
{
throw
new
IllegalStateException
(
"Failed to read content for part "
+
part
.
getName
(),
ex
);
}
});
return
request
;
}
@Nullable
private
MultipartFile
asMultipartFile
(
Part
part
)
throws
IOException
{
String
name
=
part
.
getName
();
String
filename
=
part
.
getSubmittedFileName
();
if
(
filename
!=
null
)
{
return
new
MockMultipartFile
(
name
,
filename
,
part
.
getContentType
(),
part
.
getInputStream
());
}
return
null
;
return
request
;
}
@Nullable
private
String
toParameterValue
(
Part
part
)
throws
IOException
{
String
rawType
=
part
.
getContentType
(
);
MediaType
mediaType
=
(
rawType
!=
null
?
MediaType
.
parseMediaType
(
rawType
)
:
MediaType
.
TEXT_PLAIN
);
if
(!
mediaType
.
isCompatibleWith
(
MediaType
.
TEXT_PLAIN
))
{
return
null
;
private
Charset
getCharsetOrDefault
(
Part
part
,
Charset
defaultCharset
)
{
if
(
part
.
getContentType
()
!=
null
)
{
MediaType
mediaType
=
MediaType
.
parseMediaType
(
part
.
getContentType
()
);
if
(
mediaType
.
getCharset
()
!=
null
)
{
return
mediaType
.
getCharset
();
}
}
Charset
charset
=
(
mediaType
.
getCharset
()
!=
null
?
mediaType
.
getCharset
()
:
StandardCharsets
.
UTF_8
);
InputStreamReader
reader
=
new
InputStreamReader
(
part
.
getInputStream
(),
charset
);
return
FileCopyUtils
.
copyToString
(
reader
);
return
defaultCharset
;
}
}
spring-test/src/test/java/org/springframework/test/web/servlet/request/MockMultipartHttpServletRequestBuilderTests.java
浏览文件 @
584dabbb
/*
* Copyright 2002-202
0
the original author or authors.
* Copyright 2002-202
1
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.
...
...
@@ -50,7 +50,7 @@ public class MockMultipartHttpServletRequestBuilderTests {
assertThat
(
mockRequest
.
getParts
()).
extracting
(
Part:
:
getName
).
containsExactly
(
"name"
);
}
@Test
// gh-26261
@Test
// gh-26261
, gh-26400
void
addFileWithoutFilename
()
throws
Exception
{
MockPart
jsonPart
=
new
MockPart
(
"data"
,
"{\"node\":\"node\"}"
.
getBytes
(
UTF_8
));
jsonPart
.
getHeaders
().
setContentType
(
MediaType
.
APPLICATION_JSON
);
...
...
@@ -62,7 +62,8 @@ public class MockMultipartHttpServletRequestBuilderTests {
.
buildRequest
(
new
MockServletContext
());
assertThat
(
mockRequest
.
getFileMap
()).
containsOnlyKeys
(
"file"
);
assertThat
(
mockRequest
.
getParameterMap
()).
isEmpty
();
assertThat
(
mockRequest
.
getParameterMap
()).
hasSize
(
1
);
assertThat
(
mockRequest
.
getParameter
(
"data"
)).
isEqualTo
(
"{\"node\":\"node\"}"
);
assertThat
(
mockRequest
.
getParts
()).
extracting
(
Part:
:
getName
).
containsExactly
(
"data"
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录