|
15 | 15 | */
|
16 | 16 | package org.springframework.web.server.session;
|
17 | 17 |
|
18 |
| -import java.time.Clock; |
19 |
| -import java.time.Duration; |
20 |
| -import java.time.Instant; |
21 |
| -import java.time.ZoneId; |
22 |
| -import java.util.UUID; |
23 |
| - |
24 | 18 | import org.junit.Before;
|
25 | 19 | import org.junit.Test;
|
26 |
| -import reactor.core.publisher.Mono; |
27 |
| - |
28 |
| -import org.springframework.http.codec.ServerCodecConfigurer; |
29 | 20 | import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
30 |
| -import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; |
31 | 21 | import org.springframework.web.server.ServerWebExchange;
|
32 |
| -import org.springframework.web.server.WebSession; |
33 |
| -import org.springframework.web.server.adapter.DefaultServerWebExchange; |
34 |
| -import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver; |
35 | 22 |
|
36 |
| -import static org.hamcrest.collection.IsCollectionWithSize.hasSize; |
37 |
| -import static org.hamcrest.core.Is.is; |
38 |
| -import static org.hamcrest.core.IsCollectionContaining.hasItem; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | + |
39 | 26 | import static org.junit.Assert.assertEquals;
|
40 |
| -import static org.junit.Assert.assertFalse; |
41 |
| -import static org.junit.Assert.assertNotNull; |
42 |
| -import static org.junit.Assert.assertNotSame; |
43 |
| -import static org.junit.Assert.assertNull; |
44 |
| -import static org.junit.Assert.assertThat; |
| 27 | +import static org.junit.Assert.assertTrue; |
45 | 28 |
|
46 | 29 | /**
|
47 | 30 | * Tests using {@link HeaderWebSessionIdResolver}.
|
48 | 31 | *
|
49 | 32 | * @author Greg Turnquist
|
| 33 | + * @author Rob Winch |
50 | 34 | */
|
51 | 35 | public class HeaderWebSessionIdResolverTests {
|
52 |
| - |
53 |
| - private static final Clock CLOCK = Clock.system(ZoneId.of("GMT")); |
54 |
| - |
55 |
| - |
56 | 36 | private HeaderWebSessionIdResolver idResolver;
|
57 | 37 |
|
58 |
| - private DefaultWebSessionManager manager; |
59 |
| - |
60 | 38 | private ServerWebExchange exchange;
|
61 | 39 |
|
62 |
| - |
63 | 40 | @Before
|
64 | 41 | public void setUp() {
|
65 | 42 | this.idResolver = new HeaderWebSessionIdResolver();
|
66 |
| - this.manager = new DefaultWebSessionManager(); |
67 |
| - this.manager.setSessionIdResolver(this.idResolver); |
| 43 | + this.exchange = MockServerHttpRequest.get("/path").toExchange(); |
| 44 | + } |
68 | 45 |
|
69 |
| - MockServerHttpRequest request = MockServerHttpRequest.get("/path").build(); |
70 |
| - MockServerHttpResponse response = new MockServerHttpResponse(); |
| 46 | + @Test |
| 47 | + public void expireWhenValidThenSetsEmptyHeader() { |
| 48 | + this.idResolver.expireSession(this.exchange); |
71 | 49 |
|
72 |
| - this.exchange = new DefaultServerWebExchange(request, response, this.manager, |
73 |
| - ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver()); |
| 50 | + assertEquals(Arrays.asList(""), |
| 51 | + this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)); |
74 | 52 | }
|
75 | 53 |
|
76 | 54 | @Test
|
77 |
| - public void getSessionWithoutStarting() throws Exception { |
78 |
| - WebSession session = this.manager.getSession(this.exchange).block(); |
79 |
| - session.save(); |
| 55 | + public void expireWhenMultipleInvocationThenSetsSingleEmptyHeader() { |
| 56 | + this.idResolver.expireSession(this.exchange); |
| 57 | + |
| 58 | + this.idResolver.expireSession(this.exchange); |
80 | 59 |
|
81 |
| - assertFalse(session.isStarted()); |
82 |
| - assertFalse(session.isExpired()); |
83 |
| - assertNull(this.manager.getSessionStore().retrieveSession(session.getId()).block()); |
| 60 | + assertEquals(Arrays.asList(""), |
| 61 | + this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)); |
84 | 62 | }
|
85 | 63 |
|
86 | 64 | @Test
|
87 |
| - public void startSessionExplicitly() throws Exception { |
88 |
| - WebSession session = this.manager.getSession(this.exchange).block(); |
89 |
| - session.start(); |
90 |
| - session.save().block(); |
91 |
| - |
92 |
| - assertThat(this.exchange.getResponse().getHeaders().containsKey("SESSION"), is(true)); |
93 |
| - assertThat(this.exchange.getResponse().getHeaders().get("SESSION"), hasSize(1)); |
94 |
| - assertThat(this.exchange.getResponse().getHeaders().get("SESSION"), hasItem(session.getId())); |
| 65 | + public void expireWhenAfterSetSessionIdThenSetsEmptyHeader() { |
| 66 | + this.idResolver.setSessionId(this.exchange, "123"); |
| 67 | + |
| 68 | + this.idResolver.expireSession(this.exchange); |
| 69 | + |
| 70 | + assertEquals(Arrays.asList(""), |
| 71 | + this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)); |
95 | 72 | }
|
96 | 73 |
|
97 | 74 | @Test
|
98 |
| - public void startSessionImplicitly() throws Exception { |
99 |
| - WebSession session = this.manager.getSession(this.exchange).block(); |
100 |
| - session.getAttributes().put("foo", "bar"); |
101 |
| - session.save(); |
| 75 | + public void setSessionIdWhenValidThenSetsHeader() { |
| 76 | + String id = "123"; |
| 77 | + |
| 78 | + this.idResolver.setSessionId(this.exchange, id); |
102 | 79 |
|
103 |
| - assertNotNull(this.exchange.getResponse().getHeaders().get("SESSION")); |
| 80 | + assertEquals(Arrays.asList(id), |
| 81 | + this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)); |
104 | 82 | }
|
105 | 83 |
|
106 | 84 | @Test
|
107 |
| - public void existingSession() throws Exception { |
108 |
| - UUID sessionId = UUID.randomUUID(); |
109 |
| - DefaultWebSession existing = createDefaultWebSession(sessionId); |
110 |
| - this.manager.getSessionStore().storeSession(existing); |
111 |
| - |
112 |
| - this.exchange = this.exchange.mutate() |
113 |
| - .request(this.exchange.getRequest().mutate() |
114 |
| - .header("SESSION", sessionId.toString()) |
115 |
| - .build()) |
116 |
| - .build(); |
117 |
| - |
118 |
| - WebSession actual = this.manager.getSession(this.exchange).block(); |
119 |
| - assertNotNull(actual); |
120 |
| - assertEquals(existing.getId(), actual.getId()); |
| 85 | + public void setSessionIdWhenMultipleThenSetsSingleHeader() { |
| 86 | + String id = "123"; |
| 87 | + this.idResolver.setSessionId(this.exchange, "overriddenByNextInvocation"); |
| 88 | + |
| 89 | + this.idResolver.setSessionId(this.exchange, id); |
| 90 | + |
| 91 | + assertEquals(Arrays.asList(id), |
| 92 | + this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)); |
121 | 93 | }
|
122 | 94 |
|
123 | 95 | @Test
|
124 |
| - public void existingSessionIsExpired() throws Exception { |
125 |
| - UUID sessionId = UUID.randomUUID(); |
126 |
| - DefaultWebSession existing = createDefaultWebSession(sessionId); |
127 |
| - existing.start(); |
128 |
| - Instant lastAccessTime = Instant.now(CLOCK).minus(Duration.ofMinutes(31)); |
129 |
| - existing = new DefaultWebSession(existing, lastAccessTime, s -> Mono.empty()); |
130 |
| - this.manager.getSessionStore().storeSession(existing); |
131 |
| - |
132 |
| - this.exchange = this.exchange.mutate() |
133 |
| - .request(this.exchange.getRequest().mutate() |
134 |
| - .header("SESSION", sessionId.toString()) |
135 |
| - .build()) |
136 |
| - .build(); |
137 |
| - |
138 |
| - WebSession actual = this.manager.getSession(this.exchange).block(); |
139 |
| - assertNotSame(existing, actual); |
| 96 | + public void setSessionIdWhenCustomHeaderNameThenSetsHeader() { |
| 97 | + String headerName = "x-auth"; |
| 98 | + String id = "123"; |
| 99 | + this.idResolver.setHeaderName(headerName); |
| 100 | + |
| 101 | + this.idResolver.setSessionId(this.exchange, id); |
| 102 | + |
| 103 | + assertEquals(Arrays.asList(id), |
| 104 | + this.exchange.getResponse().getHeaders().get(headerName)); |
140 | 105 | }
|
141 | 106 |
|
142 |
| - @Test |
143 |
| - public void multipleSessionIds() throws Exception { |
144 |
| - UUID sessionId = UUID.randomUUID(); |
145 |
| - DefaultWebSession existing = createDefaultWebSession(sessionId); |
146 |
| - this.manager.getSessionStore().storeSession(existing); |
147 |
| - this.manager.getSessionStore().storeSession(createDefaultWebSession(UUID.randomUUID())); |
148 |
| - this.manager.getSessionStore().storeSession(createDefaultWebSession(UUID.randomUUID())); |
149 |
| - |
150 |
| - this.exchange = this.exchange.mutate() |
151 |
| - .request(this.exchange.getRequest().mutate() |
152 |
| - .header("SESSION", sessionId.toString()) |
153 |
| - .build()) |
154 |
| - .build(); |
155 |
| - |
156 |
| - WebSession actual = this.manager.getSession(this.exchange).block(); |
157 |
| - assertNotNull(actual); |
158 |
| - assertEquals(existing.getId(), actual.getId()); |
| 107 | + @Test(expected = IllegalArgumentException.class) |
| 108 | + public void setSessionIdWhenNullIdThenIllegalArgumentException() { |
| 109 | + String id = null; |
| 110 | + |
| 111 | + this.idResolver.setSessionId(this.exchange, id); |
159 | 112 | }
|
160 | 113 |
|
161 | 114 | @Test
|
162 |
| - public void alternateHeaderName() throws Exception { |
163 |
| - this.idResolver.setHeaderName("alternateHeaderName"); |
164 |
| - |
165 |
| - UUID sessionId = UUID.randomUUID(); |
166 |
| - DefaultWebSession existing = createDefaultWebSession(sessionId); |
167 |
| - this.manager.getSessionStore().storeSession(existing); |
168 |
| - |
169 |
| - this.exchange = this.exchange.mutate() |
170 |
| - .request(this.exchange.getRequest().mutate() |
171 |
| - .header("alternateHeaderName", sessionId.toString()) |
172 |
| - .build()) |
173 |
| - .build(); |
174 |
| - |
175 |
| - WebSession actual = this.manager.getSession(this.exchange).block(); |
176 |
| - assertNotNull(actual); |
177 |
| - assertEquals(existing.getId(), actual.getId()); |
| 115 | + public void resolveSessionIdsWhenNoIdsThenEmpty() { |
| 116 | + List<String> ids = this.idResolver.resolveSessionIds(this.exchange); |
| 117 | + |
| 118 | + assertTrue(ids.isEmpty()); |
178 | 119 | }
|
179 | 120 |
|
180 |
| - private DefaultWebSession createDefaultWebSession(UUID sessionId) { |
181 |
| - return new DefaultWebSession(() -> sessionId, CLOCK, (s, session) -> Mono.empty(), s -> Mono.empty()); |
| 121 | + @Test |
| 122 | + public void resolveSessionIdsWhenIdThenIdFound() { |
| 123 | + String id = "123"; |
| 124 | + this.exchange = MockServerHttpRequest.get("/path") |
| 125 | + .header(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME, id) |
| 126 | + .toExchange(); |
| 127 | + |
| 128 | + List<String> ids = this.idResolver.resolveSessionIds(this.exchange); |
| 129 | + |
| 130 | + assertEquals(Arrays.asList(id), ids); |
182 | 131 | }
|
183 | 132 |
|
| 133 | + @Test |
| 134 | + public void resolveSessionIdsWhenMultipleIdsThenIdsFound() { |
| 135 | + String id1 = "123"; |
| 136 | + String id2 = "abc"; |
| 137 | + this.exchange = MockServerHttpRequest.get("/path") |
| 138 | + .header(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME, id1, id2) |
| 139 | + .toExchange(); |
| 140 | + |
| 141 | + List<String> ids = this.idResolver.resolveSessionIds(this.exchange); |
| 142 | + |
| 143 | + assertEquals(Arrays.asList(id1, id2), ids); |
| 144 | + } |
184 | 145 | }
|
0 commit comments