From 52a8a94b66fa97701c1df6dca2af1f7e77818b99 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Sat, 1 Jun 2013 17:07:13 -0700 Subject: [PATCH] Refactoring image request tests --- Tests/AFHTTPRequestOperationTests.m | 3 +- Tests/AFImageRequestOperationTests.m | 78 ++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/Tests/AFHTTPRequestOperationTests.m b/Tests/AFHTTPRequestOperationTests.m index 729873e..b1ffa08 100644 --- a/Tests/AFHTTPRequestOperationTests.m +++ b/Tests/AFHTTPRequestOperationTests.m @@ -112,8 +112,7 @@ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/redirect/1" relativeToURL:self.baseURL]]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; - [operation setCompletionBlockWithSuccess:nil - failure:nil]; + [operation setCompletionBlockWithSuccess:nil failure:nil]; [operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) { if(redirectResponse){ success = YES; diff --git a/Tests/AFImageRequestOperationTests.m b/Tests/AFImageRequestOperationTests.m index 52a3649..a8e03ed 100644 --- a/Tests/AFImageRequestOperationTests.m +++ b/Tests/AFImageRequestOperationTests.m @@ -1,4 +1,4 @@ -// AFHTTPRequestOperationTests.m +// AFImageRequestOperationTests.m // // Copyright (c) 2013 AFNetworking (http://afnetworking.com) // @@ -35,10 +35,58 @@ #pragma mark - -- (void)testThatImageRequestOperationAcceptsCorrectFormatTypes { - NSArray *acceptedFormats = @[@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon", @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap"]; - for (NSString *format in acceptedFormats) { - NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"/response-headers?Content-Type=%@", format] relativeToURL:self.baseURL]]; +- (void)testThatImageRequestOperationAcceptsTIFFContentType { + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=image/tiff" relativeToURL:self.baseURL]]; + AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:request]; + [operation start]; + + expect([operation isFinished]).will.beTruthy(); + expect(operation.error).will.beNil(); +} + +- (void)testThatImageRequestOperationAcceptsJPEGContentType { + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=image/jpeg" relativeToURL:self.baseURL]]; + AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:request]; + [operation start]; + + expect([operation isFinished]).will.beTruthy(); + expect(operation.error).will.beNil(); +} + +- (void)testThatImageRequestOperationAcceptsGIFContentType { + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=image/gif" relativeToURL:self.baseURL]]; + AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:request]; + [operation start]; + + expect([operation isFinished]).will.beTruthy(); + expect(operation.error).will.beNil(); +} + +- (void)testThatImageRequestOperationAcceptsPNGContentType { + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=image/png" relativeToURL:self.baseURL]]; + AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:request]; + [operation start]; + + expect([operation isFinished]).will.beTruthy(); + expect(operation.error).will.beNil(); +} + +- (void)testThatImageRequestOperationAcceptsIconContentTypes { + NSArray *acceptableIconContentTypes = @[@"image/ico", @"image/x-icon"]; + for (NSString *contentType in acceptableIconContentTypes) { + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"/response-headers?Content-Type=%@", contentType] relativeToURL:self.baseURL]]; + AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:request]; + [operation start]; + + expect([operation isFinished]).will.beTruthy(); + expect(operation.error).will.beNil(); + } +} + +- (void)testThatImageRequestOperationAcceptsBitmapContentTypes { + NSArray *acceptableBitmapContentTypes = @[@"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap"]; + for (NSString *contentType in acceptableBitmapContentTypes) { + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"/response-headers?Content-Type=%@", contentType] relativeToURL:self.baseURL]]; AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:request]; [operation start]; @@ -48,7 +96,7 @@ } - (void)testThatImageRequestOperationDoesNotAcceptInvalidFormatTypes { - NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=image/badFormat" relativeToURL:self.baseURL]]; + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=image/invalid" relativeToURL:self.baseURL]]; AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:request]; [operation start]; @@ -75,20 +123,20 @@ } - (void)testImageProcessingBlockIsRunOnSuccess { - __block UIImage *blockImage = nil; - NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=image/png" relativeToURL:self.baseURL]]; + __block BOOL imageProcessingBlockExecuted = NO; + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=image/png" relativeToURL:self.baseURL]]; AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:^UIImage *(UIImage *image) { - blockImage = [[UIImage alloc] init]; - return blockImage; + imageProcessingBlockExecuted = YES; + return image; } success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { - } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { - }]; + return; + } failure:nil]; [operation start]; expect([operation isFinished]).will.beTruthy(); expect(operation.error).will.beNil(); - expect(blockImage).willNot.beNil(); + expect(imageProcessingBlockExecuted).will.beTruthy(); } - (void)testImageProcessingBlockIsNotRunOnFailure { @@ -98,9 +146,7 @@ AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:^UIImage *(UIImage *image) { blockImage = [[UIImage alloc] init]; return blockImage; - } success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { - } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { - }]; + } success:nil failure:nil]; [operation start]; expect([operation isFinished]).will.beTruthy();