HelloRSocketApplicationITests.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * 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.RSocketServerInitializedEvent;
  19. import org.springframework.boot.test.context.SpringBootTest;
  20. import org.springframework.boot.test.context.TestConfiguration;
  21. import org.springframework.context.ApplicationListener;
  22. import org.springframework.messaging.rsocket.RSocketRequester;
  23. import org.springframework.security.rsocket.metadata.BasicAuthenticationEncoder;
  24. import org.springframework.security.rsocket.metadata.UsernamePasswordMetadata;
  25. import org.springframework.test.context.TestPropertySource;
  26. import org.springframework.test.context.junit4.SpringRunner;
  27. import org.junit.Test;
  28. import org.junit.runner.RunWith;
  29. import reactor.core.publisher.Mono;
  30. import static org.assertj.core.api.Assertions.assertThat;
  31. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  32. import static org.springframework.security.rsocket.metadata.UsernamePasswordMetadata.BASIC_AUTHENTICATION_MIME_TYPE;
  33. /**
  34. * @author Rob Winch
  35. * @since 5.0
  36. */
  37. @RunWith(SpringRunner.class)
  38. @TestPropertySource(properties = "spring.rsocket.server.port=0")
  39. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  40. public class HelloRSocketApplicationITests {
  41. @Autowired
  42. RSocketRequester.Builder requester;
  43. @Test
  44. public void messageWhenAuthenticatedThenSuccess() {
  45. UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("user", "password");
  46. RSocketRequester requester = this.requester
  47. .rsocketStrategies(builder -> builder.encoder(new BasicAuthenticationEncoder()))
  48. .setupMetadata(credentials, BASIC_AUTHENTICATION_MIME_TYPE)
  49. .connectTcp("localhost", getPort())
  50. .block();
  51. String message = requester.route("message")
  52. .data(Mono.empty())
  53. .retrieveMono(String.class)
  54. .block();
  55. assertThat(message).isEqualTo("Hello");
  56. }
  57. @Test
  58. public void messageWhenNotAuthenticatedThenError() {
  59. RSocketRequester requester = this.requester
  60. .connectTcp("localhost", getPort())
  61. .block();
  62. assertThatThrownBy(() -> requester.route("message")
  63. .data(Mono.empty())
  64. .retrieveMono(String.class)
  65. .block())
  66. .isNotNull();
  67. }
  68. // FIXME: Waiting for @LocalRSocketServerPort
  69. // https://github.com/spring-projects/spring-boot/pull/18287
  70. @Autowired
  71. Config config;
  72. private int getPort() {
  73. return this.config.port;
  74. }
  75. @TestConfiguration
  76. static class Config implements ApplicationListener<RSocketServerInitializedEvent> {
  77. private int port;
  78. @Override
  79. public void onApplicationEvent(RSocketServerInitializedEvent event) {
  80. this.port = event.getServer().address().getPort();
  81. }
  82. }
  83. }