HelloRSocketApplicationITests.java 2.7 KB

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