Skip to content

Commit

Permalink
Refactored to dispatch an error if a bad URL is passed in.
Browse files Browse the repository at this point in the history
  • Loading branch information
kcharwood committed Mar 14, 2016
1 parent fa2c806 commit ac6ba45
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Tests/Tests/AFImageDownloaderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
6 changes: 6 additions & 0 deletions UIKit+AFNetworking/AFImageDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit ac6ba45

Please sign in to comment.