1
0
mirror of synced 2026-08-03 08:51:58 +00:00

Add WebTestClient test support

SecurityExchangeMutators

Fixes gh-4343
This commit is contained in:
Rob Winch
2017-05-19 14:40:40 -05:00
parent 7b703a3b98
commit 7bc98db23c
9 changed files with 772 additions and 10 deletions
@@ -10,6 +10,7 @@ dependencies {
compile 'org.springframework:spring-context'
compile 'org.springframework:spring-webflux'
testCompile project(':spring-security-test')
testCompile 'io.projectreactor.addons:reactor-test'
testCompile 'org.skyscreamer:jsonassert'
testCompile 'org.springframework:spring-test'
@@ -0,0 +1,238 @@
/*
*
* * Copyright 2002-2017 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 sample;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.security.web.server.header.ContentTypeOptionsHttpHeadersWriter;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.ExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import java.nio.charset.Charset;
import java.util.Base64;
import static org.springframework.security.test.web.reactive.server.SecurityExchangeMutators.withUser;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
/**
* @author Rob Winch
* @since 5.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = HelloWebfluxApplication.class)
@ActiveProfiles("test")
public class HelloWebfluxApplicationTests {
@Autowired
ApplicationContext context;
WebTestClient rest;
@Before
public void setup() {
this.rest = WebTestClient.bindToApplicationContext(context).build();
}
@Test
public void basicRequired() throws Exception {
this.rest
.get()
.uri("/users")
.exchange()
.expectStatus().isUnauthorized();
}
@Test
public void basicWorks() throws Exception {
this.rest
.filter(robsCredentials())
.get()
.uri("/users")
.exchange()
.expectStatus().isOk()
.expectBody().json("[{\"id\":null,\"username\":\"rob\",\"password\":\"rob\",\"firstname\":\"Rob\",\"lastname\":\"Winch\"},{\"id\":null,\"username\":\"admin\",\"password\":\"admin\",\"firstname\":\"Admin\",\"lastname\":\"User\"}]");
}
@Test
public void basicWhenPasswordInvalid401() throws Exception {
this.rest
.filter(invalidPassword())
.get()
.uri("/users")
.exchange()
.expectStatus().isUnauthorized()
.expectBody().isEmpty();
}
@Test
public void authorizationAdmin403() throws Exception {
this.rest
.filter(robsCredentials())
.get()
.uri("/admin")
.exchange()
.expectStatus().isEqualTo(HttpStatus.FORBIDDEN)
.expectBody().isEmpty();
}
@Test
public void authorizationAdmin200() throws Exception {
this.rest
.filter(adminCredentials())
.get()
.uri("/admin")
.exchange()
.expectStatus().isOk();
}
@Test
public void basicMissingUser401() throws Exception {
this.rest
.filter(basicAuthentication("missing-user", "password"))
.get()
.uri("/admin")
.exchange()
.expectStatus().isUnauthorized();
}
@Test
public void basicInvalidPassword401() throws Exception {
this.rest
.filter(invalidPassword())
.get()
.uri("/admin")
.exchange()
.expectStatus().isUnauthorized();
}
@Test
public void basicInvalidParts401() throws Exception {
this.rest
.get()
.uri("/admin")
.header("Authorization", "Basic " + base64Encode("no colon"))
.exchange()
.expectStatus().isUnauthorized();
}
@Test
public void sessionWorks() throws Exception {
ExchangeResult result = this.rest
.filter(robsCredentials())
.get()
.uri("/users")
.exchange()
.returnResult(String.class);
ResponseCookie session = result.getResponseCookies().getFirst("SESSION");
this.rest
.get()
.uri("/users")
.cookie(session.getName(), session.getValue())
.exchange()
.expectStatus().isOk();
}
@Test
public void mockSupport() throws Exception {
this.rest
.exchangeMutator( withUser() )
.get()
.uri("/users")
.exchange()
.expectStatus().isOk();
this.rest
.get()
.uri("/users")
.exchange()
.expectStatus().isUnauthorized();
}
@Test
public void me() throws Exception {
this.rest
.filter(robsCredentials())
.get()
.uri("/me")
.exchange()
.expectStatus().isOk()
.expectBody().json("{\"username\" : \"rob\"}");
}
@Test
public void monoMe() throws Exception {
this.rest
.filter(robsCredentials())
.get()
.uri("/mono/me")
.exchange()
.expectStatus().isOk()
.expectBody().json("{\"username\" : \"rob\"}");
}
@Test
public void principal() throws Exception {
this.rest
.filter(robsCredentials())
.get()
.uri("/principal")
.exchange()
.expectStatus().isOk()
.expectBody().json("{\"username\" : \"rob\"}");
}
@Test
public void headers() throws Exception {
this.rest
.filter(robsCredentials())
.get()
.uri("/principal")
.exchange()
.expectHeader().valueEquals(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate")
.expectHeader().valueEquals(HttpHeaders.EXPIRES, "0")
.expectHeader().valueEquals(HttpHeaders.PRAGMA, "no-cache")
.expectHeader().valueEquals(ContentTypeOptionsHttpHeadersWriter.X_CONTENT_OPTIONS, ContentTypeOptionsHttpHeadersWriter.NOSNIFF);
}
private ExchangeFilterFunction robsCredentials() {
return basicAuthentication("rob","rob");
}
private ExchangeFilterFunction invalidPassword() {
return basicAuthentication("rob","INVALID");
}
private ExchangeFilterFunction adminCredentials() {
return basicAuthentication("admin","admin");
}
private String base64Encode(String value) {
return Base64.getEncoder().encodeToString(value.getBytes(Charset.defaultCharset()));
}
}
@@ -10,6 +10,7 @@ dependencies {
compile 'org.springframework:spring-context'
compile 'org.springframework:spring-webflux'
testCompile project(':spring-security-test')
testCompile 'io.projectreactor.addons:reactor-test'
testCompile 'org.skyscreamer:jsonassert'
testCompile 'org.springframework:spring-test'
@@ -42,7 +42,7 @@ import static org.springframework.web.reactive.function.client.ExchangeFilterFun
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = HelloWebfluxFnApplication.class)
@TestPropertySource(properties = "server.port=0")
public class HelloWebfluxFnApplicationTests {
public class HelloWebfluxFnApplicationITests {
@Value("#{@nettyContext.address().getPort()}")
int port;
@@ -19,10 +19,7 @@ package sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.*;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
@@ -72,8 +69,16 @@ public class HelloWebfluxFnApplication {
}
}
@Profile("default")
@Bean
public NettyContext nettyContext(UserController userController, WebFilter springSecurityFilterChain) {
public NettyContext nettyContext(HttpHandler handler) {
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", port);
return httpServer.newHandler(adapter).block();
}
@Bean
public HttpHandler httpHandler(UserController userController, WebFilter springSecurityFilterChain) {
RouterFunction<ServerResponse> route = route(
GET("/principal"), userController::principal).andRoute(
GET("/users"), userController::users).andRoute(
@@ -82,10 +87,7 @@ public class HelloWebfluxFnApplication {
HandlerStrategies handlerStrategies = HandlerStrategies.builder()
.webFilter(springSecurityFilterChain).build();
HttpHandler handler = RouterFunctions.toHttpHandler(route, handlerStrategies);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", port);
return httpServer.newHandler(adapter).block();
return RouterFunctions.toHttpHandler(route, handlerStrategies);
}
@Bean
@@ -0,0 +1,219 @@
/*
*
* * Copyright 2002-2017 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 sample;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.security.web.server.header.ContentTypeOptionsHttpHeadersWriter;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.ExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import java.nio.charset.Charset;
import java.util.Base64;
import static org.springframework.security.test.web.reactive.server.SecurityExchangeMutators.withUser;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
/**
* @author Rob Winch
* @since 5.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = HelloWebfluxFnApplication.class)
@ActiveProfiles("test")
public class HelloWebfluxFnApplicationTests {
@Autowired
HttpHandler handler;
WebTestClient rest;
@Before
public void setup() {
this.rest = WebTestClient.bindToHttpHandler(handler).build();
}
@Test
public void basicRequired() throws Exception {
this.rest
.get()
.uri("/users")
.exchange()
.expectStatus().isUnauthorized();
}
@Test
public void basicWorks() throws Exception {
this.rest
.filter(robsCredentials())
.get()
.uri("/users")
.exchange()
.expectStatus().isOk()
.expectBody().json("[{\"id\":null,\"username\":\"rob\",\"password\":\"rob\",\"firstname\":\"Rob\",\"lastname\":\"Winch\"},{\"id\":null,\"username\":\"admin\",\"password\":\"admin\",\"firstname\":\"Admin\",\"lastname\":\"User\"}]");
}
@Test
public void basicWhenPasswordInvalid401() throws Exception {
this.rest
.filter(invalidPassword())
.get()
.uri("/users")
.exchange()
.expectStatus().isUnauthorized()
.expectBody().isEmpty();
}
@Test
public void authorizationAdmin403() throws Exception {
this.rest
.filter(robsCredentials())
.get()
.uri("/admin")
.exchange()
.expectStatus().isEqualTo(HttpStatus.FORBIDDEN)
.expectBody().isEmpty();
}
@Test
public void authorizationAdmin200() throws Exception {
this.rest
.filter(adminCredentials())
.get()
.uri("/admin")
.exchange()
.expectStatus().isOk();
}
@Test
public void basicMissingUser401() throws Exception {
this.rest
.filter(basicAuthentication("missing-user", "password"))
.get()
.uri("/admin")
.exchange()
.expectStatus().isUnauthorized();
}
@Test
public void basicInvalidPassword401() throws Exception {
this.rest
.filter(invalidPassword())
.get()
.uri("/admin")
.exchange()
.expectStatus().isUnauthorized();
}
@Test
public void basicInvalidParts401() throws Exception {
this.rest
.get()
.uri("/admin")
.header("Authorization", "Basic " + base64Encode("no colon"))
.exchange()
.expectStatus().isUnauthorized();
}
@Test
public void sessionWorks() throws Exception {
ExchangeResult result = this.rest
.filter(robsCredentials())
.get()
.uri("/users")
.exchange()
.returnResult(String.class);
ResponseCookie session = result.getResponseCookies().getFirst("SESSION");
this.rest
.get()
.uri("/users")
.cookie(session.getName(), session.getValue())
.exchange()
.expectStatus().isOk();
}
@Ignore
@Test
public void mockSupport() throws Exception {
this.rest
.exchangeMutator( withUser() )
.get()
.uri("/users")
.exchange()
.expectStatus().isOk();
this.rest
.get()
.uri("/users")
.exchange()
.expectStatus().isUnauthorized();
}
@Test
public void principal() throws Exception {
this.rest
.filter(robsCredentials())
.get()
.uri("/principal")
.exchange()
.expectStatus().isOk()
.expectBody().json("{\"username\" : \"rob\"}");
}
@Test
public void headers() throws Exception {
this.rest
.filter(robsCredentials())
.get()
.uri("/principal")
.exchange()
.expectHeader().valueEquals(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate")
.expectHeader().valueEquals(HttpHeaders.EXPIRES, "0")
.expectHeader().valueEquals(HttpHeaders.PRAGMA, "no-cache")
.expectHeader().valueEquals(ContentTypeOptionsHttpHeadersWriter.X_CONTENT_OPTIONS, ContentTypeOptionsHttpHeadersWriter.NOSNIFF);
}
private ExchangeFilterFunction robsCredentials() {
return basicAuthentication("rob","rob");
}
private ExchangeFilterFunction invalidPassword() {
return basicAuthentication("rob","INVALID");
}
private ExchangeFilterFunction adminCredentials() {
return basicAuthentication("admin","admin");
}
private String base64Encode(String value) {
return Base64.getEncoder().encodeToString(value.getBytes(Charset.defaultCharset()));
}
}