diff --git a/reactive/rsocket/hello-security/build.gradle b/reactive/rsocket/hello-security/build.gradle index 816aaeb35..016037ff5 100644 --- a/reactive/rsocket/hello-security/build.gradle +++ b/reactive/rsocket/hello-security/build.gradle @@ -1,18 +1,14 @@ plugins { - id 'org.springframework.boot' version '3.0.0' - id 'io.spring.dependency-management' version '1.0.11.RELEASE' + id 'org.springframework.boot' version '2.6.11' + id 'io.spring.dependency-management' version '1.1.0' id "nebula.integtest" version "8.2.0" id 'java' } repositories { mavenCentral() - maven { url "https://repo.spring.io/milestone" } - maven { url "https://repo.spring.io/snapshot" } } -ext["micrometer.version"] = "1.10.0-SNAPSHOT" - dependencies { implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-rsocket' diff --git a/reactive/rsocket/hello-security/gradle.properties b/reactive/rsocket/hello-security/gradle.properties index ce1417e1f..e69de29bb 100644 --- a/reactive/rsocket/hello-security/gradle.properties +++ b/reactive/rsocket/hello-security/gradle.properties @@ -1,2 +0,0 @@ -version=6.0.0-SNAPSHOT -spring-security.version=6.0.0-SNAPSHOT diff --git a/reactive/rsocket/hello-security/src/integTest/java/example/HelloRSocketApplicationITests.java b/reactive/rsocket/hello-security/src/integTest/java/example/HelloRSocketApplicationITests.java index 164795906..28943df59 100644 --- a/reactive/rsocket/hello-security/src/integTest/java/example/HelloRSocketApplicationITests.java +++ b/reactive/rsocket/hello-security/src/integTest/java/example/HelloRSocketApplicationITests.java @@ -15,21 +15,21 @@ */ package example; +import io.rsocket.exceptions.RejectedSetupException; import io.rsocket.metadata.WellKnownMimeType; import org.junit.jupiter.api.Test; -import reactor.core.publisher.Mono; - import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.rsocket.server.LocalRSocketServerPort; import org.springframework.messaging.rsocket.RSocketRequester; import org.springframework.security.rsocket.metadata.SimpleAuthenticationEncoder; import org.springframework.security.rsocket.metadata.UsernamePasswordMetadata; import org.springframework.test.context.TestPropertySource; import org.springframework.util.MimeTypeUtils; +import reactor.core.publisher.Mono; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Integration tests for the rsocket application. @@ -40,36 +40,63 @@ */ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @TestPropertySource(properties = "spring.rsocket.server.port=0") -public class HelloRSocketApplicationITests { +class HelloRSocketApplicationITests { @Autowired RSocketRequester.Builder requester; - @LocalRSocketServerPort + // @LocalRSocketServerPort + @Value("${local.rsocket.server.port}") int port; @Test void messageWhenAuthenticatedThenSuccess() { - UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("user", "password"); - // @formatter:off + UsernamePasswordMetadata credentials = + new UsernamePasswordMetadata("user", "password"); + RSocketRequester requester = this.requester - .rsocketStrategies((builder) -> builder.encoder(new SimpleAuthenticationEncoder())) - .setupMetadata(credentials, MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString())) - .connectTcp("localhost", this.port) - .block(); - // @formatter:on + .rsocketStrategies(builder -> + builder.encoder(new SimpleAuthenticationEncoder()) + ) + .setupMetadata( + credentials, + MimeTypeUtils.parseMimeType( + WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString() + ) + ) + .tcp("localhost", this.port); - String message = requester.route("message").data(Mono.empty()).retrieveMono(String.class).block(); + String message = requester + .route("message") + .data(Mono.empty()) + .retrieveMono(String.class) + .block(); assertThat(message).isEqualTo("Hello"); } @Test void messageWhenNotAuthenticatedThenError() { - RSocketRequester requester = this.requester.connectTcp("localhost", this.port).block(); + UsernamePasswordMetadata invalidCredentials = + new UsernamePasswordMetadata("invalid user", "bad password"); - assertThatThrownBy(() -> requester.route("message").data(Mono.empty()).retrieveMono(String.class).block()) - .isNotNull(); - } + RSocketRequester requester = this.requester + .rsocketStrategies(builder -> + builder.encoder(new SimpleAuthenticationEncoder()) + ) + .setupMetadata( + invalidCredentials, + MimeTypeUtils.parseMimeType( + WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString() + ) + ) + .tcp("localhost", this.port); + assertThatExceptionOfType(RejectedSetupException.class).isThrownBy(() -> + requester.route("message") + .data(Mono.empty()) + .retrieveMono(String.class) + .block() + ).withMessageContaining("Invalid Credentials"); + } }