|
| 1 | +/* |
| 2 | + * Copyright 2014-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.session.web.http; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.UUID; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 26 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for {@link CompositeHttpSessionIdResolver}. |
| 32 | + * |
| 33 | + * @author Yanming Zhou |
| 34 | + */ |
| 35 | +class CompositeHttpSessionIdResolverTests { |
| 36 | + |
| 37 | + private static final String HEADER_X_AUTH_TOKEN = "X-Auth-Token"; |
| 38 | + |
| 39 | + private static final String HEADER_AUTHENTICATION_INFO = "Authentication-Info"; |
| 40 | + |
| 41 | + private MockHttpServletRequest request; |
| 42 | + |
| 43 | + private MockHttpServletResponse response; |
| 44 | + |
| 45 | + private CompositeHttpSessionIdResolver resolver; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setup() { |
| 49 | + this.request = new MockHttpServletRequest(); |
| 50 | + this.response = new MockHttpServletResponse(); |
| 51 | + this.resolver = new CompositeHttpSessionIdResolver(HeaderHttpSessionIdResolver.xAuthToken(), |
| 52 | + HeaderHttpSessionIdResolver.authenticationInfo()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void getRequestedSessionIdNull() { |
| 57 | + assertThat(this.resolver.resolveSessionIds(this.request)).isEmpty(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void resolveSessionIdsByXAuthToken() { |
| 62 | + String sessionId = UUID.randomUUID().toString(); |
| 63 | + this.request.addHeader(HEADER_X_AUTH_TOKEN, sessionId); |
| 64 | + assertThat(this.resolver.resolveSessionIds(this.request)).isEqualTo(Collections.singletonList(sessionId)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void resolveSessionIdsByAuthenticationInfo() { |
| 69 | + String sessionId = UUID.randomUUID().toString(); |
| 70 | + this.request.addHeader(HEADER_AUTHENTICATION_INFO, sessionId); |
| 71 | + assertThat(this.resolver.resolveSessionIds(this.request)).isEqualTo(Collections.singletonList(sessionId)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void onNewSession() { |
| 76 | + String sessionId = UUID.randomUUID().toString(); |
| 77 | + this.resolver.setSessionId(this.request, this.response, sessionId); |
| 78 | + assertThat(this.response.getHeader(HEADER_X_AUTH_TOKEN)).isEqualTo(sessionId); |
| 79 | + assertThat(this.response.getHeader(HEADER_AUTHENTICATION_INFO)).isEqualTo(sessionId); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void onDeleteSession() { |
| 84 | + this.resolver.expireSession(this.request, this.response); |
| 85 | + assertThat(this.response.getHeader(HEADER_X_AUTH_TOKEN)).isEmpty(); |
| 86 | + assertThat(this.response.getHeader(HEADER_AUTHENTICATION_INFO)).isEmpty(); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments