HelloRSocketApplicationITests.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2002-2019 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. * https://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 org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.boot.rsocket.context.LocalRSocketServerPort;
  19. import org.springframework.boot.test.context.SpringBootTest;
  20. import org.springframework.messaging.rsocket.RSocketRequester;
  21. import org.springframework.security.rsocket.metadata.BasicAuthenticationEncoder;
  22. import org.springframework.security.rsocket.metadata.UsernamePasswordMetadata;
  23. import org.springframework.test.context.TestPropertySource;
  24. import org.springframework.test.context.junit4.SpringRunner;
  25. import org.junit.Test;
  26. import org.junit.runner.RunWith;
  27. import reactor.core.publisher.Mono;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  30. import static org.springframework.security.rsocket.metadata.UsernamePasswordMetadata.BASIC_AUTHENTICATION_MIME_TYPE;
  31. /**
  32. * @author Rob Winch
  33. * @author Eddú Meléndez
  34. * @since 5.0
  35. */
  36. @RunWith(SpringRunner.class)
  37. @TestPropertySource(properties = "spring.rsocket.server.port=0")
  38. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  39. public class HelloRSocketApplicationITests {
  40. @Autowired
  41. RSocketRequester.Builder requester;
  42. @LocalRSocketServerPort
  43. int port;
  44. @Test
  45. public void messageWhenAuthenticatedThenSuccess() {
  46. UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("user", "password");
  47. RSocketRequester requester = this.requester
  48. .rsocketStrategies((builder) -> builder.encoder(new BasicAuthenticationEncoder()))
  49. .setupMetadata(credentials, BASIC_AUTHENTICATION_MIME_TYPE)
  50. .connectTcp("localhost", this.port)
  51. .block();
  52. String message = requester.route("message")
  53. .data(Mono.empty())
  54. .retrieveMono(String.class)
  55. .block();
  56. assertThat(message).isEqualTo("Hello");
  57. }
  58. @Test
  59. public void messageWhenNotAuthenticatedThenError() {
  60. RSocketRequester requester = this.requester
  61. .connectTcp("localhost", this.port)
  62. .block();
  63. assertThatThrownBy(() -> requester.route("message")
  64. .data(Mono.empty())
  65. .retrieveMono(String.class)
  66. .block())
  67. .isNotNull();
  68. }
  69. }