Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
6fa9871a
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 搜索 >>
提交
6fa9871a
编写于
11月 13, 2019
作者:
S
Sébastien Deleuze
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Provide orNull extensions for WebFlux ServerRequest
Closes gh-23761
上级
22211a01
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
146 addition
and
0 deletion
+146
-0
spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtensions.kt
...k/web/reactive/function/server/ServerRequestExtensions.kt
+56
-0
spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtensionsTests.kt
.../reactive/function/server/ServerRequestExtensionsTests.kt
+90
-0
未找到文件。
spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtensions.kt
浏览文件 @
6fa9871a
...
...
@@ -21,11 +21,14 @@ import kotlinx.coroutines.reactive.awaitFirstOrNull
import
kotlinx.coroutines.reactive.awaitSingle
import
kotlinx.coroutines.reactive.asFlow
import
org.springframework.core.ParameterizedTypeReference
import
org.springframework.http.MediaType
import
org.springframework.http.codec.multipart.Part
import
org.springframework.util.CollectionUtils
import
org.springframework.util.MultiValueMap
import
org.springframework.web.server.WebSession
import
reactor.core.publisher.Flux
import
reactor.core.publisher.Mono
import
java.net.InetSocketAddress
import
java.security.Principal
/**
...
...
@@ -112,3 +115,56 @@ suspend fun ServerRequest.awaitPrincipal(): Principal? =
*/
suspend
fun
ServerRequest
.
awaitSession
():
WebSession
=
session
().
awaitSingle
()
/**
* Nullable variant of [ServerRequest.remoteAddress]
*
* @author Sebastien Deleuze
* @since 5.2.2
*/
fun
ServerRequest
.
remoteAddressOrNull
():
InetSocketAddress
?
=
remoteAddress
().
orElse
(
null
)
/**
* Nullable variant of [ServerRequest.attribute]
*
* @author Sebastien Deleuze
* @since 5.2.2
*/
fun
ServerRequest
.
attributeOrNull
(
name
:
String
):
Any
?
=
attributes
()[
name
]
/**
* Nullable variant of [ServerRequest.queryParam]
*
* @author Sebastien Deleuze
* @since 5.2.2
*/
fun
ServerRequest
.
queryParamOrNull
(
name
:
String
):
String
?
{
val
queryParamValues
=
queryParams
()[
name
]
return
if
(
CollectionUtils
.
isEmpty
(
queryParamValues
))
{
null
}
else
{
var
value
:
String
?
=
queryParamValues
!!
[
0
]
if
(
value
==
null
)
{
value
=
""
}
value
}
}
/**
* Nullable variant of [ServerRequest.Headers.contentLength]
*
* @author Sebastien Deleuze
* @since 5.2.2
*/
fun
ServerRequest
.
Headers
.
contentLengthOrNull
():
Long
?
=
contentLength
().
run
{
if
(
isPresent
)
asLong
else
null
}
/**
* Nullable variant of [ServerRequest.Headers.contentType]
*
* @author Sebastien Deleuze
* @since 5.2.2
*/
fun
ServerRequest
.
Headers
.
contentTypeOrNull
():
MediaType
?
=
contentType
().
orElse
(
null
)
spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtensionsTests.kt
浏览文件 @
6fa9871a
...
...
@@ -23,11 +23,15 @@ import kotlinx.coroutines.runBlocking
import
org.assertj.core.api.Assertions.assertThat
import
org.junit.jupiter.api.Test
import
org.springframework.core.ParameterizedTypeReference
import
org.springframework.http.MediaType
import
org.springframework.http.codec.multipart.Part
import
org.springframework.util.CollectionUtils
import
org.springframework.util.MultiValueMap
import
org.springframework.web.server.WebSession
import
reactor.core.publisher.Mono
import
java.net.InetSocketAddress
import
java.security.Principal
import
java.util.*
/**
* Mock object based tests for [ServerRequest] Kotlin extensions.
...
...
@@ -38,6 +42,8 @@ class ServerRequestExtensionsTests {
val
request
=
mockk
<
ServerRequest
>(
relaxed
=
true
)
val
headers
=
mockk
<
ServerRequest
.
Headers
>(
relaxed
=
true
)
@Test
fun
`bodyToMono
with
reified
type
parameters`
()
{
request
.
bodyToMono
<
List
<
Foo
>>()
...
...
@@ -108,6 +114,90 @@ class ServerRequestExtensionsTests {
}
}
@Test
fun
`remoteAddressOrNull
with
value`
()
{
val
remoteAddress
=
InetSocketAddress
(
1234
)
every
{
request
.
remoteAddress
()
}
returns
Optional
.
of
(
remoteAddress
)
assertThat
(
remoteAddress
).
isEqualTo
(
request
.
remoteAddressOrNull
())
verify
{
request
.
remoteAddress
()
}
}
@Test
fun
`remoteAddressOrNull
with
null
`
()
{
every
{
request
.
remoteAddress
()
}
returns
Optional
.
empty
()
assertThat
(
request
.
remoteAddressOrNull
()).
isNull
()
verify
{
request
.
remoteAddress
()
}
}
@Test
fun
`attributeOrNull
with
value`
()
{
every
{
request
.
attributes
()
}
returns
mapOf
(
"foo"
to
"bar"
)
assertThat
(
request
.
attributeOrNull
(
"foo"
)).
isEqualTo
(
"bar"
)
verify
{
request
.
attributes
()
}
}
@Test
fun
`attributeOrNull
with
null
`
()
{
every
{
request
.
attributes
()
}
returns
mapOf
(
"foo"
to
"bar"
)
assertThat
(
request
.
attributeOrNull
(
"baz"
)).
isNull
()
verify
{
request
.
attributes
()
}
}
@Test
fun
`queryParamOrNull
with
value`
()
{
every
{
request
.
queryParams
()
}
returns
CollectionUtils
.
toMultiValueMap
(
mapOf
(
"foo"
to
listOf
(
"bar"
)))
assertThat
(
request
.
queryParamOrNull
(
"foo"
)).
isEqualTo
(
"bar"
)
verify
{
request
.
queryParams
()
}
}
@Test
fun
`queryParamOrNull
with
values`
()
{
every
{
request
.
queryParams
()
}
returns
CollectionUtils
.
toMultiValueMap
(
mapOf
(
"foo"
to
listOf
(
"bar"
,
"bar"
)))
assertThat
(
request
.
queryParamOrNull
(
"foo"
)).
isEqualTo
(
"bar"
)
verify
{
request
.
queryParams
()
}
}
@Test
fun
`queryParamOrNull
with
null
value`
()
{
every
{
request
.
queryParams
()
}
returns
CollectionUtils
.
toMultiValueMap
(
mapOf
(
"foo"
to
listOf
(
null
)))
assertThat
(
request
.
queryParamOrNull
(
"foo"
)).
isEqualTo
(
""
)
verify
{
request
.
queryParams
()
}
}
@Test
fun
`queryParamOrNull
with
null
`
()
{
every
{
request
.
queryParams
()
}
returns
CollectionUtils
.
toMultiValueMap
(
mapOf
(
"foo"
to
listOf
(
"bar"
)))
assertThat
(
request
.
queryParamOrNull
(
"baz"
)).
isNull
()
verify
{
request
.
queryParams
()
}
}
@Test
fun
`contentLengthOrNull
with
value`
()
{
every
{
headers
.
contentLength
()
}
returns
OptionalLong
.
of
(
123
)
assertThat
(
headers
.
contentLengthOrNull
()).
isEqualTo
(
123
)
verify
{
headers
.
contentLength
()
}
}
@Test
fun
`contentLengthOrNull
with
null
`
()
{
every
{
headers
.
contentLength
()
}
returns
OptionalLong
.
empty
()
assertThat
(
headers
.
contentLengthOrNull
()).
isNull
()
verify
{
headers
.
contentLength
()
}
}
@Test
fun
`contentTypeOrNull
with
value`
()
{
every
{
headers
.
contentType
()
}
returns
Optional
.
of
(
MediaType
.
APPLICATION_JSON
)
assertThat
(
headers
.
contentTypeOrNull
()).
isEqualTo
(
MediaType
.
APPLICATION_JSON
)
verify
{
headers
.
contentType
()
}
}
@Test
fun
`contentTypeOrNull
with
null
`
()
{
every
{
headers
.
contentType
()
}
returns
Optional
.
empty
()
assertThat
(
headers
.
contentTypeOrNull
()).
isNull
()
verify
{
headers
.
contentType
()
}
}
class
Foo
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录