@@ -361,48 +361,33 @@ class FunctionsTests: XCTestCase {
361361
362362 func testGenerateStreamContent( ) async throws {
363363 let options = HTTPSCallableOptions ( requireLimitedUseAppCheckTokens: true )
364- var response = [ String] ( )
365-
364+ var result = [ String] ( )
365+
366366 let input : [ String : Any ] = [ " data " : " Why is the sky blue " ]
367- let stream = try await functions? . stream (
368- at: URL ( string: " http://127.0.0.1:5001/demo-project/us-central1/genStream " ) !,
369- withObject: input,
370- options: options,
371- timeout: 4.0
372- )
373- // First chunk of the stream comes as NSDictionary
374- if let stream = stream {
375- for try await result in stream {
376- if let dataChunk = result. data as? NSDictionary {
377- for (key, value) in dataChunk {
378- response. append ( " \( key) \( value) " )
379- }
380- } else {
381- // Last chunk is the concatenated result so we have to parse it as String else will
382- // fail.
383- if let dataString = result. data as? String {
384- response. append ( dataString)
385- }
386- }
387- }
388- XCTAssertEqual (
389- response,
390- [
391- " chunk hello " ,
392- " chunk world " ,
393- " chunk this " ,
394- " chunk is " ,
395- " chunk cool " ,
396- " hello world this is cool " ,
397- ]
398- )
399- }
400- XCTExpectFailure ( " Failed to download stream " )
367+ let stream = try await functions!. stream (
368+ at: URL ( string: " http://127.0.0.1:5001/demo-project/us-central1/genStream " ) !,
369+ withObject: input,
370+ options: options,
371+ timeout: 4.0
372+ )
373+ result = try await response ( from: stream)
374+ XCTAssertEqual (
375+ result,
376+ [
377+ " chunk hello " ,
378+ " chunk world " ,
379+ " chunk this " ,
380+ " chunk is " ,
381+ " chunk cool " ,
382+ " hello world this is cool " ,
383+ ]
384+ )
401385 }
402386
403387 func testGenerateStreamContentCanceled( ) async {
404388 let options = HTTPSCallableOptions ( requireLimitedUseAppCheckTokens: true )
405389 let input : [ String : Any ] = [ " data " : " Why is the sky blue " ]
390+ var result = [ String] ( )
406391
407392 let task = Task . detached { [ self ] in
408393 let stream = try await functions!. stream (
@@ -411,30 +396,37 @@ class FunctionsTests: XCTestCase {
411396 options: options,
412397 timeout: 4.0
413398 )
414- // First chunk of the stream comes as NSDictionary
415- var response = [ String] ( )
416- for try await result in stream {
417- if let dataChunk = result. data as? NSDictionary {
418- for (key, value) in dataChunk {
419- response. append ( " \( key) \( value) " )
420- }
421- } else {
422- // Last chunk is the concatenated result so we have to parse it as String else will
423- // fail.
424- if let dataString = result. data as? String {
425- response. append ( dataString)
426- }
427- }
428- }
399+
400+ result = try await response ( from: stream)
429401 // Since we cancel the call we are expecting an empty array.
430402 XCTAssertEqual (
431- response ,
403+ result ,
432404 [ ]
433405 )
434406 }
435407 // We cancel the task and we expect a null response even if the stream was initiated.
436408 task. cancel ( )
437- let result = await task. result
438- XCTAssertNotNil ( result)
409+ let respone = await task. result
410+ XCTAssertNotNil ( respone)
411+ }
412+ }
413+
414+ private func response( from stream: AsyncThrowingStream < HTTPSCallableResult ,
415+ any Error > ) async throws -> [ String ] {
416+ var response = [ String] ( )
417+ for try await result in stream {
418+ // First chunk of the stream comes as NSDictionary
419+ if let dataChunk = result. data as? NSDictionary {
420+ for (key, value) in dataChunk {
421+ response. append ( " \( key) \( value) " )
422+ }
423+ } else {
424+ // Last chunk is the concatenated result so we have to parse it as String else will
425+ // fail.
426+ if let dataString = result. data as? String {
427+ response. append ( dataString)
428+ }
429+ }
439430 }
431+ return response
440432}
0 commit comments