From c976cf3db87f84ce3cb112d78f7a3f79acb2f3c3 Mon Sep 17 00:00:00 2001 From: Jakub Kaspar Date: Sun, 12 Nov 2017 12:20:24 -0800 Subject: [PATCH 1/8] fix crashes --- AFNetworking/AFNetworkReachabilityManager.h | 2 +- AFNetworking/AFURLSessionManager.m | 52 +++++++++++++++------ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/AFNetworking/AFNetworkReachabilityManager.h b/AFNetworking/AFNetworkReachabilityManager.h index 72296d496a..e14e27e4ea 100644 --- a/AFNetworking/AFNetworkReachabilityManager.h +++ b/AFNetworking/AFNetworkReachabilityManager.h @@ -112,7 +112,7 @@ NS_ASSUME_NONNULL_BEGIN * * @return nil as this method is unavailable */ -- (nullable instancetype)init NS_UNAVAILABLE; +- (nonnull instancetype)init NS_UNAVAILABLE; ///-------------------------------------------------- /// @name Starting & Stopping Reachability Monitoring diff --git a/AFNetworking/AFURLSessionManager.m b/AFNetworking/AFURLSessionManager.m index 2475595d77..4d494cfae9 100644 --- a/AFNetworking/AFURLSessionManager.m +++ b/AFNetworking/AFURLSessionManager.m @@ -39,13 +39,16 @@ static dispatch_queue_t url_session_manager_creation_queue() { } static void url_session_manager_create_task_safely(dispatch_block_t block) { - if (NSFoundationVersionNumber < NSFoundationVersionNumber_With_Fixed_5871104061079552_bug) { - // Fix of bug - // Open Radar:http://openradar.appspot.com/radar?id=5871104061079552 (status: Fixed in iOS8) - // Issue about:https://github.com/AFNetworking/AFNetworking/issues/2093 - dispatch_sync(url_session_manager_creation_queue(), block); - } else { - block(); + + if (block != nil) { + if (NSFoundationVersionNumber < NSFoundationVersionNumber_With_Fixed_5871104061079552_bug) { + // Fix of bug + // Open Radar:http://openradar.appspot.com/radar?id=5871104061079552 (status: Fixed in iOS8) + // Issue about:https://github.com/AFNetworking/AFNetworking/issues/2093 + dispatch_sync(url_session_manager_creation_queue(), block); + } else { + block(); + } } } @@ -373,8 +376,7 @@ + (void)load { 7) If the current class implementation of `resume` is not equal to the super class implementation of `resume` AND the current implementation of `resume` is not equal to the original implementation of `af_resume`, THEN swizzle the methods 8) Set the current class to the super class, and repeat steps 3-8 */ - NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; - NSURLSession * session = [NSURLSession sessionWithConfiguration:configuration]; + NSURLSession * session = [NSURLSession sharedSession]; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wnonnull" NSURLSessionDataTask *localDataTask = [session dataTaskWithURL:nil]; @@ -394,7 +396,6 @@ + (void)load { } [localDataTask cancel]; - [session finishTasksAndInvalidate]; } } @@ -484,8 +485,6 @@ - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)config self.operationQueue = [[NSOperationQueue alloc] init]; self.operationQueue.maxConcurrentOperationCount = 1; - self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue]; - self.responseSerializer = [AFJSONResponseSerializer serializer]; self.securityPolicy = [AFSecurityPolicy defaultPolicy]; @@ -499,17 +498,20 @@ - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)config self.lock = [[NSLock alloc] init]; self.lock.name = AFURLSessionManagerLockName; + __weak typeof(self) weakSelf = self; [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) { + + __strong typeof(weakSelf) strongSelf = weakSelf; for (NSURLSessionDataTask *task in dataTasks) { - [self addDelegateForDataTask:task uploadProgress:nil downloadProgress:nil completionHandler:nil]; + [strongSelf addDelegateForDataTask:task uploadProgress:nil downloadProgress:nil completionHandler:nil]; } for (NSURLSessionUploadTask *uploadTask in uploadTasks) { - [self addDelegateForUploadTask:uploadTask progress:nil completionHandler:nil]; + [strongSelf addDelegateForUploadTask:uploadTask progress:nil completionHandler:nil]; } for (NSURLSessionDownloadTask *downloadTask in downloadTasks) { - [self addDelegateForDownloadTask:downloadTask progress:nil destination:nil completionHandler:nil]; + [strongSelf addDelegateForDownloadTask:downloadTask progress:nil destination:nil completionHandler:nil]; } }]; @@ -522,6 +524,19 @@ - (void)dealloc { #pragma mark - +- (NSURLSession *)session { + + @synchronized (self) { + if (!_session) { + _session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue]; + } + } + return _session; +} + +#pragma mark - + + - (NSString *)taskDescriptionForSessionTasks { return [NSString stringWithFormat:@"%p", self]; } @@ -683,6 +698,7 @@ - (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks { } else { [self.session finishTasksAndInvalidate]; } + self.session = nil; } #pragma mark - @@ -1058,6 +1074,12 @@ - (void)URLSession:(NSURLSession *)session if (self.taskDidComplete) { self.taskDidComplete(session, task, error); } + + if (self.mutableTaskDelegatesKeyedByTaskIdentifier.allKeys.count == 0) { + @synchronized (self) { + [self invalidateSessionCancelingTasks:NO]; + } + } } #pragma mark - NSURLSessionDataDelegate From 926d4dbb8b6426553c3be08c40a28252bcca4c69 Mon Sep 17 00:00:00 2001 From: Jakub Kaspar Date: Mon, 23 Jul 2018 12:20:10 +0200 Subject: [PATCH 2/8] Remove test that is nilling session manually as it's not possible anymore to not have valid session (correctly) --- Tests/Tests/AFURLSessionManagerTests.m | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index e4f2a6b199..d387a3ba9b 100644 --- a/Tests/Tests/AFURLSessionManagerTests.m +++ b/Tests/Tests/AFURLSessionManagerTests.m @@ -133,21 +133,6 @@ - (void)testDownloadTaskDoesReportProgress { [self waitForExpectationsWithCommonTimeout]; } -// iOS 7 has a bug that may return nil for a session. To simulate that, nil out the -// session and it will return nil itself. -- (void)testFileUploadTaskReturnsNilWithBug { - [self.localManager setValue:nil forKey:@"session"]; - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wnonnull" - XCTAssertNil([self.localManager uploadTaskWithRequest:[NSURLRequest requestWithURL:self.baseURL] - fromFile:nil - progress:NULL - completionHandler:NULL], - @"Upload task should be nil."); -#pragma GCC diagnostic pop -} - - (void)testUploadTaskDoesReportProgress { NSMutableString *payload = [NSMutableString stringWithString:@"AFNetworking"]; while ([payload lengthOfBytesUsingEncoding:NSUTF8StringEncoding] < 20000) { From 86c8cc226334dd1a669f2665d42f54225702ec0e Mon Sep 17 00:00:00 2001 From: Jakub Kaspar Date: Mon, 13 Aug 2018 22:34:41 +0200 Subject: [PATCH 3/8] remove temporary method that could be doing nothing --- AFNetworking/AFURLSessionManager.m | 6 ------ 1 file changed, 6 deletions(-) diff --git a/AFNetworking/AFURLSessionManager.m b/AFNetworking/AFURLSessionManager.m index 0bb5f7d1cd..4db61537df 100644 --- a/AFNetworking/AFURLSessionManager.m +++ b/AFNetworking/AFURLSessionManager.m @@ -1088,12 +1088,6 @@ - (void)URLSession:(NSURLSession *)session if (self.taskDidComplete) { self.taskDidComplete(session, task, error); } - - if (self.mutableTaskDelegatesKeyedByTaskIdentifier.allKeys.count == 0) { - @synchronized (self) { - [self invalidateSessionCancelingTasks:NO]; - } - } } #pragma mark - NSURLSessionDataDelegate From 3c54e1f8a5f21e9017d499ce6c7e1d1b3908f97a Mon Sep 17 00:00:00 2001 From: Jakub Kaspar Date: Tue, 14 Aug 2018 08:52:28 +0200 Subject: [PATCH 4/8] Add test --- Tests/Tests/AFURLSessionManagerTests.m | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index b01b663a48..7207622cd1 100644 --- a/Tests/Tests/AFURLSessionManagerTests.m +++ b/Tests/Tests/AFURLSessionManagerTests.m @@ -158,6 +158,13 @@ - (void)testSessionTaskDoesReportMetrics { [self waitForExpectationsWithCommonTimeout]; } +- (void)testSessionRecreatesAgain { + + [self.localManager setValue:nil forKey:@"session"]; + + XCTAssertNotNil([self.localManager valueForKey:@"session"]); +} + - (void)testUploadTaskDoesReportProgress { NSMutableString *payload = [NSMutableString stringWithString:@"AFNetworking"]; while ([payload lengthOfBytesUsingEncoding:NSUTF8StringEncoding] < 20000) { From 864c8d6aedf6c5fbcf60f6f480ca886f74c61ff1 Mon Sep 17 00:00:00 2001 From: Jakub Kaspar Date: Tue, 11 Sep 2018 12:15:01 -0700 Subject: [PATCH 5/8] Update PR feedback, add more tests --- AFNetworking/AFURLSessionManager.h | 10 +++++++++- AFNetworking/AFURLSessionManager.m | 12 ++++++++++-- Tests/Tests/AFURLSessionManagerTests.m | 16 ++++++++++++---- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/AFNetworking/AFURLSessionManager.h b/AFNetworking/AFURLSessionManager.h index 3fa55f95db..4b54aad123 100644 --- a/AFNetworking/AFURLSessionManager.h +++ b/AFNetworking/AFURLSessionManager.h @@ -196,7 +196,15 @@ NS_ASSUME_NONNULL_BEGIN @param cancelPendingTasks Whether or not to cancel pending tasks. */ -- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks; +- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks DEPRECATED_ATTRIBUTE; + +/** + Invalidates the managed session, optionally canceling pending tasks and optionally resets given session. + + @param cancelPendingTasks Whether or not to cancel pending tasks. + @param resetSession Whether or not to reset the session of the manager. + */ +- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks resetSession:(BOOL)resetSession; ///------------------------- /// @name Running Data Tasks diff --git a/AFNetworking/AFURLSessionManager.m b/AFNetworking/AFURLSessionManager.m index 1f3ad16db6..744d20ebba 100644 --- a/AFNetworking/AFURLSessionManager.m +++ b/AFNetworking/AFURLSessionManager.m @@ -404,7 +404,8 @@ + (void)load { 7) If the current class implementation of `resume` is not equal to the super class implementation of `resume` AND the current implementation of `resume` is not equal to the original implementation of `af_resume`, THEN swizzle the methods 8) Set the current class to the super class, and repeat steps 3-8 */ - NSURLSession * session = [NSURLSession sharedSession]; + NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; + NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wnonnull" NSURLSessionDataTask *localDataTask = [session dataTaskWithURL:nil]; @@ -424,6 +425,7 @@ + (void)load { } [localDataTask cancel]; + [session finishTasksAndInvalidate]; } } @@ -724,12 +726,18 @@ - (NSArray *)downloadTasks { #pragma mark - - (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks { + [self invalidateSessionCancelingTasks:cancelPendingTasks resetSession:NO]; +} + +- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks resetSession:(BOOL)resetSession { if (cancelPendingTasks) { [self.session invalidateAndCancel]; } else { [self.session finishTasksAndInvalidate]; } - self.session = nil; + if (resetSession) { + self.session = nil; + } } #pragma mark - diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index 7207622cd1..b779347eba 100644 --- a/Tests/Tests/AFURLSessionManagerTests.m +++ b/Tests/Tests/AFURLSessionManagerTests.m @@ -70,10 +70,10 @@ - (void)setUp { - (void)tearDown { [super tearDown]; [self.localManager.session.configuration.URLCache removeAllCachedResponses]; - [self.localManager invalidateSessionCancelingTasks:YES]; + [self.localManager invalidateSessionCancelingTasks:YES resetSession:YES]; self.localManager = nil; - [self.backgroundManager invalidateSessionCancelingTasks:YES]; + [self.backgroundManager invalidateSessionCancelingTasks:YES resetSession:YES]; self.backgroundManager = nil; } @@ -158,11 +158,19 @@ - (void)testSessionTaskDoesReportMetrics { [self waitForExpectationsWithCommonTimeout]; } +- (void)testSessionIsStillValid { + + NSURLSession *session = self.localManager.session; + [self.localManager invalidateSessionCancelingTasks:YES resetSession:NO]; + + XCTAssertEqual(session, self.localManager.session); +} + - (void)testSessionRecreatesAgain { - [self.localManager setValue:nil forKey:@"session"]; + [self.localManager invalidateSessionCancelingTasks:YES resetSession:NO]; - XCTAssertNotNil([self.localManager valueForKey:@"session"]); + XCTAssertNotNil(self.localManager.session); } - (void)testUploadTaskDoesReportProgress { From 0579f71d5a9f51b6e98327e80e6182224f3eaa68 Mon Sep 17 00:00:00 2001 From: Jakub Kaspar Date: Tue, 11 Sep 2018 12:20:21 -0700 Subject: [PATCH 6/8] Update tests to use new API --- Tests/Tests/AFHTTPSessionManagerTests.m | 93 ++++++++++---------- Tests/Tests/AFImageDownloaderTests.m | 2 +- Tests/Tests/AFNetworkActivityManagerTests.m | 3 +- Tests/Tests/AFUIActivityIndicatorViewTests.m | 2 +- Tests/Tests/AFUIRefreshControlTests.m | 2 +- Tests/Tests/AFURLSessionManagerTests.m | 2 +- 6 files changed, 53 insertions(+), 51 deletions(-) diff --git a/Tests/Tests/AFHTTPSessionManagerTests.m b/Tests/Tests/AFHTTPSessionManagerTests.m index 0c1da0f9f2..577c8c11ea 100644 --- a/Tests/Tests/AFHTTPSessionManagerTests.m +++ b/Tests/Tests/AFHTTPSessionManagerTests.m @@ -25,24 +25,25 @@ #import "AFSecurityPolicy.h" @interface AFHTTPSessionManagerTests : AFTestCase -@property (readwrite, nonatomic, strong) AFHTTPSessionManager *manager; +@property (readwrite, nonatomic, strong) AFHTTPSessionManager *sessionManager; @end @implementation AFHTTPSessionManagerTests - (void)setUp { [super setUp]; - self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:self.baseURL]; + self.sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:self.baseURL]; } - (void)tearDown { - [self.manager invalidateSessionCancelingTasks:YES]; + [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:YES]; + self.sessionManager = nil; [super tearDown]; } #pragma mark - init - (void)testSharedManagerIsNotEqualToInitdManager { - XCTAssertFalse([[AFHTTPSessionManager manager] isEqual:self.manager]); + XCTAssertFalse([[AFHTTPSessionManager manager] isEqual:self.sessionManager]); } #pragma mark - misc @@ -54,7 +55,7 @@ - (void)testThatOperationInvokesCompletionHandlerWithResponseObjectOnSuccess { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:self.baseURL]]; - NSURLSessionDataTask *task = [self.manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil + NSURLSessionDataTask *task = [self.sessionManager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { blockResponseObject = responseObject; blockError = error; @@ -76,7 +77,7 @@ - (void)testThatOperationInvokesFailureCompletionBlockWithErrorOnFailure { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:self.baseURL]]; - NSURLSessionDataTask *task = [self.manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil + NSURLSessionDataTask *task = [self.sessionManager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { blockError = error; [expectation fulfill]; @@ -97,13 +98,13 @@ - (void)testThatRedirectBlockIsCalledWhen302IsEncountered { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; NSURLRequest *redirectRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/1" relativeToURL:self.baseURL]]; - NSURLSessionDataTask *redirectTask = [self.manager dataTaskWithRequest:redirectRequest uploadProgress:nil downloadProgress:nil + NSURLSessionDataTask *redirectTask = [self.sessionManager dataTaskWithRequest:redirectRequest uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { blockError = error; [expectation fulfill]; }]; - [self.manager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest *(NSURLSession *session, NSURLSessionTask *task, NSURLResponse *response, NSURLRequest *request) { + [self.sessionManager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest *(NSURLSession *session, NSURLSessionTask *task, NSURLResponse *response, NSURLRequest *request) { if (response) { success = YES; } @@ -126,14 +127,14 @@ - (void)testDownloadFileCompletionSpecifiesURLInCompletionWithManagerDidFinishBl __block NSURL *downloadFilePath = nil; XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; - [self.manager setDownloadTaskDidFinishDownloadingBlock:^NSURL *(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location) { + [self.sessionManager setDownloadTaskDidFinishDownloadingBlock:^NSURL *(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location) { managerDownloadFinishedBlockExecuted = YES; NSURL *dirURL = [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject]; return [dirURL URLByAppendingPathComponent:@"t1.file"]; }]; NSURLSessionDownloadTask *downloadTask; - downloadTask = [self.manager + downloadTask = [self.sessionManager downloadTaskWithRequest:[NSURLRequest requestWithURL:self.baseURL] progress:nil destination:nil @@ -155,7 +156,7 @@ - (void)testDownloadFileCompletionSpecifiesURLInCompletionBlock { __block NSURL *downloadFilePath = nil; XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; - NSURLSessionDownloadTask *downloadTask = [self.manager downloadTaskWithRequest:[NSURLRequest requestWithURL:self.baseURL] + NSURLSessionDownloadTask *downloadTask = [self.sessionManager downloadTaskWithRequest:[NSURLRequest requestWithURL:self.baseURL] progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { destinationBlockExecuted = YES; @@ -177,7 +178,7 @@ - (void)testDownloadFileCompletionSpecifiesURLInCompletionBlock { - (void)testThatSerializationErrorGeneratesErrorAndNullTaskForGET { XCTestExpectation *expectation = [self expectationWithDescription:@"Serialization should fail"]; - [self.manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) { + [self.sessionManager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) { if (error != NULL) { *error = [NSError errorWithDomain:@"Custom" code:-1 userInfo:nil]; } @@ -185,7 +186,7 @@ - (void)testThatSerializationErrorGeneratesErrorAndNullTaskForGET { }]; NSURLSessionTask *nilTask; - nilTask = [self.manager + nilTask = [self.sessionManager GET:@"test" parameters:@{@"key":@"value"} headers:nil @@ -206,12 +207,12 @@ - (void)testSupportsSecureCoding { } - (void)testCanBeEncoded { - NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.manager]; + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.sessionManager]; XCTAssertNotNil(data); } - (void)testCanBeDecoded { - NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.manager]; + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.sessionManager]; AFHTTPSessionManager *newManager = [NSKeyedUnarchiver unarchiveObjectWithData:data]; XCTAssertNotNil(newManager.securityPolicy); XCTAssertNotNil(newManager.requestSerializer); @@ -224,7 +225,7 @@ - (void)testCanBeDecoded { #pragma mark - NSCopying - (void)testCanBeCopied { - AFHTTPSessionManager *copyManager = [self.manager copy]; + AFHTTPSessionManager *copyManager = [self.sessionManager copy]; XCTAssertNotNil(copyManager); } @@ -232,7 +233,7 @@ - (void)testCanBeCopied { - (void)testDownloadProgressIsReportedForGET { __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"]; - [self.manager + [self.sessionManager GET:@"image" parameters:nil headers:nil @@ -254,7 +255,7 @@ - (void)testUploadProgressIsReportedForPOST { __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"]; - [self.manager + [self.sessionManager POST:@"post" parameters:payload headers:nil @@ -276,7 +277,7 @@ - (void)testUploadProgressIsReportedForStreamingPost { __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"]; - [self.manager + [self.sessionManager POST:@"post" parameters:nil headers:nil @@ -299,7 +300,7 @@ - (void)testDownloadProgressIsReportedForDeprecatedGET { __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager GET:@"image" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { @@ -322,7 +323,7 @@ - (void)testUploadProgressIsReportedForDeprecatedPOST { __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager POST:@"post" parameters:payload progress:^(NSProgress * _Nonnull uploadProgress) { @@ -345,7 +346,7 @@ - (void)testUploadProgressIsReportedForStreamingDeprecatedPost { __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager POST:@"post" parameters:nil constructingBodyWithBlock:^(id _Nonnull formData) { @@ -366,7 +367,7 @@ - (void)testUploadProgressIsReportedForStreamingDeprecatedPost { - (void)testThatSuccessBlockIsCalledFor200 { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; - [self.manager + [self.sessionManager GET:@"status/200" parameters:nil headers:nil @@ -380,7 +381,7 @@ - (void)testThatSuccessBlockIsCalledFor200 { - (void)testThatFailureBlockIsCalledFor404 { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; - [self.manager + [self.sessionManager GET:@"status/404" parameters:nil headers:nil @@ -395,7 +396,7 @@ - (void)testThatFailureBlockIsCalledFor404 { - (void)testThatResponseObjectIsEmptyFor204 { __block id urlResponseObject = nil; XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; - [self.manager + [self.sessionManager GET:@"status/204" parameters:nil headers:nil @@ -413,7 +414,7 @@ - (void)testThatResponseObjectIsEmptyFor204 { - (void)testGET { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; - [self.manager + [self.sessionManager GET:@"get" parameters:nil headers:nil @@ -428,7 +429,7 @@ - (void)testGET { - (void)testHEAD { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; - [self.manager + [self.sessionManager HEAD:@"get" parameters:nil headers:nil @@ -442,7 +443,7 @@ - (void)testHEAD { - (void)testPOST { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; - [self.manager + [self.sessionManager POST:@"post" parameters:@{@"key":@"value"} headers:@{@"field":@"value"} @@ -458,7 +459,7 @@ - (void)testPOST { - (void)testPOSTWithConstructingBody { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; - [self.manager + [self.sessionManager POST:@"post" parameters:@{@"key":@"value"} headers:@{@"field":@"value"} @@ -481,7 +482,7 @@ - (void)testPOSTWithConstructingBody { - (void)testPUT { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; - [self.manager + [self.sessionManager PUT:@"put" parameters:@{@"key":@"value"} headers:@{@"field":@"value"} @@ -496,7 +497,7 @@ - (void)testPUT { - (void)testDELETE { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; - [self.manager + [self.sessionManager DELETE:@"delete" parameters:@{@"key":@"value"} headers:@{@"field":@"value"} @@ -511,7 +512,7 @@ - (void)testDELETE { - (void)testPATCH { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; - [self.manager + [self.sessionManager PATCH:@"patch" parameters:@{@"key":@"value"} headers:@{@"field":@"value"} @@ -531,7 +532,7 @@ - (void)testDeprecatedGETWithoutProgress { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager GET:@"get" parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { @@ -547,7 +548,7 @@ - (void)testDeprecatedPOSTWithoutProgress { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager POST:@"post" parameters:@{@"key":@"value"} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { @@ -563,7 +564,7 @@ - (void)testDeprecatedPOSTWithoutProgressWithConstructingBody { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager POST:@"post" parameters:@{@"key":@"value"} constructingBodyWithBlock:^(id _Nonnull formData) { @@ -587,7 +588,7 @@ - (void)testDeprecatedGETWithoutHeaders { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager GET:@"get" parameters:nil progress:nil @@ -604,7 +605,7 @@ - (void)testDeprecatedHEADWithoutHeaders { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager HEAD:@"get" parameters:nil success:^(NSURLSessionDataTask * _Nonnull task) { @@ -620,7 +621,7 @@ - (void)testDeprecatedPOSTWithoutHeaders { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager POST:@"post" parameters:@{@"key":@"value"} progress:nil @@ -637,7 +638,7 @@ - (void)testDeprecatedPOSTWithoutHeadersWithConstructingBody { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager POST:@"post" parameters:@{@"key":@"value"} constructingBodyWithBlock:^(id _Nonnull formData) { @@ -661,7 +662,7 @@ - (void)testDeprecatedPUTWithoutHeaders { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager PUT:@"put" parameters:@{@"key":@"value"} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { @@ -677,7 +678,7 @@ - (void)testDeprecatedDELETEWithoutHeaders { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager DELETE:@"delete" parameters:@{@"key":@"value"} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { @@ -693,7 +694,7 @@ - (void)testDeprecatedPATCHWithoutHeaders { XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [self.manager + [self.sessionManager PATCH:@"patch" parameters:@{@"key":@"value"} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { @@ -709,8 +710,8 @@ - (void)testDeprecatedPATCHWithoutHeaders { - (void)testHiddenBasicAuthentication { __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Request should finish"]; - [self.manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"user" password:@"password"]; - [self.manager + [self.sessionManager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"user" password:@"password"]; + [self.sessionManager GET:@"hidden-basic-auth/user/password" parameters:nil headers:nil @@ -798,7 +799,7 @@ - (void)testInvalidServerTrustProducesCorrectErrorForCertificatePinning { [expectation fulfill]; }]; [self waitForExpectationsWithCommonTimeout]; - [manager invalidateSessionCancelingTasks:YES]; + [manager invalidateSessionCancelingTasks:YES resetSession:NO]; } - (void)testInvalidServerTrustProducesCorrectErrorForPublicKeyPinning { @@ -823,7 +824,7 @@ - (void)testInvalidServerTrustProducesCorrectErrorForPublicKeyPinning { [expectation fulfill]; }]; [self waitForExpectationsWithCommonTimeout]; - [manager invalidateSessionCancelingTasks:YES]; + [manager invalidateSessionCancelingTasks:YES resetSession:NO]; } @end diff --git a/Tests/Tests/AFImageDownloaderTests.m b/Tests/Tests/AFImageDownloaderTests.m index 9a94b86683..7b280d0a20 100644 --- a/Tests/Tests/AFImageDownloaderTests.m +++ b/Tests/Tests/AFImageDownloaderTests.m @@ -45,7 +45,7 @@ - (void)setUp { } - (void)tearDown { - [self.downloader.sessionManager invalidateSessionCancelingTasks:YES]; + [self.downloader.sessionManager invalidateSessionCancelingTasks:YES resetSession:YES]; self.downloader = nil; // Put teardown code here. This method is called after the invocation of each test method in the class. [super tearDown]; diff --git a/Tests/Tests/AFNetworkActivityManagerTests.m b/Tests/Tests/AFNetworkActivityManagerTests.m index ff645642bb..0f0976db3c 100644 --- a/Tests/Tests/AFNetworkActivityManagerTests.m +++ b/Tests/Tests/AFNetworkActivityManagerTests.m @@ -46,7 +46,8 @@ - (void)tearDown { [super tearDown]; self.networkActivityIndicatorManager = nil; - [self.sessionManager invalidateSessionCancelingTasks:YES]; + [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:YES]; + self.sessionManager = nil; } #pragma mark - diff --git a/Tests/Tests/AFUIActivityIndicatorViewTests.m b/Tests/Tests/AFUIActivityIndicatorViewTests.m index eb01f27581..9a2601306d 100644 --- a/Tests/Tests/AFUIActivityIndicatorViewTests.m +++ b/Tests/Tests/AFUIActivityIndicatorViewTests.m @@ -40,7 +40,7 @@ - (void)setUp { - (void)tearDown { [super tearDown]; - [self.sessionManager invalidateSessionCancelingTasks:YES]; + [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:YES]; self.sessionManager = nil; } diff --git a/Tests/Tests/AFUIRefreshControlTests.m b/Tests/Tests/AFUIRefreshControlTests.m index 3daec94831..cf4a3a94d5 100644 --- a/Tests/Tests/AFUIRefreshControlTests.m +++ b/Tests/Tests/AFUIRefreshControlTests.m @@ -40,7 +40,7 @@ - (void)setUp { - (void)tearDown { [super tearDown]; - [self.sessionManager invalidateSessionCancelingTasks:YES]; + [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:YES]; self.sessionManager = nil; } diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index b779347eba..03be292a93 100644 --- a/Tests/Tests/AFURLSessionManagerTests.m +++ b/Tests/Tests/AFURLSessionManagerTests.m @@ -168,7 +168,7 @@ - (void)testSessionIsStillValid { - (void)testSessionRecreatesAgain { - [self.localManager invalidateSessionCancelingTasks:YES resetSession:NO]; + [self.localManager invalidateSessionCancelingTasks:YES resetSession:YES]; XCTAssertNotNil(self.localManager.session); } From af23240a4b5714d9405acbbd70736029e04dd540 Mon Sep 17 00:00:00 2001 From: Jakub Kaspar Date: Tue, 11 Sep 2018 12:58:25 -0700 Subject: [PATCH 7/8] Change default teardown methods to tests --- Tests/Tests/AFHTTPSessionManagerTests.m | 2 +- Tests/Tests/AFImageDownloaderTests.m | 2 +- Tests/Tests/AFNetworkActivityManagerTests.m | 2 +- Tests/Tests/AFUIActivityIndicatorViewTests.m | 2 +- Tests/Tests/AFUIRefreshControlTests.m | 2 +- Tests/Tests/AFURLSessionManagerTests.m | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Tests/Tests/AFHTTPSessionManagerTests.m b/Tests/Tests/AFHTTPSessionManagerTests.m index 577c8c11ea..2bacc60a12 100644 --- a/Tests/Tests/AFHTTPSessionManagerTests.m +++ b/Tests/Tests/AFHTTPSessionManagerTests.m @@ -36,7 +36,7 @@ - (void)setUp { } - (void)tearDown { - [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:YES]; + [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:NO]; self.sessionManager = nil; [super tearDown]; } diff --git a/Tests/Tests/AFImageDownloaderTests.m b/Tests/Tests/AFImageDownloaderTests.m index 7b280d0a20..4f4e45c9b0 100644 --- a/Tests/Tests/AFImageDownloaderTests.m +++ b/Tests/Tests/AFImageDownloaderTests.m @@ -45,7 +45,7 @@ - (void)setUp { } - (void)tearDown { - [self.downloader.sessionManager invalidateSessionCancelingTasks:YES resetSession:YES]; + [self.downloader.sessionManager invalidateSessionCancelingTasks:YES resetSession:NO]; self.downloader = nil; // Put teardown code here. This method is called after the invocation of each test method in the class. [super tearDown]; diff --git a/Tests/Tests/AFNetworkActivityManagerTests.m b/Tests/Tests/AFNetworkActivityManagerTests.m index 0f0976db3c..764195ad7d 100644 --- a/Tests/Tests/AFNetworkActivityManagerTests.m +++ b/Tests/Tests/AFNetworkActivityManagerTests.m @@ -46,7 +46,7 @@ - (void)tearDown { [super tearDown]; self.networkActivityIndicatorManager = nil; - [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:YES]; + [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:NO]; self.sessionManager = nil; } diff --git a/Tests/Tests/AFUIActivityIndicatorViewTests.m b/Tests/Tests/AFUIActivityIndicatorViewTests.m index 9a2601306d..94094c02ba 100644 --- a/Tests/Tests/AFUIActivityIndicatorViewTests.m +++ b/Tests/Tests/AFUIActivityIndicatorViewTests.m @@ -40,7 +40,7 @@ - (void)setUp { - (void)tearDown { [super tearDown]; - [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:YES]; + [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:NO]; self.sessionManager = nil; } diff --git a/Tests/Tests/AFUIRefreshControlTests.m b/Tests/Tests/AFUIRefreshControlTests.m index cf4a3a94d5..c5ab5af709 100644 --- a/Tests/Tests/AFUIRefreshControlTests.m +++ b/Tests/Tests/AFUIRefreshControlTests.m @@ -40,7 +40,7 @@ - (void)setUp { - (void)tearDown { [super tearDown]; - [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:YES]; + [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:NO]; self.sessionManager = nil; } diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index 03be292a93..c3d28ae3e6 100644 --- a/Tests/Tests/AFURLSessionManagerTests.m +++ b/Tests/Tests/AFURLSessionManagerTests.m @@ -70,10 +70,10 @@ - (void)setUp { - (void)tearDown { [super tearDown]; [self.localManager.session.configuration.URLCache removeAllCachedResponses]; - [self.localManager invalidateSessionCancelingTasks:YES resetSession:YES]; + [self.localManager invalidateSessionCancelingTasks:YES resetSession:NO]; self.localManager = nil; - [self.backgroundManager invalidateSessionCancelingTasks:YES resetSession:YES]; + [self.backgroundManager invalidateSessionCancelingTasks:YES resetSession:NO]; self.backgroundManager = nil; } From 9bb8ea7452244cc4f0899fbab8089a7930649a79 Mon Sep 17 00:00:00 2001 From: Jakub Kaspar Date: Thu, 13 Sep 2018 10:56:00 -0700 Subject: [PATCH 8/8] Change test name to correct one --- Tests/Tests/AFHTTPSessionManagerTests.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Tests/AFHTTPSessionManagerTests.m b/Tests/Tests/AFHTTPSessionManagerTests.m index 2bacc60a12..e2328ee73a 100644 --- a/Tests/Tests/AFHTTPSessionManagerTests.m +++ b/Tests/Tests/AFHTTPSessionManagerTests.m @@ -42,7 +42,7 @@ - (void)tearDown { } #pragma mark - init -- (void)testSharedManagerIsNotEqualToInitdManager { +- (void)testSharedManagerIsNotEqualToInitedManager { XCTAssertFalse([[AFHTTPSessionManager manager] isEqual:self.sessionManager]); }