@@ -4,6 +4,7 @@ import com.openai.core.http.Headers
44import com.openai.core.http.HttpResponse
55import com.openai.core.http.SseMessage
66import com.openai.core.jsonMapper
7+ import com.openai.errors.OpenAIException
78import java.io.ByteArrayInputStream
89import java.io.InputStream
910import 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