@@ -199,65 +199,48 @@ class GithubTests: AppTestCase {
199199 }
200200 }
201201
202- func test_fetchMetadata_badUrl( ) async throws {
203- let pkg = Package ( url: " https://foo/bar " )
204- let client = MockClient { _, resp in
205- resp. status = . ok
206- }
207- do {
208- _ = try await Github . fetchMetadata ( client: client, packageUrl: pkg. url)
209- XCTFail ( " expected error to be thrown " )
210- } catch {
211- guard case Github . Error . invalidURL = error else {
212- XCTFail ( " unexpected error: \( error. localizedDescription) " )
213- return
214- }
215- }
216- }
217-
218202 func test_fetchMetadata_badData( ) async throws {
219203 // setup
220204 Current . githubToken = { " secr3t " }
221- let pkg = Package ( url: " https://github.com/foo/bar " )
222- let client = MockClient { _, resp in
223- resp. status = . ok
224- resp. body = makeBody ( " bad data " )
225- }
226205
227- // MUT
228- do {
229- _ = try await Github . fetchMetadata ( client: client, packageUrl: pkg. url)
230- XCTFail ( " expected error to be thrown " )
231- } catch let Github . Error . decodeContentFailed( uri, error) {
232- // validation
233- XCTAssertEqual ( uri, " https://api.github.com/graphql " )
234- guard case DecodingError . dataCorrupted = error else {
235- XCTFail ( " unexpected error: \( error. localizedDescription) " )
236- return
206+ await withDependencies {
207+ $0. httpClient. post = { @Sendable _, _, _ in . ok( body: " bad data " ) }
208+ } operation: {
209+ // MUT
210+ do {
211+ _ = try await Github . fetchMetadata ( owner: " foo " , repository: " bar " )
212+ XCTFail ( " expected error to be thrown " )
213+ } catch let Github . Error . decodeContentFailed( uri, error) {
214+ // validation
215+ XCTAssertEqual ( uri, " https://api.github.com/graphql " )
216+ guard case DecodingError . dataCorrupted = error else {
217+ XCTFail ( " unexpected error: \( error. localizedDescription) " )
218+ return
219+ }
220+ } catch {
221+ XCTFail ( " Unexpected error: \( error) " )
237222 }
238- } catch {
239- XCTFail ( " Unexpected error: \( error) " )
240223 }
241224 }
242225
243226 func test_fetchMetadata_rateLimiting_429( ) async throws {
244227 // Github doesn't actually send a 429 when you hit the rate limit
245228 // setup
246229 Current . githubToken = { " secr3t " }
247- let pkg = Package ( url: " https://github.com/foo/bar " )
248- let client = MockClient { _, resp in
249- resp. status = . tooManyRequests
250- }
251230
252- // MUT
253- do {
254- _ = try await Github . fetchMetadata ( client: client, packageUrl: pkg. url)
255- XCTFail ( " expected error to be thrown " )
256- } catch {
257- // validation
258- guard case Github . Error . requestFailed( . tooManyRequests) = error else {
259- XCTFail ( " unexpected error: \( error. localizedDescription) " )
260- return
231+ await withDependencies {
232+ $0. httpClient. post = { @Sendable _, _, _ in . tooManyRequests }
233+ } operation: {
234+ // MUT
235+ do {
236+ _ = try await Github . fetchMetadata ( owner: " foo " , repository: " bar " )
237+ XCTFail ( " expected error to be thrown " )
238+ } catch {
239+ // validation
240+ guard case Github . Error . requestFailed( . tooManyRequests) = error else {
241+ XCTFail ( " unexpected error: \( error. localizedDescription) " )
242+ return
243+ }
261244 }
262245 }
263246 }
@@ -297,26 +280,27 @@ class GithubTests: AppTestCase {
297280 // Ensure we record it as a rate limit error and raise a Rollbar item
298281 // setup
299282 Current . githubToken = { " secr3t " }
300- let pkg = Package ( url: " https://github.com/foo/bar " )
301- let client = MockClient { _, resp in
302- resp. status = . forbidden
303- resp. headers. add ( name: " X-RateLimit-Remaining " , value: " 0 " )
304- }
305283
306- // MUT
307- do {
308- _ = try await Github . fetchMetadata ( client: client, packageUrl: pkg. url)
309- XCTFail ( " expected error to be thrown " )
310- } catch {
311- // validation
312- logger. logs. withValue { logs in
313- XCTAssertEqual ( logs, [
314- . init( level: . critical, message: " rate limited while fetching resource Response<Metadata> " )
315- ] )
284+ await withDependencies {
285+ $0. httpClient. post = { @Sendable _, _, _ in
286+ . init( status: . forbidden, headers: [ " X-RateLimit-Remaining " : " 0 " ] )
316287 }
317- guard case Github . Error . requestFailed( . tooManyRequests) = error else {
318- XCTFail ( " unexpected error: \( error. localizedDescription) " )
319- return
288+ } operation: {
289+ // MUT
290+ do {
291+ _ = try await Github . fetchMetadata ( owner: " foo " , repository: " bar " )
292+ XCTFail ( " expected error to be thrown " )
293+ } catch {
294+ // validation
295+ logger. logs. withValue { logs in
296+ XCTAssertEqual ( logs, [
297+ . init( level: . critical, message: " rate limited while fetching resource Response<Metadata> " )
298+ ] )
299+ }
300+ guard case Github . Error . requestFailed( . tooManyRequests) = error else {
301+ XCTFail ( " unexpected error: \( error. localizedDescription) " )
302+ return
303+ }
320304 }
321305 }
322306 }
0 commit comments