Search in sources :

Example 21 with Payload

use of io.rsocket.Payload in project spring-framework by spring-projects.

the class DefaultRSocketRequesterTests method testSendWithAsyncMetadata.

@Test
public void testSendWithAsyncMetadata() {
    MimeType compositeMimeType = MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_COMPOSITE_METADATA.getString());
    Mono<String> asyncMeta1 = Mono.delay(Duration.ofMillis(1)).map(aLong -> "Async Metadata 1");
    Mono<String> asyncMeta2 = Mono.delay(Duration.ofMillis(1)).map(aLong -> "Async Metadata 2");
    TestRSocket rsocket = new TestRSocket();
    RSocketRequester.wrap(rsocket, TEXT_PLAIN, compositeMimeType, this.strategies).route("toA").metadata(asyncMeta1, new MimeType("text", "x.test.metadata1")).metadata(asyncMeta2, new MimeType("text", "x.test.metadata2")).data("data").send().block(Duration.ofSeconds(5));
    Payload payload = rsocket.getSavedPayload();
    DefaultMetadataExtractor extractor = new DefaultMetadataExtractor(this.strategies.decoders());
    extractor.metadataToExtract(new MimeType("text", "x.test.metadata1"), String.class, "asyncMeta1");
    extractor.metadataToExtract(new MimeType("text", "x.test.metadata2"), String.class, "asyncMeta2");
    Map<String, Object> metadataValues = extractor.extract(payload, compositeMimeType);
    assertThat(metadataValues.get("asyncMeta1")).isEqualTo("Async Metadata 1");
    assertThat(metadataValues.get("asyncMeta2")).isEqualTo("Async Metadata 2");
    assertThat(payload.getDataUtf8()).isEqualTo("data");
}
Also used : Payload(io.rsocket.Payload) MimeType(org.springframework.util.MimeType) WellKnownMimeType(io.rsocket.metadata.WellKnownMimeType) Test(org.junit.jupiter.api.Test)

Example 22 with Payload

use of io.rsocket.Payload in project spring-framework by spring-projects.

the class PayloadUtilsTests method createWithNettyAndDefaultBuffers.

@Test
public void createWithNettyAndDefaultBuffers() {
    NettyDataBuffer data = createNettyDataBuffer("sample data");
    DefaultDataBuffer metadata = createDefaultDataBuffer("sample metadata");
    Payload payload = PayloadUtils.createPayload(data, metadata);
    try {
        assertThat(payload).isInstanceOf(ByteBufPayload.class);
        assertThat(payload.data()).isSameAs(data.getNativeBuffer());
        assertThat(payload.getMetadataUtf8()).isEqualTo(metadata.toString(UTF_8));
    } finally {
        payload.release();
    }
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) Payload(io.rsocket.Payload) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) Test(org.junit.jupiter.api.Test)

Example 23 with Payload

use of io.rsocket.Payload in project spring-framework by spring-projects.

the class PayloadUtilsTests method retainAndReleaseWithDefaultFactory.

@Test
public void retainAndReleaseWithDefaultFactory() {
    Payload payload = ByteBufPayload.create("sample data");
    DataBuffer buffer = PayloadUtils.retainDataAndReleasePayload(payload, DefaultDataBufferFactory.sharedInstance);
    assertThat(buffer).isInstanceOf(DefaultDataBuffer.class);
    assertThat(payload.refCnt()).isEqualTo(0);
}
Also used : Payload(io.rsocket.Payload) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) Test(org.junit.jupiter.api.Test)

Example 24 with Payload

use of io.rsocket.Payload in project spring-security by spring-projects.

the class PayloadInterceptorRSocketTests method requestResponseWhenSecurityContextThenDelegateContext.

@Test
public void requestResponseWhenSecurityContextThenDelegateContext() {
    TestingAuthenticationToken authentication = new TestingAuthenticationToken("user", "password");
    given(this.interceptor.intercept(any(), any())).willAnswer(withAuthenticated(authentication));
    given(this.delegate.requestResponse(any())).willReturn(this.payloadResult.mono());
    RSocket assertAuthentication = new RSocketProxy(this.delegate) {

        @Override
        public Mono<Payload> requestResponse(Payload payload) {
            return assertAuthentication(authentication).flatMap((a) -> super.requestResponse(payload));
        }
    };
    PayloadInterceptorRSocket interceptor = new PayloadInterceptorRSocket(assertAuthentication, Arrays.asList(this.interceptor), this.metadataMimeType, this.dataMimeType);
    StepVerifier.create(interceptor.requestResponse(this.payload)).then(() -> this.payloadResult.assertSubscribers()).then(() -> this.payloadResult.emit(this.payload)).expectNext(this.payload).verifyComplete();
    verify(this.interceptor).intercept(this.exchange.capture(), any());
    assertThat(this.exchange.getValue().getPayload()).isEqualTo(this.payload);
    verify(this.delegate).requestResponse(this.payload);
}
Also used : RSocketProxy(io.rsocket.util.RSocketProxy) RSocket(io.rsocket.RSocket) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) Payload(io.rsocket.Payload) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 25 with Payload

use of io.rsocket.Payload in project spring-security by spring-projects.

the class PayloadInterceptorRSocketTests method requestStreamWhenSecurityContextThenDelegateContext.

@Test
public void requestStreamWhenSecurityContextThenDelegateContext() {
    TestingAuthenticationToken authentication = new TestingAuthenticationToken("user", "password");
    given(this.interceptor.intercept(any(), any())).willAnswer(withAuthenticated(authentication));
    given(this.delegate.requestStream(any())).willReturn(this.payloadResult.flux());
    RSocket assertAuthentication = new RSocketProxy(this.delegate) {

        @Override
        public Flux<Payload> requestStream(Payload payload) {
            return assertAuthentication(authentication).flatMapMany((a) -> super.requestStream(payload));
        }
    };
    PayloadInterceptorRSocket interceptor = new PayloadInterceptorRSocket(assertAuthentication, Arrays.asList(this.interceptor), this.metadataMimeType, this.dataMimeType);
    StepVerifier.create(interceptor.requestStream(this.payload)).then(() -> this.payloadResult.assertSubscribers()).then(() -> this.payloadResult.emit(this.payload)).expectNext(this.payload).verifyComplete();
    verify(this.interceptor).intercept(this.exchange.capture(), any());
    assertThat(this.exchange.getValue().getPayload()).isEqualTo(this.payload);
    verify(this.delegate).requestStream(this.payload);
}
Also used : RSocketProxy(io.rsocket.util.RSocketProxy) RSocket(io.rsocket.RSocket) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) Payload(io.rsocket.Payload) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.jupiter.api.Test)

Aggregations

Payload (io.rsocket.Payload)26 Test (org.junit.jupiter.api.Test)23 DefaultPayload (io.rsocket.util.DefaultPayload)16 ByteBufPayload (io.rsocket.util.ByteBufPayload)14 DataBuffer (org.springframework.core.io.buffer.DataBuffer)12 NettyDataBuffer (org.springframework.core.io.buffer.NettyDataBuffer)9 RSocket (io.rsocket.RSocket)7 RSocketProxy (io.rsocket.util.RSocketProxy)6 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)6 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)5 MimeType (org.springframework.util.MimeType)5 WellKnownMimeType (io.rsocket.metadata.WellKnownMimeType)4 Publisher (org.reactivestreams.Publisher)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 RSocketConnector (io.rsocket.core.RSocketConnector)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Flux (reactor.core.publisher.Flux)2 Mono (reactor.core.publisher.Mono)2