Skip to content

Commit 2900e92

Browse files
committed
Fix custom MIME type serialization incompatibility (#260)
1 parent 2239c5c commit 2900e92

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/frame/io/mimeType.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ internal fun ByteReadPacket.readAuthType(): AuthType = readType(
4747

4848
private fun BytePacketBuilder.writeTextWithLength(text: String) {
4949
val typeBytes = text.encodeToByteArray()
50-
writeByte(typeBytes.size.toByte()) //write length
51-
writeFully(typeBytes) //write mime type
50+
writeByte((typeBytes.size - 1).toByte())
51+
writeFully(typeBytes)
5252
}
5353

5454
private const val KnownTypeFlag: Byte = Byte.MIN_VALUE
@@ -66,7 +66,7 @@ private inline fun <T> ByteReadPacket.readType(
6666
val identifier = byte xor KnownTypeFlag
6767
fromIdentifier(identifier)
6868
} else {
69-
val stringType = readTextExactBytes(byte.toInt())
69+
val stringType = readTextExactBytes(byte.toInt() + 1)
7070
fromText(stringType)
7171
}
7272
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2015-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+
* http://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+
17+
package io.rsocket.kotlin.frame.io
18+
19+
import io.ktor.utils.io.core.*
20+
import io.rsocket.kotlin.core.*
21+
import kotlin.test.*
22+
23+
class CustomMimeTypeTest {
24+
val name = "message/x.foo"
25+
private val fooMimeType = CustomMimeType(name)
26+
27+
@Test
28+
fun customMimeTypeSerialization() {
29+
val packet = BytePacketBuilder().apply {
30+
writeMimeType(fooMimeType)
31+
}.build()
32+
33+
assertEquals((name.length - 1).toByte(), packet.copy().readByte())
34+
assertEquals(fooMimeType, packet.copy().readMimeType())
35+
}
36+
}

0 commit comments

Comments
 (0)