@@ -16,17 +16,31 @@ import XCTest
16
16
import Foundation
17
17
18
18
final class Test_ServerSentEventsDecoding : Test_Runtime {
19
- func _test( input: String , output: [ ServerSentEvent ] , file: StaticString = #filePath, line: UInt = #line)
19
+ func _test( input: String , output: [ ServerSentEvent ] , file: StaticString = #filePath, line: UInt = #line, terminate : ( ( ArraySlice < UInt8 > ) -> Bool ) ? = nil , eventCountOffset : Int = 0 )
20
20
async throws
21
21
{
22
22
let sequence = asOneBytePerElementSequence ( ArraySlice ( input. utf8) ) . asDecodedServerSentEvents ( )
23
23
let events = try await [ ServerSentEvent] ( collecting: sequence)
24
- XCTAssertEqual ( events. count, output. count, file: file, line: line)
24
+ let eventCount = events. count + eventCountOffset
25
+ XCTAssertEqual ( eventCount, output. count, file: file, line: line)
25
26
for (index, linePair) in zip ( events, output) . enumerated ( ) {
26
27
let ( actualEvent, expectedEvent) = linePair
27
28
XCTAssertEqual ( actualEvent, expectedEvent, " Event: \( index) " , file: file, line: line)
28
29
}
29
30
}
31
+
32
+ func _test( input: String , output: [ ServerSentEvent ] , file: StaticString = #filePath, line: UInt = #line, terminatingData: ArraySlice < UInt8 > , eventCountOffset: Int )
33
+ async throws {
34
+ try await _test (
35
+ input: input,
36
+ output: output,
37
+ file: file,
38
+ line: line,
39
+ terminate: { incomingData in incomingData == terminatingData } ,
40
+ eventCountOffset: eventCountOffset
41
+ )
42
+ }
43
+
30
44
func test( ) async throws {
31
45
// Simple event.
32
46
try await _test (
@@ -83,22 +97,80 @@ final class Test_ServerSentEventsDecoding: Test_Runtime {
83
97
. init( id: " 123 " , data: " This is a message with an ID. " ) ,
84
98
]
85
99
)
100
+
101
+ try await _test (
102
+ input: #"""
103
+ data: hello
104
+ data: world
105
+
106
+ data: [DONE]
107
+
108
+ data: hello2
109
+ data: world2
110
+
111
+
112
+ """# ,
113
+ output: [
114
+ . init( data: " hello \n world " )
115
+ ] ,
116
+ terminate: { incomingData in
117
+ incomingData == ArraySlice < UInt8 > ( Data ( " [DONE] " . utf8) )
118
+ } ,
119
+ eventCountOffset: - 2
120
+ )
121
+
122
+ try await _test (
123
+ input: #"""
124
+ data: hello
125
+ data: world
126
+
127
+ data: [DONE]
128
+
129
+ data: hello2
130
+ data: world2
131
+
132
+
133
+ """# ,
134
+ output: [
135
+ . init( data: " hello \n world " )
136
+ ] ,
137
+ terminatingData: ArraySlice < UInt8 > ( Data ( " [DONE] " . utf8) ) ,
138
+ eventCountOffset: - 2
139
+ )
86
140
}
87
141
func _testJSONData< JSONType: Decodable & Hashable & Sendable > (
88
142
input: String ,
89
143
output: [ ServerSentEventWithJSONData < JSONType > ] ,
90
144
file: StaticString = #filePath,
91
- line: UInt = #line
145
+ line: UInt = #line,
146
+ terminate: ( @Sendable ( ArraySlice < UInt8 > ) -> Bool ) ? = nil
92
147
) async throws {
93
148
let sequence = asOneBytePerElementSequence ( ArraySlice ( input. utf8) )
94
- . asDecodedServerSentEventsWithJSONData ( of: JSONType . self)
149
+ . asDecodedServerSentEventsWithJSONData ( of: JSONType . self, terminate : terminate )
95
150
let events = try await [ ServerSentEventWithJSONData < JSONType > ] ( collecting: sequence)
96
151
XCTAssertEqual ( events. count, output. count, file: file, line: line)
97
152
for (index, linePair) in zip ( events, output) . enumerated ( ) {
98
153
let ( actualEvent, expectedEvent) = linePair
99
154
XCTAssertEqual ( actualEvent, expectedEvent, " Event: \( index) " , file: file, line: line)
100
155
}
101
156
}
157
+
158
+ func _testJSONData< JSONType: Decodable & Hashable & Sendable > (
159
+ input: String ,
160
+ output: [ ServerSentEventWithJSONData < JSONType > ] ,
161
+ file: StaticString = #filePath,
162
+ line: UInt = #line,
163
+ terminatingDataSequence: ArraySlice < UInt8 > ?
164
+ ) async throws {
165
+ try await _testJSONData (
166
+ input: input,
167
+ output: output,
168
+ file: file,
169
+ terminate: { incomingData in incomingData == ArraySlice < UInt8 > ( Data ( " [DONE] " . utf8) )
170
+ }
171
+ )
172
+ }
173
+
102
174
struct TestEvent : Decodable , Hashable , Sendable { var index : Int }
103
175
func testJSONData( ) async throws {
104
176
// Simple event.
@@ -121,6 +193,62 @@ final class Test_ServerSentEventsDecoding: Test_Runtime {
121
193
. init( event: " event2 " , data: TestEvent ( index: 2 ) , id: " 2 " ) ,
122
194
]
123
195
)
196
+
197
+ try await _testJSONData (
198
+ input: #"""
199
+ event: event1
200
+ id: 1
201
+ data: {"index":1}
202
+
203
+ event: event2
204
+ id: 2
205
+ data: {
206
+ data: "index": 2
207
+ data: }
208
+
209
+ data: [DONE]
210
+
211
+ event: event3
212
+ id: 1
213
+ data: {"index":3}
214
+
215
+
216
+ """# ,
217
+ output: [
218
+ . init( event: " event1 " , data: TestEvent ( index: 1 ) , id: " 1 " ) ,
219
+ . init( event: " event2 " , data: TestEvent ( index: 2 ) , id: " 2 " ) ,
220
+ ] ,
221
+ terminate: { incomingData in
222
+ incomingData == ArraySlice < UInt8 > ( Data ( " [DONE] " . utf8) )
223
+ }
224
+ )
225
+
226
+ try await _testJSONData (
227
+ input: #"""
228
+ event: event1
229
+ id: 1
230
+ data: {"index":1}
231
+
232
+ event: event2
233
+ id: 2
234
+ data: {
235
+ data: "index": 2
236
+ data: }
237
+
238
+ data: [DONE]
239
+
240
+ event: event3
241
+ id: 1
242
+ data: {"index":3}
243
+
244
+
245
+ """# ,
246
+ output: [
247
+ . init( event: " event1 " , data: TestEvent ( index: 1 ) , id: " 1 " ) ,
248
+ . init( event: " event2 " , data: TestEvent ( index: 2 ) , id: " 2 " ) ,
249
+ ] ,
250
+ terminatingDataSequence: ArraySlice < UInt8 > ( Data ( " [DONE] " . utf8) )
251
+ )
124
252
}
125
253
}
126
254
0 commit comments