Polish HeaderWebSessionIdResolverTests

See gh-26675
This commit is contained in:
Sam Brannen
2021-03-15 12:12:53 +01:00
parent 2728a9b931
commit 70f0895f96

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2021 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.
@@ -15,11 +15,6 @@
*/ */
package org.springframework.web.server.session; package org.springframework.web.server.session;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
@@ -35,106 +30,92 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* @author Greg Turnquist * @author Greg Turnquist
* @author Rob Winch * @author Rob Winch
*/ */
public class HeaderWebSessionIdResolverTests { class HeaderWebSessionIdResolverTests {
private HeaderWebSessionIdResolver idResolver;
private ServerWebExchange exchange; private final HeaderWebSessionIdResolver idResolver = new HeaderWebSessionIdResolver();
@BeforeEach private ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path"));
public void setUp() {
this.idResolver = new HeaderWebSessionIdResolver();
this.exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path")); @Test
void expireWhenValidThenSetsEmptyHeader() {
this.idResolver.expireSession(this.exchange);
assertThat(this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)).containsExactly("");
} }
@Test @Test
public void expireWhenValidThenSetsEmptyHeader() { void expireWhenMultipleInvocationThenSetsSingleEmptyHeader() {
this.idResolver.expireSession(this.exchange);
this.idResolver.expireSession(this.exchange); this.idResolver.expireSession(this.exchange);
assertThat(this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)).isEqualTo(Collections.singletonList("")); assertThat(this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)).containsExactly("");
} }
@Test @Test
public void expireWhenMultipleInvocationThenSetsSingleEmptyHeader() { void expireWhenAfterSetSessionIdThenSetsEmptyHeader() {
this.idResolver.expireSession(this.exchange);
this.idResolver.expireSession(this.exchange);
assertThat(this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)).isEqualTo(Collections.singletonList(""));
}
@Test
public void expireWhenAfterSetSessionIdThenSetsEmptyHeader() {
this.idResolver.setSessionId(this.exchange, "123"); this.idResolver.setSessionId(this.exchange, "123");
this.idResolver.expireSession(this.exchange); this.idResolver.expireSession(this.exchange);
assertThat(this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)).isEqualTo(Collections.singletonList("")); assertThat(this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)).containsExactly("");
} }
@Test @Test
public void setSessionIdWhenValidThenSetsHeader() { void setSessionIdWhenValidThenSetsHeader() {
String id = "123"; String id = "123";
this.idResolver.setSessionId(this.exchange, id); this.idResolver.setSessionId(this.exchange, id);
assertThat(this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)).isEqualTo(Collections.singletonList(id)); assertThat(this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)).containsExactly(id);
} }
@Test @Test
public void setSessionIdWhenMultipleThenSetsSingleHeader() { void setSessionIdWhenMultipleThenSetsSingleHeader() {
String id = "123"; String id = "123";
this.idResolver.setSessionId(this.exchange, "overriddenByNextInvocation"); this.idResolver.setSessionId(this.exchange, "overriddenByNextInvocation");
this.idResolver.setSessionId(this.exchange, id); this.idResolver.setSessionId(this.exchange, id);
assertThat(this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)).isEqualTo(Collections.singletonList(id)); assertThat(this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)).containsExactly(id);
} }
@Test @Test
public void setSessionIdWhenCustomHeaderNameThenSetsHeader() { void setSessionIdWhenCustomHeaderNameThenSetsHeader() {
String headerName = "x-auth"; String headerName = "x-auth";
String id = "123"; String id = "123";
this.idResolver.setHeaderName(headerName); this.idResolver.setHeaderName(headerName);
this.idResolver.setSessionId(this.exchange, id); this.idResolver.setSessionId(this.exchange, id);
assertThat(this.exchange.getResponse().getHeaders().get(headerName)).isEqualTo(Collections.singletonList(id)); assertThat(this.exchange.getResponse().getHeaders().get(headerName)).containsExactly(id);
} }
@Test @Test
public void setSessionIdWhenNullIdThenIllegalArgumentException() { void setSessionIdWhenNullIdThenIllegalArgumentException() {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() ->
this.idResolver.setSessionId(this.exchange, null)); this.idResolver.setSessionId(this.exchange, null));
} }
@Test @Test
public void resolveSessionIdsWhenNoIdsThenEmpty() { void resolveSessionIdsWhenNoIdsThenEmpty() {
List<String> ids = this.idResolver.resolveSessionIds(this.exchange); assertThat(this.idResolver.resolveSessionIds(this.exchange)).isEmpty();
assertThat(ids.isEmpty()).isTrue();
} }
@Test @Test
public void resolveSessionIdsWhenIdThenIdFound() { void resolveSessionIdsWhenIdThenIdFound() {
String id = "123"; String id = "123";
this.exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path") this.exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path")
.header(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME, id)); .header(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME, id));
List<String> ids = this.idResolver.resolveSessionIds(this.exchange); assertThat(this.idResolver.resolveSessionIds(this.exchange)).containsExactly(id);
assertThat(ids).isEqualTo(Collections.singletonList(id));
} }
@Test @Test
public void resolveSessionIdsWhenMultipleIdsThenIdsFound() { void resolveSessionIdsWhenMultipleIdsThenIdsFound() {
String id1 = "123"; String id1 = "123";
String id2 = "abc"; String id2 = "abc";
this.exchange = MockServerWebExchange.from( this.exchange = MockServerWebExchange.from(
MockServerHttpRequest.get("/path") MockServerHttpRequest.get("/path")
.header(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME, id1, id2)); .header(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME, id1, id2));
List<String> ids = this.idResolver.resolveSessionIds(this.exchange); assertThat(this.idResolver.resolveSessionIds(this.exchange)).containsExactly(id1, id2);
assertThat(ids).isEqualTo(Arrays.asList(id1, id2));
} }
} }