2017-05-02 21:19:14 -05: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
|
|
|
|
|
*
|
2019-03-14 15:30:00 -05:00
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
2017-05-02 21:19:14 -05:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2020-07-29 00:21:57 -07:00
|
|
|
|
2017-05-02 21:19:14 -05:00
|
|
|
package sample;
|
|
|
|
|
|
2017-09-13 15:00:42 -05:00
|
|
|
import java.util.function.Consumer;
|
2017-09-13 09:54:32 -05:00
|
|
|
|
2017-05-02 21:19:14 -05:00
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
2018-06-05 10:27:08 -05:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
2018-07-30 14:45:14 -05:00
|
|
|
import org.springframework.http.HttpHeaders;
|
2017-05-02 21:19:14 -05:00
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Rob Winch
|
|
|
|
|
* @since 5.0
|
|
|
|
|
*/
|
|
|
|
|
@RunWith(SpringRunner.class)
|
2018-06-05 10:27:08 -05:00
|
|
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
2017-05-17 15:33:38 -05:00
|
|
|
public class HelloWebfluxApplicationITests {
|
2017-05-02 21:19:14 -05:00
|
|
|
|
2018-06-05 10:27:08 -05:00
|
|
|
@Autowired
|
2018-07-30 14:45:14 -05:00
|
|
|
WebTestClient rest;
|
2017-05-02 21:19:14 -05:00
|
|
|
|
|
|
|
|
@Test
|
2019-08-23 01:03:54 +02:00
|
|
|
public void basicWhenNoCredentialsThenUnauthorized() {
|
2017-05-02 21:19:14 -05:00
|
|
|
this.rest
|
|
|
|
|
.get()
|
2017-09-13 09:54:32 -05:00
|
|
|
.uri("/")
|
2017-05-02 21:19:14 -05:00
|
|
|
.exchange()
|
|
|
|
|
.expectStatus().isUnauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2019-08-23 01:03:54 +02:00
|
|
|
public void basicWhenValidCredentialsThenOk() {
|
2017-05-02 21:19:14 -05:00
|
|
|
this.rest
|
|
|
|
|
.get()
|
2017-09-13 09:54:32 -05:00
|
|
|
.uri("/")
|
2018-07-30 14:45:14 -05:00
|
|
|
.headers(userCredentials())
|
2017-05-02 21:19:14 -05:00
|
|
|
.exchange()
|
|
|
|
|
.expectStatus().isOk()
|
2017-09-13 09:54:32 -05:00
|
|
|
.expectBody().json("{\"message\":\"Hello user!\"}");
|
2017-05-02 21:19:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2019-08-23 01:03:54 +02:00
|
|
|
public void basicWhenInvalidCredentialsThenUnauthorized() {
|
2017-05-02 21:19:14 -05:00
|
|
|
this.rest
|
|
|
|
|
.get()
|
2017-09-13 09:54:32 -05:00
|
|
|
.uri("/")
|
2018-07-30 14:45:14 -05:00
|
|
|
.headers(invalidCredentials())
|
2017-05-02 21:19:14 -05:00
|
|
|
.exchange()
|
|
|
|
|
.expectStatus().isUnauthorized()
|
|
|
|
|
.expectBody().isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-30 14:45:14 -05:00
|
|
|
private Consumer<HttpHeaders> userCredentials() {
|
2020-07-29 18:18:05 -07:00
|
|
|
return (httpHeaders) -> httpHeaders.setBasicAuth("user", "user");
|
2017-05-02 21:19:14 -05:00
|
|
|
}
|
|
|
|
|
|
2018-07-30 14:45:14 -05:00
|
|
|
private Consumer<HttpHeaders> invalidCredentials() {
|
2020-07-29 18:18:05 -07:00
|
|
|
return (httpHeaders) -> httpHeaders.setBasicAuth("user", "INVALID");
|
2017-05-02 21:19:14 -05:00
|
|
|
}
|
|
|
|
|
}
|