Add NegatedServerWebExchangeMatcher

Fixes: gh-5170
This commit is contained in:
Tao Qian 2018-03-28 12:28:09 -07:00 committed by Rob Winch
parent 234c20eb30
commit d83b67e4cb
2 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,49 @@
/*
* Copyright 2002-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.web.server.util.matcher;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* @author Tao Qian
* @since 5.0
*/
public class NegatedServerWebExchangeMatcher implements ServerWebExchangeMatcher {
private final ServerWebExchangeMatcher matcher;
public NegatedServerWebExchangeMatcher(ServerWebExchangeMatcher matcher) {
Assert.notNull(matcher, "matcher cannot be null");
this.matcher = matcher;
}
/* (non-Javadoc)
* @see org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher#matches(org.springframework.web.server.ServerWebExchange)
*/
@Override
public Mono<MatchResult> matches(ServerWebExchange exchange) {
return matcher.matches(exchange)
.flatMap(m -> m.isMatch() ? MatchResult.notMatch() : MatchResult.match());
}
@Override
public String toString() {
return "NegatedServerWebExchangeMatcher{" +
"matcher=" + matcher +
'}';
}
}

View File

@ -0,0 +1,71 @@
/*
* Copyright 2002-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.web.server.util.matcher;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.web.server.ServerWebExchange;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Tao Qian
* @since 5.0
*/
@RunWith(MockitoJUnitRunner.class)
public class NegatedServerWebExchangeMatcherTests {
@Mock
ServerWebExchange exchange;
@Mock
ServerWebExchangeMatcher matcher1;
NegatedServerWebExchangeMatcher matcher;
@Before
public void setUp() throws Exception {
matcher = new NegatedServerWebExchangeMatcher(matcher1);
}
@Test
public void matchesWhenFalseThenTrue() throws Exception {
when(matcher1.matches(exchange)).thenReturn(ServerWebExchangeMatcher.MatchResult.notMatch());
ServerWebExchangeMatcher.MatchResult matches = matcher.matches(exchange).block();
assertThat(matches.isMatch()).isTrue();
assertThat(matches.getVariables()).isEmpty();
verify(matcher1).matches(exchange);
}
@Test
public void matchesWhenTrueThenFalse() throws Exception {
when(matcher1.matches(exchange)).thenReturn(ServerWebExchangeMatcher.MatchResult.match());
ServerWebExchangeMatcher.MatchResult matches = matcher.matches(exchange).block();
assertThat(matches.isMatch()).isFalse();
assertThat(matches.getVariables()).isEmpty();
verify(matcher1).matches(exchange);
}
}