Skip to content

Commit

Permalink
Polish HeaderWebSessionIdResolverTests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Mar 15, 2021
1 parent 2728a9b commit 70f0895
Showing 1 changed file with 25 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -15,11 +15,6 @@
*/
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.springframework.web.server.ServerWebExchange;
Expand All @@ -35,106 +30,92 @@
* @author Greg Turnquist
* @author Rob Winch
*/
public class HeaderWebSessionIdResolverTests {
private HeaderWebSessionIdResolver idResolver;
class HeaderWebSessionIdResolverTests {

private ServerWebExchange exchange;
private final HeaderWebSessionIdResolver idResolver = new HeaderWebSessionIdResolver();

private ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path"));

@BeforeEach
public void setUp() {
this.idResolver = new HeaderWebSessionIdResolver();
this.exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path"));
}

@Test
public void expireWhenValidThenSetsEmptyHeader() {
void expireWhenValidThenSetsEmptyHeader() {
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
public void expireWhenMultipleInvocationThenSetsSingleEmptyHeader() {
void expireWhenMultipleInvocationThenSetsSingleEmptyHeader() {
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
public void expireWhenAfterSetSessionIdThenSetsEmptyHeader() {
void expireWhenAfterSetSessionIdThenSetsEmptyHeader() {
this.idResolver.setSessionId(this.exchange, "123");

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
public void setSessionIdWhenValidThenSetsHeader() {
void setSessionIdWhenValidThenSetsHeader() {
String id = "123";

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
public void setSessionIdWhenMultipleThenSetsSingleHeader() {
void setSessionIdWhenMultipleThenSetsSingleHeader() {
String id = "123";
this.idResolver.setSessionId(this.exchange, "overriddenByNextInvocation");

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
public void setSessionIdWhenCustomHeaderNameThenSetsHeader() {
void setSessionIdWhenCustomHeaderNameThenSetsHeader() {
String headerName = "x-auth";
String id = "123";
this.idResolver.setHeaderName(headerName);

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
public void setSessionIdWhenNullIdThenIllegalArgumentException() {
void setSessionIdWhenNullIdThenIllegalArgumentException() {
assertThatIllegalArgumentException().isThrownBy(() ->
this.idResolver.setSessionId(this.exchange, null));
}

@Test
public void resolveSessionIdsWhenNoIdsThenEmpty() {
List<String> ids = this.idResolver.resolveSessionIds(this.exchange);

assertThat(ids.isEmpty()).isTrue();
void resolveSessionIdsWhenNoIdsThenEmpty() {
assertThat(this.idResolver.resolveSessionIds(this.exchange)).isEmpty();
}

@Test
public void resolveSessionIdsWhenIdThenIdFound() {
void resolveSessionIdsWhenIdThenIdFound() {
String id = "123";
this.exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path")
.header(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME, id));

List<String> ids = this.idResolver.resolveSessionIds(this.exchange);

assertThat(ids).isEqualTo(Collections.singletonList(id));
assertThat(this.idResolver.resolveSessionIds(this.exchange)).containsExactly(id);
}

@Test
public void resolveSessionIdsWhenMultipleIdsThenIdsFound() {
void resolveSessionIdsWhenMultipleIdsThenIdsFound() {
String id1 = "123";
String id2 = "abc";
this.exchange = MockServerWebExchange.from(
MockServerHttpRequest.get("/path")
.header(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME, id1, id2));

List<String> ids = this.idResolver.resolveSessionIds(this.exchange);

assertThat(ids).isEqualTo(Arrays.asList(id1, id2));
assertThat(this.idResolver.resolveSessionIds(this.exchange)).containsExactly(id1, id2);
}

}

0 comments on commit 70f0895

Please sign in to comment.