Polish ServerWebExchangeDelegatingServerHttpHeadersWriter

Issue gh-11073
This commit is contained in:
Marcus Da Coregio 2022-05-06 09:42:30 -03:00
parent 0e2fc51bad
commit 195d767d98
2 changed files with 33 additions and 29 deletions

View File

@ -43,6 +43,8 @@ public final class ServerWebExchangeDelegatingServerHttpHeadersWriter implements
public ServerWebExchangeDelegatingServerHttpHeadersWriter( public ServerWebExchangeDelegatingServerHttpHeadersWriter(
ServerWebExchangeMatcherEntry<ServerHttpHeadersWriter> headersWriter) { ServerWebExchangeMatcherEntry<ServerHttpHeadersWriter> headersWriter) {
Assert.notNull(headersWriter, "headersWriter cannot be null"); Assert.notNull(headersWriter, "headersWriter cannot be null");
Assert.notNull(headersWriter.getMatcher(), "webExchangeMatcher cannot be null");
Assert.notNull(headersWriter.getEntry(), "delegateHeadersWriter cannot be null");
this.headersWriter = headersWriter; this.headersWriter = headersWriter;
} }
@ -55,9 +57,7 @@ public final class ServerWebExchangeDelegatingServerHttpHeadersWriter implements
*/ */
public ServerWebExchangeDelegatingServerHttpHeadersWriter(ServerWebExchangeMatcher webExchangeMatcher, public ServerWebExchangeDelegatingServerHttpHeadersWriter(ServerWebExchangeMatcher webExchangeMatcher,
ServerHttpHeadersWriter delegateHeadersWriter) { ServerHttpHeadersWriter delegateHeadersWriter) {
Assert.notNull(webExchangeMatcher, "webExchangeMatcher cannot be null"); this(new ServerWebExchangeMatcherEntry<>(webExchangeMatcher, delegateHeadersWriter));
Assert.notNull(delegateHeadersWriter, "delegateHeadersWriter cannot be null");
this.headersWriter = new ServerWebExchangeMatcherEntry<>(webExchangeMatcher, delegateHeadersWriter);
} }
@Override @Override

View File

@ -17,7 +17,6 @@
package org.springframework.security.web.server.header; package org.springframework.security.web.server.header;
import java.util.Collections; import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -49,9 +48,6 @@ public class ServerWebExchangeDelegatingServerHttpHeadersWriterTests {
@Mock @Mock
private ServerHttpHeadersWriter delegate; private ServerHttpHeadersWriter delegate;
@Mock
private ServerWebExchangeMatcherEntry<ServerHttpHeadersWriter> matcherEntry;
private ServerWebExchange exchange; private ServerWebExchange exchange;
private ServerWebExchangeDelegatingServerHttpHeadersWriter headerWriter; private ServerWebExchangeDelegatingServerHttpHeadersWriter headerWriter;
@ -63,49 +59,57 @@ public class ServerWebExchangeDelegatingServerHttpHeadersWriterTests {
} }
@Test @Test
public void constructorNullWebExchangeMatcher() { public void constructorWhenNullWebExchangeMatcherThenException() {
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
.isThrownBy(() -> new ServerWebExchangeDelegatingServerHttpHeadersWriter(null, this.delegate)); .isThrownBy(() -> new ServerWebExchangeDelegatingServerHttpHeadersWriter(null, this.delegate))
.withMessage("webExchangeMatcher cannot be null");
} }
@Test @Test
public void constructorNullWebExchangeMatcherEntry() { public void constructorWhenNullWebExchangeMatcherEntryThenException() {
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
.isThrownBy(() -> new ServerWebExchangeDelegatingServerHttpHeadersWriter(null)); .isThrownBy(() -> new ServerWebExchangeDelegatingServerHttpHeadersWriter(null))
.withMessage("headersWriter cannot be null");
} }
@Test @Test
public void constructorNullDelegate() { public void constructorWhenNullDelegateHeadersWriterThenException() {
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
.isThrownBy(() -> new ServerWebExchangeDelegatingServerHttpHeadersWriter(this.matcher, null)); .isThrownBy(() -> new ServerWebExchangeDelegatingServerHttpHeadersWriter(this.matcher, null))
.withMessage("delegateHeadersWriter cannot be null");
} }
@Test @Test
public void writeHeadersOnMatch() { public void constructorWhenEntryWithNullMatcherThenException() {
Map<String, Object> params = Collections.singletonMap("foo", "bar"); assertThatIllegalArgumentException()
given(this.matcher.matches(this.exchange)).willReturn(ServerWebExchangeMatcher.MatchResult.match(params)); .isThrownBy(() -> new ServerWebExchangeDelegatingServerHttpHeadersWriter(
new ServerWebExchangeMatcherEntry<>(null, this.delegate)))
.withMessage("webExchangeMatcher cannot be null");
}
@Test
public void constructorWhenEntryWithNullEntryThenException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new ServerWebExchangeDelegatingServerHttpHeadersWriter(
new ServerWebExchangeMatcherEntry<>(this.matcher, null)))
.withMessage("delegateHeadersWriter cannot be null");
}
@Test
public void writeHeadersWhenMatchThenDelegateWriteHttpHeaders() {
given(this.matcher.matches(this.exchange))
.willReturn(ServerWebExchangeMatcher.MatchResult.match(Collections.emptyMap()));
given(this.delegate.writeHttpHeaders(this.exchange)).willReturn(Mono.empty()); given(this.delegate.writeHttpHeaders(this.exchange)).willReturn(Mono.empty());
this.headerWriter.writeHttpHeaders(this.exchange).block(); this.headerWriter.writeHttpHeaders(this.exchange).block();
verify(this.delegate).writeHttpHeaders(this.exchange); verify(this.delegate).writeHttpHeaders(this.exchange);
} }
@Test @Test
public void writeHeadersOnNoMatch() { public void writeHeadersWhenNoMatchThenDelegateNotCalled() {
given(this.matcher.matches(this.exchange)).willReturn(ServerWebExchangeMatcher.MatchResult.notMatch()); given(this.matcher.matches(this.exchange)).willReturn(ServerWebExchangeMatcher.MatchResult.notMatch());
this.headerWriter.writeHttpHeaders(this.exchange).block(); this.headerWriter.writeHttpHeaders(this.exchange).block();
verify(this.matcher).matches(this.exchange);
verify(this.delegate, times(0)).writeHttpHeaders(this.exchange); verify(this.delegate, times(0)).writeHttpHeaders(this.exchange);
} }
@Test
public void writeHeadersOnMatchWithServerWebExchangeMatcherEntry() {
this.headerWriter = new ServerWebExchangeDelegatingServerHttpHeadersWriter(this.matcherEntry);
given(this.matcherEntry.getMatcher()).willReturn(this.matcher);
given(this.matcherEntry.getEntry()).willReturn(this.delegate);
Map<String, Object> params = Collections.singletonMap("foo", "bar");
given(this.matcher.matches(this.exchange)).willReturn(ServerWebExchangeMatcher.MatchResult.match(params));
given(this.delegate.writeHttpHeaders(this.exchange)).willReturn(Mono.empty());
this.headerWriter.writeHttpHeaders(this.exchange).block();
verify(this.delegate).writeHttpHeaders(this.exchange);
}
} }