Skip to content

Commit

Permalink
Fix custom MIME type serialization incompatibility (rsocket#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
akowal committed Apr 26, 2024
1 parent 2239c5c commit eeb2af6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Expand Up @@ -47,8 +47,8 @@ internal fun ByteReadPacket.readAuthType(): AuthType = readType(

private fun BytePacketBuilder.writeTextWithLength(text: String) {
val typeBytes = text.encodeToByteArray()
writeByte(typeBytes.size.toByte()) //write length
writeFully(typeBytes) //write mime type
writeByte((typeBytes.size - 1).toByte())
writeFully(typeBytes)
}

private const val KnownTypeFlag: Byte = Byte.MIN_VALUE
Expand All @@ -66,7 +66,7 @@ private inline fun <T> ByteReadPacket.readType(
val identifier = byte xor KnownTypeFlag
fromIdentifier(identifier)
} else {
val stringType = readTextExactBytes(byte.toInt())
val stringType = readTextExactBytes(byte.toInt() + 1)
fromText(stringType)
}
}
Expand Down
@@ -0,0 +1,36 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.frame.io

import io.ktor.utils.io.core.*
import io.rsocket.kotlin.core.*
import kotlin.test.*

class CustomMimeTypeTest {
val name = "message/x.foo"
private val fooMimeType = CustomMimeType(name)

@Test
fun customMimeTypeSerialization() {
val packet = BytePacketBuilder().apply {
writeMimeType(fooMimeType)
}.build()

assertEquals((name.length - 1).toByte(), packet.copy().readByte())
assertEquals(fooMimeType, packet.copy().readMimeType())
}
}

0 comments on commit eeb2af6

Please sign in to comment.