Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
54192cf5
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 搜索 >>
提交
54192cf5
编写于
3月 16, 2017
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
WebTestClient (server-less) setup accepts WebFilter's
Issue: SPR-15349
上级
a99fe3ed
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
101 addition
and
33 deletion
+101
-33
spring-test/src/main/java/org/springframework/test/web/reactive/server/AbstractMockServerSpec.java
...work/test/web/reactive/server/AbstractMockServerSpec.java
+33
-3
spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java
...ringframework/test/web/reactive/server/WebTestClient.java
+7
-0
spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ApplicationContextTests.java
...reactive/server/samples/bind/ApplicationContextTests.java
+30
-14
spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ControllerTests.java
...est/web/reactive/server/samples/bind/ControllerTests.java
+31
-16
未找到文件。
spring-test/src/main/java/org/springframework/test/web/reactive/server/AbstractMockServerSpec.java
浏览文件 @
54192cf5
...
...
@@ -16,10 +16,14 @@
package
org.springframework.test.web.reactive.server
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.function.UnaryOperator
;
import
org.springframework.http.server.reactive.HttpHandler
;
import
org.springframework.web.server.ServerWebExchange
;
import
org.springframework.web.server.WebFilter
;
import
org.springframework.web.server.adapter.WebHttpHandlerBuilder
;
/**
...
...
@@ -33,6 +37,13 @@ abstract class AbstractMockServerSpec<B extends WebTestClient.MockServerSpec<B>>
private
final
ExchangeMutatorWebFilter
exchangeMutatorFilter
=
new
ExchangeMutatorWebFilter
();
private
final
List
<
WebFilter
>
filters
=
new
ArrayList
<>(
4
);
AbstractMockServerSpec
()
{
this
.
filters
.
add
(
this
.
exchangeMutatorFilter
);
}
@Override
public
<
T
extends
B
>
T
exchangeMutator
(
UnaryOperator
<
ServerWebExchange
>
mutator
)
{
...
...
@@ -40,6 +51,12 @@ abstract class AbstractMockServerSpec<B extends WebTestClient.MockServerSpec<B>>
return
self
();
}
@Override
public
<
T
extends
B
>
T
webFilter
(
WebFilter
...
filter
)
{
this
.
filters
.
addAll
(
Arrays
.
asList
(
filter
));
return
self
();
}
@SuppressWarnings
(
"unchecked"
)
private
<
T
extends
B
>
T
self
()
{
return
(
T
)
this
;
...
...
@@ -48,12 +65,25 @@ abstract class AbstractMockServerSpec<B extends WebTestClient.MockServerSpec<B>>
@Override
public
WebTestClient
.
Builder
configureClient
()
{
HttpHandler
handler
=
initHttpHandlerBuilder
().
prependFilter
(
this
.
exchangeMutatorFilter
).
build
();
return
new
DefaultWebTestClientBuilder
(
handler
,
this
.
exchangeMutatorFilter
);
WebHttpHandlerBuilder
builder
=
initHttpHandlerBuilder
();
filtersInReverse
().
forEach
(
builder:
:
prependFilter
);
return
new
DefaultWebTestClientBuilder
(
builder
.
build
(),
this
.
exchangeMutatorFilter
);
}
/**
* Sub-classes to create the {@code WebHttpHandlerBuilder} to use.
*/
protected
abstract
WebHttpHandlerBuilder
initHttpHandlerBuilder
();
/**
* Return the filters in reverse order for pre-pending.
*/
private
List
<
WebFilter
>
filtersInReverse
()
{
List
<
WebFilter
>
result
=
new
ArrayList
<>(
this
.
filters
);
Collections
.
reverse
(
result
);
return
result
;
}
@Override
public
WebTestClient
build
()
{
return
configureClient
().
build
();
...
...
spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java
浏览文件 @
54192cf5
...
...
@@ -50,6 +50,7 @@ import org.springframework.web.reactive.function.client.ExchangeStrategies;
import
org.springframework.web.reactive.function.client.WebClient
;
import
org.springframework.web.reactive.function.server.RouterFunction
;
import
org.springframework.web.server.ServerWebExchange
;
import
org.springframework.web.server.WebFilter
;
import
org.springframework.web.util.UriBuilder
;
import
org.springframework.web.util.UriBuilderFactory
;
...
...
@@ -193,6 +194,12 @@ public interface WebTestClient {
*/
<
T
extends
B
>
T
exchangeMutator
(
UnaryOperator
<
ServerWebExchange
>
mutator
);
/**
* Configure {@link WebFilter}'s for server request processing.
* @param filter one or more filters
*/
<
T
extends
B
>
T
webFilter
(
WebFilter
...
filter
);
/**
* Proceed to configure and build the test client.
*/
...
...
spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ApplicationContextTests.java
浏览文件 @
54192cf5
...
...
@@ -32,9 +32,7 @@ import org.springframework.web.bind.annotation.RequestAttribute;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.reactive.config.EnableWebFlux
;
import
org.springframework.web.server.ServerWebExchange
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
org.springframework.web.server.WebFilter
;
/**
* Binding to server infrastructure declared in a Spring ApplicationContext.
...
...
@@ -56,24 +54,16 @@ public class ApplicationContextTests {
this
.
client
=
WebTestClient
.
bindToApplicationContext
(
context
)
.
exchangeMutator
(
principal
(
"Pablo"
))
.
webFilter
(
prefixFilter
(
"Mr."
))
.
build
();
}
private
UnaryOperator
<
ServerWebExchange
>
principal
(
String
userName
)
{
return
exchange
->
{
Principal
user
=
mock
(
Principal
.
class
);
when
(
user
.
getName
()).
thenReturn
(
userName
);
return
exchange
.
mutate
().
principal
(
Mono
.
just
(
user
)).
build
();
};
}
@Test
public
void
basic
()
throws
Exception
{
this
.
client
.
get
().
uri
(
"/principal"
)
.
exchange
()
.
expectStatus
().
isOk
()
.
expectBody
(
String
.
class
).
value
().
isEqualTo
(
"Hello Pablo!"
);
.
expectBody
(
String
.
class
).
value
().
isEqualTo
(
"Hello
Mr.
Pablo!"
);
}
@Test
...
...
@@ -82,7 +72,7 @@ public class ApplicationContextTests {
.
get
().
uri
(
"/principal"
)
.
exchange
()
.
expectStatus
().
isOk
()
.
expectBody
(
String
.
class
).
value
().
isEqualTo
(
"Hello Giovanni!"
);
.
expectBody
(
String
.
class
).
value
().
isEqualTo
(
"Hello
Mr.
Giovanni!"
);
}
@Test
...
...
@@ -96,6 +86,18 @@ public class ApplicationContextTests {
.
expectBody
(
String
.
class
).
value
().
isEqualTo
(
"foo+bar"
);
}
private
UnaryOperator
<
ServerWebExchange
>
principal
(
String
userName
)
{
return
exchange
->
exchange
.
mutate
().
principal
(
Mono
.
just
(
new
TestUser
(
userName
))).
build
();
}
private
WebFilter
prefixFilter
(
String
prefix
)
{
return
(
exchange
,
chain
)
->
{
Mono
<
Principal
>
user
=
exchange
.
getPrincipal
().
map
(
p
->
new
TestUser
(
prefix
+
" "
+
p
.
getName
()));
return
chain
.
filter
(
exchange
.
mutate
().
principal
(
user
).
build
());
};
}
private
UnaryOperator
<
ServerWebExchange
>
attribute
(
String
attrName
,
String
attrValue
)
{
return
exchange
->
{
exchange
.
getAttributes
().
put
(
attrName
,
attrValue
);
...
...
@@ -129,4 +131,18 @@ public class ApplicationContextTests {
}
}
private
static
class
TestUser
implements
Principal
{
private
final
String
name
;
TestUser
(
String
name
)
{
this
.
name
=
name
;
}
@Override
public
String
getName
()
{
return
this
.
name
;
}
}
}
spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ControllerTests.java
浏览文件 @
54192cf5
...
...
@@ -27,9 +27,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import
org.springframework.web.bind.annotation.RequestAttribute
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.server.ServerWebExchange
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
org.springframework.web.server.WebFilter
;
/**
* Bind to annotated controllers.
...
...
@@ -39,27 +37,18 @@ import static org.mockito.Mockito.when;
*/
public
class
ControllerTests
{
private
final
WebTestClient
client
=
WebTestClient
.
bindToController
(
new
TestController
())
private
final
WebTestClient
client
=
WebTestClient
.
bindToController
(
new
TestController
())
.
exchangeMutator
(
principal
(
"Pablo"
))
.
webFilter
(
prefixFilter
(
"Mr."
))
.
build
();
private
UnaryOperator
<
ServerWebExchange
>
principal
(
String
userName
)
{
return
exchange
->
{
Principal
user
=
mock
(
Principal
.
class
);
when
(
user
.
getName
()).
thenReturn
(
userName
);
return
exchange
.
mutate
().
principal
(
Mono
.
just
(
user
)).
build
();
};
}
@Test
public
void
basic
()
throws
Exception
{
this
.
client
.
get
().
uri
(
"/principal"
)
.
exchange
()
.
expectStatus
().
isOk
()
.
expectBody
(
String
.
class
).
value
().
isEqualTo
(
"Hello Pablo!"
);
.
expectBody
(
String
.
class
).
value
().
isEqualTo
(
"Hello
Mr.
Pablo!"
);
}
@Test
...
...
@@ -68,7 +57,7 @@ public class ControllerTests {
.
get
().
uri
(
"/principal"
)
.
exchange
()
.
expectStatus
().
isOk
()
.
expectBody
(
String
.
class
).
value
().
isEqualTo
(
"Hello Giovanni!"
);
.
expectBody
(
String
.
class
).
value
().
isEqualTo
(
"Hello
Mr.
Giovanni!"
);
}
@Test
...
...
@@ -82,6 +71,18 @@ public class ControllerTests {
.
expectBody
(
String
.
class
).
value
().
isEqualTo
(
"foo+bar"
);
}
private
UnaryOperator
<
ServerWebExchange
>
principal
(
String
userName
)
{
return
exchange
->
exchange
.
mutate
().
principal
(
Mono
.
just
(
new
TestUser
(
userName
))).
build
();
}
private
WebFilter
prefixFilter
(
String
prefix
)
{
return
(
exchange
,
chain
)
->
{
Mono
<
Principal
>
user
=
exchange
.
getPrincipal
().
map
(
p
->
new
TestUser
(
prefix
+
" "
+
p
.
getName
()));
return
chain
.
filter
(
exchange
.
mutate
().
principal
(
user
).
build
());
};
}
private
UnaryOperator
<
ServerWebExchange
>
attribute
(
String
attrName
,
String
attrValue
)
{
return
exchange
->
{
exchange
.
getAttributes
().
put
(
attrName
,
attrValue
);
...
...
@@ -104,4 +105,18 @@ public class ControllerTests {
}
}
private
static
class
TestUser
implements
Principal
{
private
final
String
name
;
TestUser
(
String
name
)
{
this
.
name
=
name
;
}
@Override
public
String
getName
()
{
return
this
.
name
;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录