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 871962f0ab..744d20ebba 100644 --- a/AFNetworking/AFURLSessionManager.m +++ b/AFNetworking/AFURLSessionManager.m @@ -405,7 +405,7 @@ + (void)load { 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 sessionWithConfiguration:configuration]; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wnonnull" NSURLSessionDataTask *localDataTask = [session dataTaskWithURL:nil]; @@ -518,8 +518,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]; @@ -533,17 +531,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]; } }]; @@ -556,6 +557,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]; } @@ -712,11 +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]; } + if (resetSession) { + self.session = nil; + } } #pragma mark - diff --git a/Tests/Tests/AFHTTPSessionManagerTests.m b/Tests/Tests/AFHTTPSessionManagerTests.m index 0c1da0f9f2..e2328ee73a 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:NO]; + self.sessionManager = nil; [super tearDown]; } #pragma mark - init -- (void)testSharedManagerIsNotEqualToInitdManager { - XCTAssertFalse([[AFHTTPSessionManager manager] isEqual:self.manager]); +- (void)testSharedManagerIsNotEqualToInitedManager { + 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..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]; + [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 ff645642bb..764195ad7d 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:NO]; + self.sessionManager = nil; } #pragma mark - diff --git a/Tests/Tests/AFUIActivityIndicatorViewTests.m b/Tests/Tests/AFUIActivityIndicatorViewTests.m index eb01f27581..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]; + [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:NO]; self.sessionManager = nil; } diff --git a/Tests/Tests/AFUIRefreshControlTests.m b/Tests/Tests/AFUIRefreshControlTests.m index 3daec94831..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]; + [self.sessionManager invalidateSessionCancelingTasks:YES resetSession:NO]; self.sessionManager = nil; } diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index ebc8a296b2..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]; + [self.localManager invalidateSessionCancelingTasks:YES resetSession:NO]; self.localManager = nil; - [self.backgroundManager invalidateSessionCancelingTasks:YES]; + [self.backgroundManager invalidateSessionCancelingTasks:YES resetSession:NO]; self.backgroundManager = nil; } @@ -158,19 +158,19 @@ - (void)testSessionTaskDoesReportMetrics { [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"]; +- (void)testSessionIsStillValid { -#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 + NSURLSession *session = self.localManager.session; + [self.localManager invalidateSessionCancelingTasks:YES resetSession:NO]; + + XCTAssertEqual(session, self.localManager.session); +} + +- (void)testSessionRecreatesAgain { + + [self.localManager invalidateSessionCancelingTasks:YES resetSession:YES]; + + XCTAssertNotNil(self.localManager.session); } - (void)testUploadTaskDoesReportProgress {