Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
sesemomo
spring-framework
提交
eef592d9
S
spring-framework
项目概览
sesemomo
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
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,发现更多精彩内容 >>
提交
eef592d9
编写于
5月 10, 2018
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add builder to MockServerWebExchange
Issue: SPR-16772
上级
d82b0e37
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
242 addition
and
185 deletion
+242
-185
spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java
...pringframework/mock/web/server/MockServerWebExchange.java
+81
-10
spring-web/src/test/java/org/springframework/mock/web/test/server/MockServerWebExchange.java
...framework/mock/web/test/server/MockServerWebExchange.java
+81
-10
spring-web/src/test/java/org/springframework/web/server/session/DefaultWebSessionManagerTests.java
...ork/web/server/session/DefaultWebSessionManagerTests.java
+44
-50
spring-web/src/test/java/org/springframework/web/server/session/InMemoryWebSessionStoreTests.java
...work/web/server/session/InMemoryWebSessionStoreTests.java
+3
-5
spring-web/src/test/java/org/springframework/web/server/session/MockWebSessionManager.java
...ngframework/web/server/session/MockWebSessionManager.java
+0
-47
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeArgumentResolverTests.java
...od/annotation/ServerWebExchangeArgumentResolverTests.java
+5
-4
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributeMethodArgumentResolverTests.java
...notation/SessionAttributeMethodArgumentResolverTests.java
+11
-29
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributesHandlerTests.java
...sult/method/annotation/SessionAttributesHandlerTests.java
+10
-15
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/WebSessionArgumentResolverTests.java
...lt/method/annotation/WebSessionArgumentResolverTests.java
+7
-15
未找到文件。
spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java
浏览文件 @
eef592d9
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
8
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.
...
...
@@ -15,15 +15,20 @@
*/
package
org.springframework.mock.web.server
;
import
reactor.core.publisher.Mono
;
import
org.springframework.http.codec.ServerCodecConfigurer
;
import
org.springframework.lang.Nullable
;
import
org.springframework.mock.http.server.reactive.MockServerHttpRequest
;
import
org.springframework.mock.http.server.reactive.MockServerHttpResponse
;
import
org.springframework.web.server.WebSession
;
import
org.springframework.web.server.adapter.DefaultServerWebExchange
;
import
org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver
;
import
org.springframework.web.server.session.DefaultWebSessionManager
;
import
org.springframework.web.server.session.WebSessionManager
;
/**
*
Variant of {@link DefaultServerWebExchange} for use in tests
with
*
Extension of {@link DefaultServerWebExchange} for use in tests, along
with
* {@link MockServerHttpRequest} and {@link MockServerHttpResponse}.
*
* <p>See static factory methods to create an instance.
...
...
@@ -34,8 +39,8 @@ import org.springframework.web.server.session.DefaultWebSessionManager;
public
final
class
MockServerWebExchange
extends
DefaultServerWebExchange
{
private
MockServerWebExchange
(
MockServerHttpRequest
request
)
{
super
(
request
,
new
MockServerHttpResponse
(),
new
DefaultWebSessionManager
()
,
private
MockServerWebExchange
(
MockServerHttpRequest
request
,
WebSessionManager
sessionManager
)
{
super
(
request
,
new
MockServerHttpResponse
(),
sessionManager
,
ServerCodecConfigurer
.
create
(),
new
AcceptHeaderLocaleContextResolver
());
}
...
...
@@ -47,22 +52,88 @@ public final class MockServerWebExchange extends DefaultServerWebExchange {
/**
* Create a {@link MockServerWebExchange} from the given request.
* Create a {@link MockServerWebExchange} from the given
mock
request.
* @param request the request to use.
* @return the exchange
*/
public
static
MockServerWebExchange
from
(
MockServerHttpRequest
request
)
{
return
new
MockServerWebExchange
(
request
);
return
builder
(
request
).
build
(
);
}
/**
* A variant of {@link #from(MockServerHttpRequest)} that accepts a request
* builder. Internally invokes the {@code build()} to build the request.
* @param requestBuilder the builder for the request.
* Variant of {@link #from(MockServerHttpRequest)} with a mock request builder.
* @param requestBuilder the builder for the mock request.
* @return the exchange
*/
public
static
MockServerWebExchange
from
(
MockServerHttpRequest
.
BaseBuilder
<?>
requestBuilder
)
{
return
new
MockServerWebExchange
(
requestBuilder
.
build
());
return
builder
(
requestBuilder
).
build
();
}
/**
* Create a {@link Builder} starting with the given mock request.
* @param request the request to use.
* @return the exchange builder
* @since 5.1
*/
public
static
MockServerWebExchange
.
Builder
builder
(
MockServerHttpRequest
request
)
{
return
new
MockServerWebExchange
.
Builder
(
request
);
}
/**
* Variant of {@link #builder(MockServerHttpRequest)} with a mock request builder.
* @param requestBuilder the builder for the mock request.
* @return the exchange builder
* @since 5.1
*/
public
static
MockServerWebExchange
.
Builder
builder
(
MockServerHttpRequest
.
BaseBuilder
<?>
requestBuilder
)
{
return
new
MockServerWebExchange
.
Builder
(
requestBuilder
.
build
());
}
/**
* Builder for a {@link MockServerWebExchange}.
* @since 5.1
*/
public
static
class
Builder
{
private
final
MockServerHttpRequest
request
;
@Nullable
private
WebSessionManager
sessionManager
;
public
Builder
(
MockServerHttpRequest
request
)
{
this
.
request
=
request
;
}
/**
* Set the session to use for the exchange.
* <p>This is mutually exclusive with {@link #sessionManager(WebSessionManager)}.
* @param session the session to use
*/
public
Builder
session
(
WebSession
session
)
{
this
.
sessionManager
=
exchange
->
Mono
.
just
(
session
);
return
this
;
}
/**
* Provide a {@code WebSessionManager} instance to use with the exchange.
* <p>This is mutually exclusive with {@link #session(WebSession)}.
* @param sessionManager the session manager to use
*/
public
Builder
sessionManager
(
WebSessionManager
sessionManager
)
{
this
.
sessionManager
=
sessionManager
;
return
this
;
}
/**
* Build the {@code MockServerWebExchange} instance.
*/
public
MockServerWebExchange
build
()
{
return
new
MockServerWebExchange
(
this
.
request
,
this
.
sessionManager
!=
null
?
this
.
sessionManager
:
new
DefaultWebSessionManager
());
}
}
}
spring-web/src/test/java/org/springframework/mock/web/test/server/MockServerWebExchange.java
浏览文件 @
eef592d9
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
8
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.
...
...
@@ -15,15 +15,20 @@
*/
package
org.springframework.mock.web.test.server
;
import
reactor.core.publisher.Mono
;
import
org.springframework.http.codec.ServerCodecConfigurer
;
import
org.springframework.lang.Nullable
;
import
org.springframework.mock.http.server.reactive.test.MockServerHttpRequest
;
import
org.springframework.mock.http.server.reactive.test.MockServerHttpResponse
;
import
org.springframework.web.server.WebSession
;
import
org.springframework.web.server.adapter.DefaultServerWebExchange
;
import
org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver
;
import
org.springframework.web.server.session.DefaultWebSessionManager
;
import
org.springframework.web.server.session.WebSessionManager
;
/**
*
Variant of {@link DefaultServerWebExchange} for use in tests
with
*
Extension of {@link DefaultServerWebExchange} for use in tests, along
with
* {@link MockServerHttpRequest} and {@link MockServerHttpResponse}.
*
* <p>See static factory methods to create an instance.
...
...
@@ -34,8 +39,8 @@ import org.springframework.web.server.session.DefaultWebSessionManager;
public
final
class
MockServerWebExchange
extends
DefaultServerWebExchange
{
private
MockServerWebExchange
(
MockServerHttpRequest
request
)
{
super
(
request
,
new
MockServerHttpResponse
(),
new
DefaultWebSessionManager
()
,
private
MockServerWebExchange
(
MockServerHttpRequest
request
,
WebSessionManager
sessionManager
)
{
super
(
request
,
new
MockServerHttpResponse
(),
sessionManager
,
ServerCodecConfigurer
.
create
(),
new
AcceptHeaderLocaleContextResolver
());
}
...
...
@@ -47,22 +52,88 @@ public final class MockServerWebExchange extends DefaultServerWebExchange {
/**
* Create a {@link MockServerWebExchange} from the given request.
* Create a {@link MockServerWebExchange} from the given
mock
request.
* @param request the request to use.
* @return the exchange
*/
public
static
MockServerWebExchange
from
(
MockServerHttpRequest
request
)
{
return
new
MockServerWebExchange
(
request
);
return
builder
(
request
).
build
(
);
}
/**
* A variant of {@link #from(MockServerHttpRequest)} that accepts a request
* builder. Internally invokes the {@code build()} to build the request.
* @param requestBuilder the builder for the request.
* Variant of {@link #from(MockServerHttpRequest)} with a mock request builder.
* @param requestBuilder the builder for the mock request.
* @return the exchange
*/
public
static
MockServerWebExchange
from
(
MockServerHttpRequest
.
BaseBuilder
<?>
requestBuilder
)
{
return
new
MockServerWebExchange
(
requestBuilder
.
build
());
return
builder
(
requestBuilder
).
build
();
}
/**
* Create a {@link Builder} starting with the given mock request.
* @param request the request to use.
* @return the exchange builder
* @since 5.1
*/
public
static
MockServerWebExchange
.
Builder
builder
(
MockServerHttpRequest
request
)
{
return
new
MockServerWebExchange
.
Builder
(
request
);
}
/**
* Variant of {@link #builder(MockServerHttpRequest)} with a mock request builder.
* @param requestBuilder the builder for the mock request.
* @return the exchange builder
* @since 5.1
*/
public
static
MockServerWebExchange
.
Builder
builder
(
MockServerHttpRequest
.
BaseBuilder
<?>
requestBuilder
)
{
return
new
MockServerWebExchange
.
Builder
(
requestBuilder
.
build
());
}
/**
* Builder for a {@link MockServerWebExchange}.
* @since 5.1
*/
public
static
class
Builder
{
private
final
MockServerHttpRequest
request
;
@Nullable
private
WebSessionManager
sessionManager
;
public
Builder
(
MockServerHttpRequest
request
)
{
this
.
request
=
request
;
}
/**
* Set the session to use for the exchange.
* <p>This is mutually exclusive with {@link #sessionManager(WebSessionManager)}.
* @param session the session to use
*/
public
Builder
session
(
WebSession
session
)
{
this
.
sessionManager
=
exchange
->
Mono
.
just
(
session
);
return
this
;
}
/**
* Provide a {@code WebSessionManager} instance to use with the exchange.
* <p>This is mutually exclusive with {@link #session(WebSession)}.
* @param sessionManager the session manager to use
*/
public
Builder
sessionManager
(
WebSessionManager
sessionManager
)
{
this
.
sessionManager
=
sessionManager
;
return
this
;
}
/**
* Build the {@code MockServerWebExchange} instance.
*/
public
MockServerWebExchange
build
()
{
return
new
MockServerWebExchange
(
this
.
request
,
this
.
sessionManager
!=
null
?
this
.
sessionManager
:
new
DefaultWebSessionManager
());
}
}
}
spring-web/src/test/java/org/springframework/web/server/session/DefaultWebSessionManagerTests.java
浏览文件 @
eef592d9
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
8
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.web.server.session;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.List
;
import
org.junit.Before
;
import
org.junit.Test
;
...
...
@@ -33,14 +34,10 @@ import org.springframework.web.server.WebSession;
import
org.springframework.web.server.adapter.DefaultServerWebExchange
;
import
org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
ArgumentMatchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
never
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.*;
/**
* Unit tests for {@link DefaultWebSessionManager}.
...
...
@@ -50,15 +47,15 @@ import static org.mockito.Mockito.when;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
DefaultWebSessionManagerTests
{
private
DefaultWebSessionManager
m
anager
;
private
DefaultWebSessionManager
sessionM
anager
;
private
ServerWebExchange
exchange
;
@Mock
private
WebSessionIdResolver
i
dResolver
;
private
WebSessionIdResolver
sessionI
dResolver
;
@Mock
private
WebSessionStore
store
;
private
WebSessionStore
s
essionS
tore
;
@Mock
private
WebSession
createSession
;
...
...
@@ -69,78 +66,75 @@ public class DefaultWebSessionManagerTests {
@Before
public
void
setUp
()
throws
Exception
{
when
(
this
.
store
.
createWebSession
()).
thenReturn
(
Mono
.
just
(
this
.
createSession
));
when
(
this
.
createSession
.
save
()).
thenReturn
(
Mono
.
empty
());
when
(
this
.
createSession
.
getId
()).
thenReturn
(
"create-session-id"
);
when
(
this
.
updateSession
.
getId
()).
thenReturn
(
"update-session-id"
);
this
.
manager
=
new
DefaultWebSessionManager
();
this
.
manager
.
setSessionIdResolver
(
this
.
idResolver
);
this
.
manager
.
setSessionStore
(
this
.
store
);
when
(
this
.
sessionStore
.
createWebSession
()).
thenReturn
(
Mono
.
just
(
this
.
createSession
));
when
(
this
.
sessionStore
.
retrieveSession
(
this
.
updateSession
.
getId
())).
thenReturn
(
Mono
.
just
(
this
.
updateSession
));
this
.
sessionManager
=
new
DefaultWebSessionManager
();
this
.
sessionManager
.
setSessionIdResolver
(
this
.
sessionIdResolver
);
this
.
sessionManager
.
setSessionStore
(
this
.
sessionStore
);
MockServerHttpRequest
request
=
MockServerHttpRequest
.
get
(
"/path"
).
build
();
MockServerHttpResponse
response
=
new
MockServerHttpResponse
();
this
.
exchange
=
new
DefaultServerWebExchange
(
request
,
response
,
this
.
m
anager
,
this
.
exchange
=
new
DefaultServerWebExchange
(
request
,
response
,
this
.
sessionM
anager
,
ServerCodecConfigurer
.
create
(),
new
AcceptHeaderLocaleContextResolver
());
}
@Test
public
void
getSessionSaveWhenCreatedAndNotStartedThenNotSaved
()
throws
Exception
{
when
(
this
.
idResolver
.
resolveSessionIds
(
this
.
exchange
)).
thenReturn
(
Collections
.
emptyList
());
WebSession
session
=
this
.
manager
.
getSession
(
this
.
exchange
).
block
();
public
void
getSessionSaveWhenCreatedAndNotStartedThenNotSaved
()
{
when
(
this
.
sessionIdResolver
.
resolveSessionIds
(
this
.
exchange
)).
thenReturn
(
Collections
.
emptyList
());
WebSession
session
=
this
.
sessionManager
.
getSession
(
this
.
exchange
).
block
();
this
.
exchange
.
getResponse
().
setComplete
().
block
();
assertSame
(
this
.
createSession
,
session
);
assertFalse
(
session
.
isStarted
());
assertFalse
(
session
.
isExpired
());
verify
(
this
.
createSession
,
never
()).
save
();
verify
(
this
.
i
dResolver
,
never
()).
setSessionId
(
any
(),
any
());
verify
(
this
.
sessionI
dResolver
,
never
()).
setSessionId
(
any
(),
any
());
}
@Test
public
void
getSessionSaveWhenCreatedAndStartedThenSavesAndSetsId
()
throws
Exception
{
when
(
this
.
idResolver
.
resolveSessionIds
(
this
.
exchange
)).
thenReturn
(
Collections
.
emptyList
());
WebSession
session
=
this
.
manager
.
getSession
(
this
.
exchange
).
block
();
when
(
this
.
createSession
.
isStarted
()).
thenReturn
(
true
);
this
.
exchange
.
getResponse
().
setComplete
().
block
();
public
void
getSessionSaveWhenCreatedAndStartedThenSavesAndSetsId
()
{
String
id
=
session
.
getId
();
verify
(
this
.
store
).
createWebSession
();
verify
(
this
.
createSession
).
save
();
verify
(
this
.
idResolver
).
setSessionId
(
any
(),
eq
(
id
));
}
when
(
this
.
sessionIdResolver
.
resolveSessionIds
(
this
.
exchange
)).
thenReturn
(
Collections
.
emptyList
());
WebSession
session
=
this
.
sessionManager
.
getSession
(
this
.
exchange
).
block
();
assertSame
(
this
.
createSession
,
session
);
String
sessionId
=
this
.
createSession
.
getId
();
@Test
public
void
exchangeWhenResponseSetCompleteThenSavesAndSetsId
()
throws
Exception
{
when
(
this
.
idResolver
.
resolveSessionIds
(
this
.
exchange
)).
thenReturn
(
Collections
.
emptyList
());
String
id
=
this
.
createSession
.
getId
();
WebSession
session
=
this
.
manager
.
getSession
(
this
.
exchange
).
block
();
when
(
this
.
createSession
.
isStarted
()).
thenReturn
(
true
);
this
.
exchange
.
getResponse
().
setComplete
().
block
();
verify
(
this
.
idResolver
).
setSessionId
(
any
(),
eq
(
id
));
verify
(
this
.
sessionStore
).
createWebSession
();
verify
(
this
.
sessionIdResolver
).
setSessionId
(
any
(),
eq
(
sessionId
));
verify
(
this
.
createSession
).
save
();
}
@Test
public
void
existingSession
()
throws
Exception
{
String
id
=
this
.
updateSession
.
getId
();
when
(
this
.
store
.
retrieveSession
(
id
)).
thenReturn
(
Mono
.
just
(
this
.
updateSession
)
);
when
(
this
.
idResolver
.
resolveSessionIds
(
this
.
exchange
)).
thenReturn
(
Collections
.
singletonList
(
i
d
));
public
void
existingSession
()
{
String
sessionId
=
this
.
updateSession
.
getId
(
);
when
(
this
.
sessionIdResolver
.
resolveSessionIds
(
this
.
exchange
)).
thenReturn
(
Collections
.
singletonList
(
sessionI
d
));
WebSession
actual
=
this
.
m
anager
.
getSession
(
this
.
exchange
).
block
();
WebSession
actual
=
this
.
sessionM
anager
.
getSession
(
this
.
exchange
).
block
();
assertNotNull
(
actual
);
assertEquals
(
i
d
,
actual
.
getId
());
assertEquals
(
sessionI
d
,
actual
.
getId
());
}
@Test
public
void
multipleSessionIds
()
throws
Exception
{
WebSession
existing
=
this
.
updateSession
;
String
id
=
existing
.
getId
(
);
when
(
this
.
s
tore
.
retrieveSession
(
any
()
)).
thenReturn
(
Mono
.
empty
());
when
(
this
.
s
tore
.
retrieveSession
(
id
)).
thenReturn
(
Mono
.
just
(
existing
));
when
(
this
.
idResolver
.
resolveSessionIds
(
this
.
exchange
)).
thenReturn
(
Arrays
.
asList
(
"neither-this"
,
"nor-that"
,
id
)
);
WebSession
actual
=
this
.
manager
.
getSession
(
this
.
exchange
).
block
();
public
void
multipleSessionIds
()
{
List
<
String
>
ids
=
Arrays
.
asList
(
"not-this"
,
"not-that"
,
this
.
updateSession
.
getId
()
);
when
(
this
.
s
essionStore
.
retrieveSession
(
"not-this"
)).
thenReturn
(
Mono
.
empty
());
when
(
this
.
s
essionStore
.
retrieveSession
(
"not-that"
)).
thenReturn
(
Mono
.
empty
(
));
when
(
this
.
sessionIdResolver
.
resolveSessionIds
(
this
.
exchange
)).
thenReturn
(
ids
);
WebSession
actual
=
this
.
sessionManager
.
getSession
(
this
.
exchange
).
block
();
assertNotNull
(
actual
);
assertEquals
(
existing
.
getId
(),
actual
.
getId
());
assertEquals
(
this
.
updateSession
.
getId
(),
actual
.
getId
());
}
}
spring-web/src/test/java/org/springframework/web/server/session/InMemoryWebSessionStoreTests.java
浏览文件 @
eef592d9
...
...
@@ -55,7 +55,7 @@ public class InMemoryWebSessionStoreTests {
}
@Test
public
void
retrieveExpiredSession
()
throws
Exception
{
public
void
retrieveExpiredSession
()
{
WebSession
session
=
this
.
store
.
createWebSession
().
block
();
assertNotNull
(
session
);
session
.
getAttributes
().
put
(
"foo"
,
"bar"
);
...
...
@@ -73,7 +73,7 @@ public class InMemoryWebSessionStoreTests {
}
@Test
public
void
lastAccessTimeIsUpdatedOnRetrieve
()
throws
Exception
{
public
void
lastAccessTimeIsUpdatedOnRetrieve
()
{
WebSession
session1
=
this
.
store
.
createWebSession
().
block
();
assertNotNull
(
session1
);
String
id
=
session1
.
getId
();
...
...
@@ -91,7 +91,7 @@ public class InMemoryWebSessionStoreTests {
}
@Test
public
void
expirationChecks
()
throws
Exception
{
public
void
expirationChecks
()
{
// Create 3 sessions
WebSession
session1
=
this
.
store
.
createWebSession
().
block
();
assertNotNull
(
session1
);
...
...
@@ -131,6 +131,4 @@ public class InMemoryWebSessionStoreTests {
assertNotNull
(
this
.
store
.
retrieveSession
(
session5
.
getId
()).
block
());
}
}
\ No newline at end of file
spring-web/src/test/java/org/springframework/web/server/session/MockWebSessionManager.java
已删除
100644 → 0
浏览文件 @
d82b0e37
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.web.server.session
;
import
reactor.core.publisher.Mono
;
import
org.springframework.web.server.ServerWebExchange
;
import
org.springframework.web.server.WebSession
;
/**
* Mock implementation of {@link WebSessionManager}.
*
* @author Rossen Stoyanchev
*/
public
class
MockWebSessionManager
implements
WebSessionManager
{
private
final
Mono
<
WebSession
>
session
;
public
MockWebSessionManager
(
WebSession
session
)
{
this
(
Mono
.
just
(
session
));
}
public
MockWebSessionManager
(
Mono
<
WebSession
>
session
)
{
this
.
session
=
session
;
}
@Override
public
Mono
<
WebSession
>
getSession
(
ServerWebExchange
exchange
)
{
return
this
.
session
;
}
}
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeArgumentResolverTests.java
浏览文件 @
eef592d9
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
8
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.
...
...
@@ -58,7 +58,7 @@ public class ServerWebExchangeArgumentResolverTests {
@Test
public
void
supportsParameter
()
throws
Exception
{
public
void
supportsParameter
()
{
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
ServerWebExchange
.
class
)));
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
ServerHttpRequest
.
class
)));
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
ServerHttpResponse
.
class
)));
...
...
@@ -69,6 +69,7 @@ public class ServerWebExchangeArgumentResolverTests {
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
UriComponentsBuilder
.
class
)));
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
UriBuilder
.
class
)));
assertFalse
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
WebSession
.
class
)));
assertFalse
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
String
.
class
)));
try
{
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
Mono
.
class
,
ServerWebExchange
.
class
));
...
...
@@ -82,7 +83,7 @@ public class ServerWebExchangeArgumentResolverTests {
}
@Test
public
void
resolveArgument
()
throws
Exception
{
public
void
resolveArgument
()
{
testResolveArgument
(
this
.
testMethod
.
arg
(
ServerWebExchange
.
class
),
this
.
exchange
);
testResolveArgument
(
this
.
testMethod
.
arg
(
ServerHttpRequest
.
class
),
this
.
exchange
.
getRequest
());
testResolveArgument
(
this
.
testMethod
.
arg
(
ServerHttpResponse
.
class
),
this
.
exchange
.
getResponse
());
...
...
@@ -97,7 +98,7 @@ public class ServerWebExchangeArgumentResolverTests {
}
@Test
public
void
resolveUriComponentsBuilder
()
throws
Exception
{
public
void
resolveUriComponentsBuilder
()
{
MethodParameter
param
=
this
.
testMethod
.
arg
(
UriComponentsBuilder
.
class
);
Object
value
=
this
.
resolver
.
resolveArgument
(
param
,
new
BindingContext
(),
this
.
exchange
).
block
();
...
...
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributeMethodArgumentResolverTests.java
浏览文件 @
eef592d9
...
...
@@ -31,10 +31,8 @@ import org.springframework.core.MethodParameter;
import
org.springframework.core.ReactiveAdapterRegistry
;
import
org.springframework.core.annotation.SynthesizingMethodParameter
;
import
org.springframework.format.support.DefaultFormattingConversionService
;
import
org.springframework.http.codec.ServerCodecConfigurer
;
import
org.springframework.http.server.reactive.ServerHttpRequest
;
import
org.springframework.mock.http.server.reactive.test.MockServerHttpRequest
;
import
org.springframework.mock.
http.server.reactive.test.MockServerHttpRespons
e
;
import
org.springframework.mock.
web.test.server.MockServerWebExchang
e
;
import
org.springframework.util.ReflectionUtils
;
import
org.springframework.web.bind.annotation.SessionAttribute
;
import
org.springframework.web.bind.support.ConfigurableWebBindingInitializer
;
...
...
@@ -42,22 +40,12 @@ import org.springframework.web.reactive.BindingContext;
import
org.springframework.web.server.ServerWebExchange
;
import
org.springframework.web.server.ServerWebInputException
;
import
org.springframework.web.server.WebSession
;
import
org.springframework.web.server.adapter.DefaultServerWebExchange
;
import
org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver
;
import
org.springframework.web.server.session.MockWebSessionManager
;
import
org.springframework.web.server.session.WebSessionManager
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertSame
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.*;
/**
* Unit tests for {@link SessionAttributeMethodArgumentResolver}.
*
* @author Rossen Stoyanchev
*/
public
class
SessionAttributeMethodArgumentResolverTests
{
...
...
@@ -73,31 +61,25 @@ public class SessionAttributeMethodArgumentResolverTests {
@Before
@SuppressWarnings
(
"resource"
)
public
void
setup
()
throws
Exception
{
public
void
setup
()
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
context
.
refresh
();
ReactiveAdapterRegistry
adapterRegistry
=
ReactiveAdapterRegistry
.
getSharedInstance
();
this
.
resolver
=
new
SessionAttributeMethodArgumentResolver
(
context
.
getBeanFactory
(),
adapterRegistry
);
this
.
session
=
mock
(
WebSession
.
class
);
WebSessionManager
sessionManager
=
new
MockWebSessionManager
(
this
.
session
);
ServerHttpRequest
request
=
MockServerHttpRequest
.
get
(
"/"
).
build
();
this
.
exchange
=
new
DefaultServerWebExchange
(
request
,
new
MockServerHttpResponse
(),
sessionManager
,
ServerCodecConfigurer
.
create
(),
new
AcceptHeaderLocaleContextResolver
());
this
.
exchange
=
MockServerWebExchange
.
builder
(
MockServerHttpRequest
.
get
(
"/"
)).
session
(
this
.
session
).
build
();
this
.
handleMethod
=
ReflectionUtils
.
findMethod
(
getClass
(),
"handleWithSessionAttribute"
,
(
Class
<?>[])
null
);
}
@Test
public
void
supportsParameter
()
throws
Exception
{
public
void
supportsParameter
()
{
assertTrue
(
this
.
resolver
.
supportsParameter
(
new
MethodParameter
(
this
.
handleMethod
,
0
)));
assertFalse
(
this
.
resolver
.
supportsParameter
(
new
MethodParameter
(
this
.
handleMethod
,
4
)));
}
@Test
public
void
resolve
()
throws
Exception
{
public
void
resolve
()
{
MethodParameter
param
=
initMethodParameter
(
0
);
Mono
<
Object
>
mono
=
this
.
resolver
.
resolveArgument
(
param
,
new
BindingContext
(),
this
.
exchange
);
StepVerifier
.
create
(
mono
).
expectError
(
ServerWebInputException
.
class
).
verify
();
...
...
@@ -109,7 +91,7 @@ public class SessionAttributeMethodArgumentResolverTests {
}
@Test
public
void
resolveWithName
()
throws
Exception
{
public
void
resolveWithName
()
{
MethodParameter
param
=
initMethodParameter
(
1
);
Foo
foo
=
new
Foo
();
when
(
this
.
session
.
getAttribute
(
"specialFoo"
)).
thenReturn
(
foo
);
...
...
@@ -118,7 +100,7 @@ public class SessionAttributeMethodArgumentResolverTests {
}
@Test
public
void
resolveNotRequired
()
throws
Exception
{
public
void
resolveNotRequired
()
{
MethodParameter
param
=
initMethodParameter
(
2
);
Mono
<
Object
>
mono
=
this
.
resolver
.
resolveArgument
(
param
,
new
BindingContext
(),
this
.
exchange
);
assertNull
(
mono
.
block
());
...
...
@@ -131,7 +113,7 @@ public class SessionAttributeMethodArgumentResolverTests {
@SuppressWarnings
(
"unchecked"
)
@Test
public
void
resolveOptional
()
throws
Exception
{
public
void
resolveOptional
()
{
MethodParameter
param
=
initMethodParameter
(
3
);
Optional
<
Object
>
actual
=
(
Optional
<
Object
>)
this
.
resolver
.
resolveArgument
(
param
,
new
BindingContext
(),
this
.
exchange
).
block
();
...
...
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributesHandlerTests.java
浏览文件 @
eef592d9
/*
* Copyright 2002-201
6
the original author or authors.
* Copyright 2002-201
8
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,7 +17,6 @@
package
org.springframework.web.reactive.result.method.annotation
;
import
java.time.Duration
;
import
java.util.HashSet
;
import
org.junit.Test
;
...
...
@@ -28,12 +27,8 @@ import org.springframework.web.bind.annotation.SessionAttributes;
import
org.springframework.web.server.WebSession
;
import
org.springframework.web.server.session.InMemoryWebSessionStore
;
import
static
java
.
util
.
Arrays
.
asList
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
java
.
util
.
Arrays
.*;
import
static
org
.
junit
.
Assert
.*;
/**
* Test fixture with {@link SessionAttributesHandler}.
...
...
@@ -46,7 +41,7 @@ public class SessionAttributesHandlerTests {
@Test
public
void
isSessionAttribute
()
throws
Exception
{
public
void
isSessionAttribute
()
{
assertTrue
(
this
.
sessionAttributesHandler
.
isHandlerSessionAttribute
(
"attr1"
,
String
.
class
));
assertTrue
(
this
.
sessionAttributesHandler
.
isHandlerSessionAttribute
(
"attr2"
,
String
.
class
));
assertTrue
(
this
.
sessionAttributesHandler
.
isHandlerSessionAttribute
(
"simple"
,
TestBean
.
class
));
...
...
@@ -54,8 +49,8 @@ public class SessionAttributesHandlerTests {
}
@Test
public
void
retrieveAttributes
()
throws
Exception
{
WebSession
session
=
new
InMemoryWebSessionStore
().
createWebSession
().
block
(
Duration
.
ZERO
);
public
void
retrieveAttributes
()
{
WebSession
session
=
new
InMemoryWebSessionStore
().
createWebSession
().
block
();
assertNotNull
(
session
);
session
.
getAttributes
().
put
(
"attr1"
,
"value1"
);
...
...
@@ -76,8 +71,8 @@ public class SessionAttributesHandlerTests {
}
@Test
public
void
cleanupAttributes
()
throws
Exception
{
WebSession
session
=
new
InMemoryWebSessionStore
().
createWebSession
().
block
(
Duration
.
ZERO
);
public
void
cleanupAttributes
()
{
WebSession
session
=
new
InMemoryWebSessionStore
().
createWebSession
().
block
();
assertNotNull
(
session
);
session
.
getAttributes
().
put
(
"attr1"
,
"value1"
);
...
...
@@ -98,8 +93,8 @@ public class SessionAttributesHandlerTests {
}
@Test
public
void
storeAttributes
()
throws
Exception
{
WebSession
session
=
new
InMemoryWebSessionStore
().
createWebSession
().
block
(
Duration
.
ZERO
);
public
void
storeAttributes
()
{
WebSession
session
=
new
InMemoryWebSessionStore
().
createWebSession
().
block
();
assertNotNull
(
session
);
ModelMap
model
=
new
ModelMap
();
...
...
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/WebSessionArgumentResolverTests.java
浏览文件 @
eef592d9
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
8
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.
...
...
@@ -21,21 +21,15 @@ import reactor.core.publisher.Mono;
import
org.springframework.core.MethodParameter
;
import
org.springframework.core.ReactiveAdapterRegistry
;
import
org.springframework.http.codec.ServerCodecConfigurer
;
import
org.springframework.mock.http.server.reactive.test.MockServerHttpRequest
;
import
org.springframework.mock.
http.server.reactive.test.MockServerHttpRespons
e
;
import
org.springframework.mock.
web.test.server.MockServerWebExchang
e
;
import
org.springframework.web.method.ResolvableMethod
;
import
org.springframework.web.reactive.BindingContext
;
import
org.springframework.web.server.ServerWebExchange
;
import
org.springframework.web.server.WebSession
;
import
org.springframework.web.server.adapter.DefaultServerWebExchange
;
import
org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver
;
import
org.springframework.web.server.session.WebSessionManager
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertSame
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.*;
/**
* Unit tests for {@link WebSessionArgumentResolver}.
...
...
@@ -50,7 +44,7 @@ public class WebSessionArgumentResolverTests {
@Test
public
void
supportsParameter
()
throws
Exception
{
public
void
supportsParameter
()
{
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
WebSession
.
class
)));
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
Mono
.
class
,
WebSession
.
class
)));
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
Single
.
class
,
WebSession
.
class
)));
...
...
@@ -58,14 +52,12 @@ public class WebSessionArgumentResolverTests {
@Test
public
void
resolverArgument
()
throws
Exception
{
public
void
resolverArgument
()
{
BindingContext
context
=
new
BindingContext
();
WebSession
session
=
mock
(
WebSession
.
class
);
WebSessionManager
manager
=
exchange
->
Mono
.
just
(
session
);
MockServerHttpRequest
request
=
MockServerHttpRequest
.
get
(
"/"
).
build
();
ServerWebExchange
exchange
=
new
DefaultServerWebExchange
(
request
,
new
MockServerHttpResponse
(),
manager
,
ServerCodecConfigurer
.
create
(),
new
AcceptHeaderLocaleContextResolver
());
ServerWebExchange
exchange
=
MockServerWebExchange
.
builder
(
request
).
session
(
session
).
build
();
MethodParameter
param
=
this
.
testMethod
.
arg
(
WebSession
.
class
);
Object
actual
=
this
.
resolver
.
resolveArgument
(
param
,
context
,
exchange
).
block
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录