Skip to content

Commit

Permalink
Merge pull request AFNetworking#3385 from AFNetworking/bug/fix_image_…
Browse files Browse the repository at this point in the history
…downloader_crash_with_bad_url

Fixed crash if bad URL was passed into the image downloader
  • Loading branch information
kcharwood committed Mar 14, 2016
2 parents 5d1dc7c + ac6ba45 commit 32cf30a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Tests/Tests/AFImageDownloaderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ - (void)testThatImageDownloaderCanBeInitializedAndDeinitializedWithActiveDownloa
XCTAssertNil(self.downloader, @"Downloader should be nil");
}

- (void)testThatImageDownloaderReturnsNilWithInvalidURL
{
NSURL *pngURL = [NSURL URLWithString:@"https://httpbin.org/image/png"];
NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:pngURL];
[mutableURLRequest setURL:nil];
/** NSURLRequest nor NSMutableURLRequest can be initialized with a nil URL,
* but NSMutableURLRequest can have its URL set to nil
**/
NSURLRequest *invalidRequest = [mutableURLRequest copy];
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");
}

- (void)testThatImageDownloaderCanDownloadImage {
XCTestExpectation *expectation = [self expectationWithDescription:@"image download should succeed"];

Expand Down
9 changes: 9 additions & 0 deletions UIKit+AFNetworking/AFImageDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)
__block NSURLSessionDataTask *task = nil;
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;
}

// 1) Append the success and failure blocks to a pre-existing request if it already exists
AFImageDownloaderMergedTask *existingMergedTask = self.mergedTasks[URLIdentifier];
Expand Down

0 comments on commit 32cf30a

Please sign in to comment.