HelloWebfluxFnApplicationTests.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser;
  18. import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
  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.autoconfigure.web.reactive.AutoConfigureWebTestClient;
  24. import org.springframework.boot.test.context.SpringBootTest;
  25. import org.springframework.context.ApplicationContext;
  26. import org.springframework.http.HttpHeaders;
  27. import org.springframework.security.test.context.support.WithMockUser;
  28. import org.springframework.test.context.junit4.SpringRunner;
  29. import org.springframework.test.web.reactive.server.WebTestClient;
  30. /**
  31. * @author Rob Winch
  32. * @since 5.0
  33. */
  34. @RunWith(SpringRunner.class)
  35. @SpringBootTest
  36. @AutoConfigureWebTestClient
  37. public class HelloWebfluxFnApplicationTests {
  38. WebTestClient rest;
  39. @Autowired
  40. public void setup(ApplicationContext context) {
  41. this.rest = WebTestClient
  42. .bindToApplicationContext(context)
  43. .apply(springSecurity())
  44. .configureClient()
  45. .build();
  46. }
  47. @Test
  48. public void basicWhenNoCredentialsThenUnauthorized() throws Exception {
  49. this.rest
  50. .get()
  51. .uri("/")
  52. .exchange()
  53. .expectStatus().isUnauthorized();
  54. }
  55. @Test
  56. public void basicWhenValidCredentialsThenOk() throws Exception {
  57. this.rest
  58. .get()
  59. .uri("/")
  60. .headers(userCredentials())
  61. .exchange()
  62. .expectStatus().isOk()
  63. .expectBody().json("{\"message\":\"Hello user!\"}");
  64. }
  65. @Test
  66. public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
  67. this.rest
  68. .get()
  69. .uri("/")
  70. .headers(invalidCredentials())
  71. .exchange()
  72. .expectStatus().isUnauthorized()
  73. .expectBody().isEmpty();
  74. }
  75. @Test
  76. public void mockSupportWhenMutateWithMockUserThenOk() throws Exception {
  77. this.rest
  78. .mutateWith(mockUser())
  79. .get()
  80. .uri("/")
  81. .exchange()
  82. .expectStatus().isOk()
  83. .expectBody().json("{\"message\":\"Hello user!\"}");
  84. }
  85. @Test
  86. @WithMockUser
  87. public void mockSupportWhenWithMockUserThenOk() throws Exception {
  88. this.rest
  89. .get()
  90. .uri("/")
  91. .exchange()
  92. .expectStatus().isOk()
  93. .expectBody().json("{\"message\":\"Hello user!\"}");
  94. }
  95. private Consumer<HttpHeaders> userCredentials() {
  96. return httpHeaders -> httpHeaders.setBasicAuth("user", "user");
  97. }
  98. private Consumer<HttpHeaders> invalidCredentials() {
  99. return httpHeaders -> httpHeaders.setBasicAuth("user", "INVALID");
  100. }
  101. }