@@ -349,48 +349,52 @@ class GithubTests: AppTestCase {
349349 // setup
350350 Current . githubToken = { " secr3t " }
351351 let requestCount = QueueIsolated ( 0 )
352- let client = MockClient { req, resp in
353- requestCount. increment ( )
354- switch req. headers [ . accept] {
355- case [ " application/vnd.github.html+json " ] :
356- resp. status = . ok
357- resp. body = makeBody ( " readme html " )
358- resp. headers. add ( name: . eTag, value: " etag " )
359- case [ ] :
360- resp. status = . ok
361- struct Response : Encodable {
362- var htmlUrl : String
363- }
364- resp. body = makeBody ( try ! JSONEncoder ( ) . encode ( Response ( htmlUrl: " readme url " ) ) )
365- default :
366- XCTFail ( " unexpected accept header " )
352+ await withDependencies {
353+ $0. httpClient. get = { @Sendable _, headers in
354+ requestCount. increment ( )
355+ switch headers [ . accept] {
356+ case [ " application/vnd.github.html+json " ] :
357+ return . ok( body: " readme html " , headers: [ " ETag " : " etag " ] )
358+ case [ ] :
359+ struct Response : Encodable {
360+ var htmlUrl : String
361+ }
362+ return try . ok( jsonEncode: Response ( htmlUrl: " readme url " ) )
363+ default :
364+ XCTFail ( " unexpected accept header " )
365+ }
366+ enum Error : Swift . Error { case unexpectedCodePath }
367+ throw Error . unexpectedCodePath
367368 }
368- }
369+ } operation: {
370+ // MUT
371+ let res = await Github . fetchReadme ( owner: " foo " , repository: " bar " )
369372
370- // MUT
371- let res = await Github . fetchReadme ( client: client, owner: " foo " , repository: " bar " )
372-
373- // validate
374- XCTAssertEqual ( requestCount. value, 2 )
375- XCTAssertEqual (
376- res,
377- . init( etag: " etag " ,
378- html: " readme html " ,
379- htmlUrl: " readme url " ,
380- imagesToCache: [ ] )
381- )
373+ // validate
374+ XCTAssertEqual ( requestCount. value, 2 )
375+ XCTAssertEqual (
376+ res,
377+ . init( etag: " etag " ,
378+ html: " readme html " ,
379+ htmlUrl: " readme url " ,
380+ imagesToCache: [ ] )
381+ )
382+ }
382383 }
383384
384385 func test_fetchReadme_notFound( ) async throws {
385386 // setup
386387 Current . githubToken = { " secr3t " }
387- let client = MockClient { _, resp in resp. status = . notFound }
388388
389- // MUT
390- let res = await Github . fetchReadme ( client: client, owner: " foo " , repository: " bar " )
389+ await withDependencies {
390+ $0. httpClient. get = { @Sendable _, headers in . notFound }
391+ } operation: {
392+ // MUT
393+ let res = await Github . fetchReadme ( owner: " foo " , repository: " bar " )
391394
392- // validate
393- XCTAssertEqual ( res, nil )
395+ // validate
396+ XCTAssertEqual ( res, nil )
397+ }
394398 }
395399
396400 func test_extractImagesRequiringCaching( ) async throws {
@@ -482,11 +486,16 @@ class GithubTests: AppTestCase {
482486
483487
484488private extension HTTPClient . Response {
485- static func ok( body: String ) -> Self {
486- . init( status: . ok, body: . init( string: body) )
489+ static func ok( body: String , headers : HTTPHeaders = . init ( ) ) -> Self {
490+ . init( status: . ok, headers : headers , body: . init( string: body) )
487491 }
488492
489493 static func ok( fixture: String ) throws -> Self {
490494 try . init( status: . ok, body: . init( data: fixtureData ( for: fixture) ) )
491495 }
496+
497+ static func ok< T: Encodable > ( jsonEncode value: T ) throws -> Self {
498+ let data = try JSONEncoder ( ) . encode ( value)
499+ return . init( status: . ok, body: . init( data: data) )
500+ }
492501}
0 commit comments