瀏覽代碼

Refactored to dispatch an error if a bad URL is passed in.

Kevin Harwood 9 年之前
父節點
當前提交
ac6ba4580e
共有 2 個文件被更改,包括 17 次插入4 次删除
  1. 11 4
      Tests/Tests/AFImageDownloaderTests.m
  2. 6 0
      UIKit+AFNetworking/AFImageDownloader.m

+ 11 - 4
Tests/Tests/AFImageDownloaderTests.m

@@ -73,10 +73,17 @@
      *  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");
 }
 

+ 6 - 0
UIKit+AFNetworking/AFImageDownloader.m

@@ -190,6 +190,12 @@
     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;
         }