HelloWebfluxApplicationITests.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2002-2017 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package sample;
  17. import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
  18. import java.util.Map;
  19. import java.util.function.Consumer;
  20. import org.junit.Test;
  21. import org.junit.runner.RunWith;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.boot.test.context.SpringBootTest;
  24. import org.springframework.test.context.junit4.SpringRunner;
  25. import org.springframework.test.web.reactive.server.WebTestClient;
  26. import org.springframework.web.reactive.function.client.ExchangeFilterFunctions;
  27. /**
  28. * @author Rob Winch
  29. * @since 5.0
  30. */
  31. @RunWith(SpringRunner.class)
  32. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  33. public class HelloWebfluxApplicationITests {
  34. WebTestClient rest;
  35. @Autowired
  36. public void setRest(WebTestClient rest) {
  37. this.rest = rest
  38. .mutateWith((b, h, c) -> b.filter(ExchangeFilterFunctions.basicAuthentication()));
  39. }
  40. @Test
  41. public void basicWhenNoCredentialsThenUnauthorized() throws Exception {
  42. this.rest
  43. .get()
  44. .uri("/")
  45. .exchange()
  46. .expectStatus().isUnauthorized();
  47. }
  48. @Test
  49. public void basicWhenValidCredentialsThenOk() throws Exception {
  50. this.rest
  51. .get()
  52. .uri("/")
  53. .attributes(userCredentials())
  54. .exchange()
  55. .expectStatus().isOk()
  56. .expectBody().json("{\"message\":\"Hello user!\"}");
  57. }
  58. @Test
  59. public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
  60. this.rest
  61. .get()
  62. .uri("/")
  63. .attributes(invalidCredentials())
  64. .exchange()
  65. .expectStatus().isUnauthorized()
  66. .expectBody().isEmpty();
  67. }
  68. private Consumer<Map<String, Object>> userCredentials() {
  69. return basicAuthenticationCredentials("user", "user");
  70. }
  71. private Consumer<Map<String, Object>> invalidCredentials() {
  72. return basicAuthenticationCredentials("user", "INVALID");
  73. }
  74. }