2017-05-17 04:38:07 +08:00
|
|
|
/*
|
|
|
|
|
*
|
2017-05-20 03:40:40 +08:00
|
|
|
* * 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.
|
2017-05-17 04:38:07 +08:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
package sample;
|
|
|
|
|
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
2017-09-13 23:39:42 +08:00
|
|
|
|
2017-05-20 03:40:40 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2017-05-24 05:29:30 +08:00
|
|
|
import org.springframework.security.web.server.WebFilterChainFilter;
|
2017-05-20 03:40:40 +08:00
|
|
|
import org.springframework.test.context.ActiveProfiles;
|
2017-05-17 04:38:07 +08:00
|
|
|
import org.springframework.test.context.ContextConfiguration;
|
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
|
|
|
|
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
|
2017-05-24 05:29:30 +08:00
|
|
|
import org.springframework.web.reactive.function.server.RouterFunction;
|
2017-05-17 04:38:07 +08:00
|
|
|
|
2017-09-13 23:39:42 +08:00
|
|
|
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser;
|
2017-07-14 09:47:09 +08:00
|
|
|
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
|
2017-05-17 04:38:07 +08:00
|
|
|
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Rob Winch
|
|
|
|
|
* @since 5.0
|
|
|
|
|
*/
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
@ContextConfiguration(classes = HelloWebfluxFnApplication.class)
|
2017-05-20 03:40:40 +08:00
|
|
|
@ActiveProfiles("test")
|
2017-05-17 04:38:07 +08:00
|
|
|
public class HelloWebfluxFnApplicationTests {
|
2017-05-20 03:40:40 +08:00
|
|
|
@Autowired
|
2017-05-24 05:29:30 +08:00
|
|
|
RouterFunction<?> routerFunction;
|
|
|
|
|
@Autowired
|
|
|
|
|
WebFilterChainFilter springSecurityFilterChain;
|
2017-05-17 04:38:07 +08:00
|
|
|
|
|
|
|
|
WebTestClient rest;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setup() {
|
2017-05-24 05:29:30 +08:00
|
|
|
this.rest = WebTestClient
|
2017-09-13 23:39:42 +08:00
|
|
|
.bindToRouterFunction(this.routerFunction)
|
|
|
|
|
.webFilter(this.springSecurityFilterChain)
|
|
|
|
|
.apply(springSecurity())
|
2017-05-24 05:29:30 +08:00
|
|
|
.build();
|
2017-05-17 04:38:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2017-09-13 23:39:42 +08:00
|
|
|
public void basicWhenNoCredentialsThenUnauthorized() throws Exception {
|
2017-05-17 04:38:07 +08:00
|
|
|
this.rest
|
|
|
|
|
.get()
|
2017-09-13 23:39:42 +08:00
|
|
|
.uri("/")
|
2017-05-17 04:38:07 +08:00
|
|
|
.exchange()
|
|
|
|
|
.expectStatus().isUnauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2017-09-13 23:39:42 +08:00
|
|
|
public void basicWhenValidCredentialsThenOk() throws Exception {
|
2017-05-17 04:38:07 +08:00
|
|
|
this.rest
|
2017-06-21 05:20:22 +08:00
|
|
|
.mutate()
|
2017-09-13 23:39:42 +08:00
|
|
|
.filter(userCredentials())
|
2017-06-21 05:20:22 +08:00
|
|
|
.build()
|
2017-05-17 04:38:07 +08:00
|
|
|
.get()
|
2017-09-13 23:39:42 +08:00
|
|
|
.uri("/")
|
2017-05-17 04:38:07 +08:00
|
|
|
.exchange()
|
|
|
|
|
.expectStatus().isOk()
|
2017-09-13 23:39:42 +08:00
|
|
|
.expectBody().json("{\"message\":\"Hello user!\"}");
|
2017-05-17 04:38:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2017-09-13 23:39:42 +08:00
|
|
|
public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
|
2017-05-17 04:38:07 +08:00
|
|
|
this.rest
|
2017-06-21 05:20:22 +08:00
|
|
|
.mutate()
|
2017-05-17 04:38:07 +08:00
|
|
|
.filter(invalidPassword())
|
2017-06-21 05:20:22 +08:00
|
|
|
.build()
|
2017-05-17 04:38:07 +08:00
|
|
|
.get()
|
2017-09-13 23:39:42 +08:00
|
|
|
.uri("/")
|
2017-05-17 04:38:07 +08:00
|
|
|
.exchange()
|
|
|
|
|
.expectStatus().isUnauthorized()
|
|
|
|
|
.expectBody().isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-14 09:47:09 +08:00
|
|
|
@Test
|
2017-09-13 23:39:42 +08:00
|
|
|
public void mockSupportWhenValidMockUserThenOk() throws Exception {
|
2017-05-17 04:38:07 +08:00
|
|
|
this.rest
|
2017-09-13 23:39:42 +08:00
|
|
|
.mutateWith(mockUser())
|
2017-05-17 04:38:07 +08:00
|
|
|
.get()
|
2017-09-13 23:39:42 +08:00
|
|
|
.uri("/")
|
2017-05-17 04:38:07 +08:00
|
|
|
.exchange()
|
|
|
|
|
.expectStatus().isOk()
|
2017-09-13 23:39:42 +08:00
|
|
|
.expectBody().json("{\"message\":\"Hello user!\"}");
|
2017-05-17 04:38:07 +08:00
|
|
|
|
|
|
|
|
this.rest
|
2017-05-20 03:40:40 +08:00
|
|
|
.get()
|
2017-09-13 23:39:42 +08:00
|
|
|
.uri("/")
|
2017-05-20 03:40:40 +08:00
|
|
|
.exchange()
|
2017-09-13 23:39:42 +08:00
|
|
|
.expectStatus().isUnauthorized();
|
2017-05-17 04:38:07 +08:00
|
|
|
}
|
|
|
|
|
|
2017-09-13 23:39:42 +08:00
|
|
|
private ExchangeFilterFunction userCredentials() {
|
|
|
|
|
return basicAuthentication("user","user");
|
2017-05-17 04:38:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ExchangeFilterFunction invalidPassword() {
|
2017-09-13 23:39:42 +08:00
|
|
|
return basicAuthentication("user","INVALID");
|
2017-05-17 04:38:07 +08:00
|
|
|
}
|
|
|
|
|
}
|