Support ByteBuf as a metadata value
This allows encoding of metadata values externally via some existing API for encoding RSocket metadata, rather than relying on registered Encoders. Also remove explicit checks for DataBuffer since those are supported through the DataBufferEncoder (registered by default). See gh-23513
This commit is contained in:
@@ -15,13 +15,13 @@
|
||||
*/
|
||||
package org.springframework.messaging.rsocket;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.rsocket.metadata.CompositeMetadata;
|
||||
import io.rsocket.metadata.RoutingMetadata;
|
||||
import io.rsocket.metadata.WellKnownMimeType;
|
||||
@@ -35,6 +35,7 @@ import org.springframework.core.io.buffer.support.DataBufferTestUtils;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@@ -53,27 +54,12 @@ public class MetadataEncoderTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void compositeMetadataWithRoute() {
|
||||
DataBuffer buffer = new MetadataEncoder(COMPOSITE_METADATA, this.strategies)
|
||||
.route("toA")
|
||||
.encode();
|
||||
|
||||
CompositeMetadata entries = new CompositeMetadata(((NettyDataBuffer) buffer).getNativeBuffer(), false);
|
||||
Iterator<CompositeMetadata.Entry> iterator = entries.iterator();
|
||||
|
||||
assertThat(iterator.hasNext()).isTrue();
|
||||
CompositeMetadata.Entry entry = iterator.next();
|
||||
assertThat(entry.getMimeType()).isEqualTo(WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString());
|
||||
assertRoute("toA", entry.getContent());
|
||||
assertThat(iterator.hasNext()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compositeMetadataWithRouteAndText() {
|
||||
public void compositeMetadata() {
|
||||
|
||||
DataBuffer buffer = new MetadataEncoder(COMPOSITE_METADATA, this.strategies)
|
||||
.route("toA")
|
||||
.metadata("My metadata", MimeTypeUtils.TEXT_PLAIN)
|
||||
.metadata(Unpooled.wrappedBuffer("Raw data".getBytes(UTF_8)), MimeTypeUtils.APPLICATION_OCTET_STREAM)
|
||||
.encode();
|
||||
|
||||
CompositeMetadata entries = new CompositeMetadata(((NettyDataBuffer) buffer).getNativeBuffer(), false);
|
||||
@@ -86,19 +72,25 @@ public class MetadataEncoderTests {
|
||||
|
||||
assertThat(iterator.hasNext()).isTrue();
|
||||
entry = iterator.next();
|
||||
assertThat(entry.getMimeType()).isEqualTo(MimeTypeUtils.TEXT_PLAIN.toString());
|
||||
assertThat(entry.getContent().toString(StandardCharsets.UTF_8)).isEqualTo("My metadata");
|
||||
assertThat(entry.getMimeType()).isEqualTo(MimeTypeUtils.TEXT_PLAIN_VALUE);
|
||||
assertThat(entry.getContent().toString(UTF_8)).isEqualTo("My metadata");
|
||||
|
||||
assertThat(iterator.hasNext()).isTrue();
|
||||
entry = iterator.next();
|
||||
assertThat(entry.getMimeType()).isEqualTo(MimeTypeUtils.APPLICATION_OCTET_STREAM_VALUE);
|
||||
assertThat(entry.getContent().toString(UTF_8)).isEqualTo("Raw data");
|
||||
|
||||
assertThat(iterator.hasNext()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void routeWithRoutingMimeType() {
|
||||
MimeType metaMimeType = MimeTypeUtils.parseMimeType(
|
||||
|
||||
MimeType mimeType = MimeTypeUtils.parseMimeType(
|
||||
WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString());
|
||||
|
||||
DataBuffer buffer =
|
||||
new MetadataEncoder(metaMimeType, this.strategies)
|
||||
new MetadataEncoder(mimeType, this.strategies)
|
||||
.route("toA")
|
||||
.encode();
|
||||
|
||||
@@ -127,6 +119,16 @@ public class MetadataEncoderTests {
|
||||
|
||||
@Test
|
||||
public void metadataWithTextPlainMimeType() {
|
||||
DataBuffer buffer =
|
||||
new MetadataEncoder(MimeTypeUtils.TEXT_PLAIN, this.strategies)
|
||||
.metadata(Unpooled.wrappedBuffer("Raw data".getBytes(UTF_8)), null)
|
||||
.encode();
|
||||
|
||||
assertThat(dumpString(buffer)).isEqualTo("Raw data");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void metadataWithByteBuf() {
|
||||
DataBuffer buffer =
|
||||
new MetadataEncoder(MimeTypeUtils.TEXT_PLAIN, this.strategies)
|
||||
.metadata("toA", null)
|
||||
@@ -210,7 +212,7 @@ public class MetadataEncoderTests {
|
||||
}
|
||||
|
||||
private String dumpString(DataBuffer buffer) {
|
||||
return DataBufferTestUtils.dumpString(buffer, StandardCharsets.UTF_8);
|
||||
return DataBufferTestUtils.dumpString(buffer, UTF_8);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user