Skip to content

Commit 379c419

Browse files
chore(tests): add more tests for streaming (#108)
1 parent 8042591 commit 379c419

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

openai-java-core/src/test/kotlin/com/openai/core/handlers/SseHandlerTest.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.openai.core.http.Headers
44
import com.openai.core.http.HttpResponse
55
import com.openai.core.http.SseMessage
66
import com.openai.core.jsonMapper
7+
import com.openai.errors.OpenAIException
78
import java.io.ByteArrayInputStream
89
import java.io.InputStream
910
import java.nio.charset.StandardCharsets
@@ -21,6 +22,25 @@ class SseHandlerTest {
2122
internal val expectedMessages: List<SseMessage>? = null,
2223
internal val expectedException: Exception? = null
2324
) {
25+
DATA_MISSING_EVENT(
26+
buildString {
27+
append("data: {\"foo\":true}\n")
28+
append("\n")
29+
},
30+
listOf(sseMessageBuilder().data("{\"foo\":true}").build())
31+
),
32+
MULTIPLE_DATA_MISSING_EVENT(
33+
buildString {
34+
append("data: {\"foo\":true}\n")
35+
append("\n")
36+
append("data: {\"bar\":false}\n")
37+
append("\n")
38+
},
39+
listOf(
40+
sseMessageBuilder().data("{\"foo\":true}").build(),
41+
sseMessageBuilder().data("{\"bar\":false}").build()
42+
)
43+
),
2444
DATA_JSON_ESCAPED_DOUBLE_NEW_LINE(
2545
buildString {
2646
append("data: {\n")
@@ -60,6 +80,27 @@ class SseHandlerTest {
6080
append("\n")
6181
},
6282
listOf(sseMessageBuilder().data("{\"content\":\"известни\"}").build())
83+
),
84+
STRING_ERROR_PROPERTY(
85+
buildString {
86+
append("data: {\"error\":\"ERROR!\"}\n")
87+
append("\n")
88+
},
89+
expectedException = OpenAIException("ERROR!")
90+
),
91+
ERROR_PROPERTY_WITH_MESSAGE(
92+
buildString {
93+
append("data: {\"error\":{\"message\":\"ERROR!\"}}\n")
94+
append("\n")
95+
},
96+
expectedException = OpenAIException("ERROR!")
97+
),
98+
ERROR_PROPERTY_MALFORMED(
99+
buildString {
100+
append("data: {\"error\":42}\n")
101+
append("\n")
102+
},
103+
expectedException = OpenAIException("An error occurred during streaming")
63104
)
64105
}
65106

0 commit comments

Comments
 (0)