|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2017 the original author or authors. |
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with 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.Arrays; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Collections; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
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.adapter.DefaultServerWebExchange; |
|
|
|
import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver; |
|
|
|
import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.junit.Assert.assertFalse; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertNotNull; |
|
|
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any; |
|
|
|
import static org.mockito.ArgumentMatchers.any; |
|
|
|
import static org.mockito.ArgumentMatchers.eq; |
|
|
|
import static org.mockito.ArgumentMatchers.eq; |
|
|
|
import static org.mockito.Mockito.never; |
|
|
|
import static org.mockito.Mockito.*; |
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Unit tests for {@link DefaultWebSessionManager}. |
|
|
|
* Unit tests for {@link DefaultWebSessionManager}. |
|
|
|
@ -50,15 +47,15 @@ import static org.mockito.Mockito.when; |
|
|
|
@RunWith(MockitoJUnitRunner.class) |
|
|
|
@RunWith(MockitoJUnitRunner.class) |
|
|
|
public class DefaultWebSessionManagerTests { |
|
|
|
public class DefaultWebSessionManagerTests { |
|
|
|
|
|
|
|
|
|
|
|
private DefaultWebSessionManager manager; |
|
|
|
private DefaultWebSessionManager sessionManager; |
|
|
|
|
|
|
|
|
|
|
|
private ServerWebExchange exchange; |
|
|
|
private ServerWebExchange exchange; |
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
@Mock |
|
|
|
private WebSessionIdResolver idResolver; |
|
|
|
private WebSessionIdResolver sessionIdResolver; |
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
@Mock |
|
|
|
private WebSessionStore store; |
|
|
|
private WebSessionStore sessionStore; |
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
@Mock |
|
|
|
private WebSession createSession; |
|
|
|
private WebSession createSession; |
|
|
|
@ -69,78 +66,75 @@ public class DefaultWebSessionManagerTests { |
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
@Before |
|
|
|
public void setUp() throws Exception { |
|
|
|
public void setUp() throws Exception { |
|
|
|
when(this.store.createWebSession()).thenReturn(Mono.just(this.createSession)); |
|
|
|
|
|
|
|
when(this.createSession.save()).thenReturn(Mono.empty()); |
|
|
|
when(this.createSession.save()).thenReturn(Mono.empty()); |
|
|
|
|
|
|
|
when(this.createSession.getId()).thenReturn("create-session-id"); |
|
|
|
when(this.updateSession.getId()).thenReturn("update-session-id"); |
|
|
|
when(this.updateSession.getId()).thenReturn("update-session-id"); |
|
|
|
|
|
|
|
|
|
|
|
this.manager = new DefaultWebSessionManager(); |
|
|
|
when(this.sessionStore.createWebSession()).thenReturn(Mono.just(this.createSession)); |
|
|
|
this.manager.setSessionIdResolver(this.idResolver); |
|
|
|
when(this.sessionStore.retrieveSession(this.updateSession.getId())).thenReturn(Mono.just(this.updateSession)); |
|
|
|
this.manager.setSessionStore(this.store); |
|
|
|
|
|
|
|
|
|
|
|
this.sessionManager = new DefaultWebSessionManager(); |
|
|
|
|
|
|
|
this.sessionManager.setSessionIdResolver(this.sessionIdResolver); |
|
|
|
|
|
|
|
this.sessionManager.setSessionStore(this.sessionStore); |
|
|
|
|
|
|
|
|
|
|
|
MockServerHttpRequest request = MockServerHttpRequest.get("/path").build(); |
|
|
|
MockServerHttpRequest request = MockServerHttpRequest.get("/path").build(); |
|
|
|
MockServerHttpResponse response = new MockServerHttpResponse(); |
|
|
|
MockServerHttpResponse response = new MockServerHttpResponse(); |
|
|
|
this.exchange = new DefaultServerWebExchange(request, response, this.manager, |
|
|
|
this.exchange = new DefaultServerWebExchange(request, response, this.sessionManager, |
|
|
|
ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver()); |
|
|
|
ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getSessionSaveWhenCreatedAndNotStartedThenNotSaved() throws Exception { |
|
|
|
public void getSessionSaveWhenCreatedAndNotStartedThenNotSaved() { |
|
|
|
when(this.idResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.emptyList()); |
|
|
|
|
|
|
|
WebSession session = this.manager.getSession(this.exchange).block(); |
|
|
|
when(this.sessionIdResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.emptyList()); |
|
|
|
|
|
|
|
WebSession session = this.sessionManager.getSession(this.exchange).block(); |
|
|
|
this.exchange.getResponse().setComplete().block(); |
|
|
|
this.exchange.getResponse().setComplete().block(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertSame(this.createSession, session); |
|
|
|
assertFalse(session.isStarted()); |
|
|
|
assertFalse(session.isStarted()); |
|
|
|
assertFalse(session.isExpired()); |
|
|
|
assertFalse(session.isExpired()); |
|
|
|
verify(this.createSession, never()).save(); |
|
|
|
verify(this.createSession, never()).save(); |
|
|
|
verify(this.idResolver, never()).setSessionId(any(), any()); |
|
|
|
verify(this.sessionIdResolver, never()).setSessionId(any(), any()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getSessionSaveWhenCreatedAndStartedThenSavesAndSetsId() throws Exception { |
|
|
|
public void getSessionSaveWhenCreatedAndStartedThenSavesAndSetsId() { |
|
|
|
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(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String id = session.getId(); |
|
|
|
when(this.sessionIdResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.emptyList()); |
|
|
|
verify(this.store).createWebSession(); |
|
|
|
WebSession session = this.sessionManager.getSession(this.exchange).block(); |
|
|
|
verify(this.createSession).save(); |
|
|
|
assertSame(this.createSession, session); |
|
|
|
verify(this.idResolver).setSessionId(any(), eq(id)); |
|
|
|
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); |
|
|
|
when(this.createSession.isStarted()).thenReturn(true); |
|
|
|
this.exchange.getResponse().setComplete().block(); |
|
|
|
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(); |
|
|
|
verify(this.createSession).save(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void existingSession() throws Exception { |
|
|
|
public void existingSession() { |
|
|
|
String id = this.updateSession.getId(); |
|
|
|
|
|
|
|
when(this.store.retrieveSession(id)).thenReturn(Mono.just(this.updateSession)); |
|
|
|
String sessionId = this.updateSession.getId(); |
|
|
|
when(this.idResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.singletonList(id)); |
|
|
|
when(this.sessionIdResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.singletonList(sessionId)); |
|
|
|
|
|
|
|
|
|
|
|
WebSession actual = this.manager.getSession(this.exchange).block(); |
|
|
|
WebSession actual = this.sessionManager.getSession(this.exchange).block(); |
|
|
|
assertNotNull(actual); |
|
|
|
assertNotNull(actual); |
|
|
|
assertEquals(id, actual.getId()); |
|
|
|
assertEquals(sessionId, actual.getId()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void multipleSessionIds() throws Exception { |
|
|
|
public void multipleSessionIds() { |
|
|
|
WebSession existing = this.updateSession; |
|
|
|
|
|
|
|
String id = existing.getId(); |
|
|
|
List<String> ids = Arrays.asList("not-this", "not-that", this.updateSession.getId()); |
|
|
|
when(this.store.retrieveSession(any())).thenReturn(Mono.empty()); |
|
|
|
when(this.sessionStore.retrieveSession("not-this")).thenReturn(Mono.empty()); |
|
|
|
when(this.store.retrieveSession(id)).thenReturn(Mono.just(existing)); |
|
|
|
when(this.sessionStore.retrieveSession("not-that")).thenReturn(Mono.empty()); |
|
|
|
when(this.idResolver.resolveSessionIds(this.exchange)).thenReturn(Arrays.asList("neither-this", "nor-that", id)); |
|
|
|
when(this.sessionIdResolver.resolveSessionIds(this.exchange)).thenReturn(ids); |
|
|
|
|
|
|
|
WebSession actual = this.sessionManager.getSession(this.exchange).block(); |
|
|
|
WebSession actual = this.manager.getSession(this.exchange).block(); |
|
|
|
|
|
|
|
assertNotNull(actual); |
|
|
|
assertNotNull(actual); |
|
|
|
assertEquals(existing.getId(), actual.getId()); |
|
|
|
assertEquals(this.updateSession.getId(), actual.getId()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|