-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ByteChannelTextTest.kt
59 lines (48 loc) · 1.53 KB
/
ByteChannelTextTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.utils.io.charsets
import io.ktor.utils.io.*
import kotlinx.coroutines.*
import kotlin.test.*
class ByteChannelTextTest {
@Test
fun testReadUtf8LineThrowTooLongLine() = runBlocking<Unit> {
val line100 = (0..99).joinToString("")
val channel = ByteChannel()
channel.writeStringUtf8(line100)
channel.close()
assertFailsWith<TooLongLineException> {
channel.readUTF8Line(50)
}
}
@Test
fun testReadUtf8Line32k() = runBlocking {
val line = "x".repeat(32 * 1024)
val bytes = line.encodeToByteArray()
val channel = ByteReadChannel(bytes)
val result = channel.readUTF8Line()
assertEquals(line, result)
}
@Test
fun testReadLineUtf8Chunks() = runBlocking {
val line = "x".repeat(32 * 1024)
val channel = writer {
channel.writeStringUtf8(line)
}.channel
val result = channel.readUTF8Line()
assertEquals(line, result)
}
@Test
fun test2EmptyLines() {
val text = ByteReadChannel("\r\n\r\n")
runBlocking {
assertEquals(4, text.availableForRead)
assertEquals("", text.readUTF8Line())
assertEquals(2, text.availableForRead)
assertEquals(2, text.totalBytesRead)
assertEquals("", text.readUTF8Line())
assertNull(text.readUTF8Line())
}
}
}