Skip to content

Commit

Permalink
Update tests to use new API
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaspik committed Sep 11, 2018
1 parent 864c8d6 commit 0579f71
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 51 deletions.
93 changes: 47 additions & 46 deletions Tests/Tests/AFHTTPSessionManagerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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];
Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -177,15 +178,15 @@ - (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];
}
return @"";
}];

NSURLSessionTask *nilTask;
nilTask = [self.manager
nilTask = [self.sessionManager
GET:@"test"
parameters:@{@"key":@"value"}
headers:nil
Expand All @@ -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);
Expand All @@ -224,15 +225,15 @@ - (void)testCanBeDecoded {
#pragma mark - NSCopying

- (void)testCanBeCopied {
AFHTTPSessionManager *copyManager = [self.manager copy];
AFHTTPSessionManager *copyManager = [self.sessionManager copy];
XCTAssertNotNil(copyManager);
}

#pragma mark - Progress

- (void)testDownloadProgressIsReportedForGET {
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"];
[self.manager
[self.sessionManager
GET:@"image"
parameters:nil
headers:nil
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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<AFMultipartFormData> _Nonnull formData) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"}
Expand All @@ -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"}
Expand All @@ -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"}
Expand All @@ -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"}
Expand All @@ -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"}
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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<AFMultipartFormData> _Nonnull formData) {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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<AFMultipartFormData> _Nonnull formData) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -798,7 +799,7 @@ - (void)testInvalidServerTrustProducesCorrectErrorForCertificatePinning {
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
[manager invalidateSessionCancelingTasks:YES];
[manager invalidateSessionCancelingTasks:YES resetSession:NO];
}

- (void)testInvalidServerTrustProducesCorrectErrorForPublicKeyPinning {
Expand All @@ -823,7 +824,7 @@ - (void)testInvalidServerTrustProducesCorrectErrorForPublicKeyPinning {
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
[manager invalidateSessionCancelingTasks:YES];
[manager invalidateSessionCancelingTasks:YES resetSession:NO];
}

@end
Loading

0 comments on commit 0579f71

Please sign in to comment.