diff --git a/Tests/Tests/AFImageDownloaderTests.m b/Tests/Tests/AFImageDownloaderTests.m index 4900169364..e5c8eb8357 100644 --- a/Tests/Tests/AFImageDownloaderTests.m +++ b/Tests/Tests/AFImageDownloaderTests.m @@ -73,10 +73,17 @@ - (void)testThatImageDownloaderReturnsNilWithInvalidURL * but NSMutableURLRequest can have its URL set to nil **/ NSURLRequest *invalidRequest = [mutableURLRequest copy]; - AFImageDownloadReceipt *downloadReceipt = [self.downloader downloadImageForURLRequest:invalidRequest - success:nil - failure:nil]; - + XCTestExpectation *expectation = [self expectationWithDescription:@"Request should fail"]; + AFImageDownloadReceipt *downloadReceipt = [self.downloader + downloadImageForURLRequest:invalidRequest + success:nil + failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) { + XCTAssertNotNil(error); + XCTAssertTrue([error.domain isEqualToString:NSURLErrorDomain]); + XCTAssertTrue(error.code == NSURLErrorBadURL); + [expectation fulfill]; + }]; + [self waitForExpectationsWithCommonTimeoutUsingHandler:nil]; XCTAssertNil(downloadReceipt, @"downloadReceipt should be nil"); } diff --git a/UIKit+AFNetworking/AFImageDownloader.m b/UIKit+AFNetworking/AFImageDownloader.m index 3470ad4089..78477bf6c6 100644 --- a/UIKit+AFNetworking/AFImageDownloader.m +++ b/UIKit+AFNetworking/AFImageDownloader.m @@ -190,6 +190,12 @@ - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *) dispatch_sync(self.synchronizationQueue, ^{ NSString *URLIdentifier = request.URL.absoluteString; if (URLIdentifier == nil) { + if (failure) { + NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadURL userInfo:nil]; + dispatch_async(dispatch_get_main_queue(), ^{ + failure(request, nil, error); + }); + } return; }