HelloWebfluxApplicationTests.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 java.util.Map;
  18. import java.util.function.Consumer;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. import org.junit.runner.RunWith;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.context.ApplicationContext;
  24. import org.springframework.security.test.context.support.WithMockUser;
  25. import org.springframework.test.context.ActiveProfiles;
  26. import org.springframework.test.context.ContextConfiguration;
  27. import org.springframework.test.context.junit4.SpringRunner;
  28. import org.springframework.test.web.reactive.server.WebTestClient;
  29. import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser;
  30. import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
  31. import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
  32. import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
  33. /**
  34. * @author Rob Winch
  35. * @since 5.0
  36. */
  37. @RunWith(SpringRunner.class)
  38. @ContextConfiguration(classes = HelloWebfluxApplication.class)
  39. @ActiveProfiles("test")
  40. public class HelloWebfluxApplicationTests {
  41. @Autowired
  42. ApplicationContext context;
  43. WebTestClient rest;
  44. @Before
  45. public void setup() {
  46. this.rest = WebTestClient
  47. .bindToApplicationContext(this.context)
  48. .apply(springSecurity())
  49. .configureClient()
  50. .filter(basicAuthentication())
  51. .build();
  52. }
  53. @Test
  54. public void basicWhenNoCredentialsThenUnauthorized() throws Exception {
  55. this.rest
  56. .get()
  57. .uri("/")
  58. .exchange()
  59. .expectStatus().isUnauthorized();
  60. }
  61. @Test
  62. public void basicWhenValidCredentialsThenOk() throws Exception {
  63. this.rest
  64. .get()
  65. .uri("/")
  66. .attributes(userCredentials())
  67. .exchange()
  68. .expectStatus().isOk()
  69. .expectBody().json("{\"message\":\"Hello user!\"}");
  70. }
  71. @Test
  72. public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
  73. this.rest
  74. .get()
  75. .uri("/")
  76. .attributes(invalidCredentials())
  77. .exchange()
  78. .expectStatus().isUnauthorized()
  79. .expectBody().isEmpty();
  80. }
  81. @Test
  82. public void mockSupportWhenMutateWithMockUserThenOk() throws Exception {
  83. this.rest
  84. .mutateWith(mockUser())
  85. .get()
  86. .uri("/")
  87. .exchange()
  88. .expectStatus().isOk()
  89. .expectBody().json("{\"message\":\"Hello user!\"}");
  90. }
  91. @Test
  92. @WithMockUser
  93. public void mockSupportWhenWithMockUserThenOk() throws Exception {
  94. this.rest
  95. .get()
  96. .uri("/")
  97. .exchange()
  98. .expectStatus().isOk()
  99. .expectBody().json("{\"message\":\"Hello user!\"}");
  100. }
  101. private Consumer<Map<String, Object>> userCredentials() {
  102. return basicAuthenticationCredentials("user","user");
  103. }
  104. private Consumer<Map<String, Object>> invalidCredentials() {
  105. return basicAuthenticationCredentials("user","INVALID");
  106. }
  107. }