Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3233 from AFNetworking/code-coverage/response_ser…
Browse files Browse the repository at this point in the history
…ialization

Added tests for response serializers to increase test coverage
  • Loading branch information
kcharwood committed Dec 16, 2015
2 parents 73df7b9 + a56a227 commit 6031cb6
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 1 deletion.
10 changes: 9 additions & 1 deletion AFNetworking.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@
299522AF1BBF13C700859F49 /* UIRefreshControl+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = 299522991BBF13C700859F49 /* UIRefreshControl+AFNetworking.m */; };
299522B01BBF13C700859F49 /* UIWebView+AFNetworking.h in Headers */ = {isa = PBXBuildFile; fileRef = 2995229A1BBF13C700859F49 /* UIWebView+AFNetworking.h */; settings = {ATTRIBUTES = (Public, ); }; };
299522B11BBF13C700859F49 /* UIWebView+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = 2995229B1BBF13C700859F49 /* UIWebView+AFNetworking.m */; };
29D3413F1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 29D3413E1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m */; };
29D341401C20D46400A7D266 /* AFCompoundResponseSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 29D3413E1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m */; };
29D341411C20D46400A7D266 /* AFCompoundResponseSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 29D3413E1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m */; };
29D96E7A1BCC3D6000F571A5 /* AFHTTPSessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 299522461BBF125A00859F49 /* AFHTTPSessionManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
29D96E7C1BCC3D6000F571A5 /* AFSecurityPolicy.h in Headers */ = {isa = PBXBuildFile; fileRef = 2995224B1BBF125A00859F49 /* AFSecurityPolicy.h */; settings = {ATTRIBUTES = (Public, ); }; };
29D96E7D1BCC3D6000F571A5 /* AFURLRequestSerialization.h in Headers */ = {isa = PBXBuildFile; fileRef = 2995224D1BBF125A00859F49 /* AFURLRequestSerialization.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -288,6 +291,7 @@
299522991BBF13C700859F49 /* UIRefreshControl+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIRefreshControl+AFNetworking.m"; sourceTree = "<group>"; };
2995229A1BBF13C700859F49 /* UIWebView+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWebView+AFNetworking.h"; sourceTree = "<group>"; };
2995229B1BBF13C700859F49 /* UIWebView+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIWebView+AFNetworking.m"; sourceTree = "<group>"; };
29D3413E1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFCompoundResponseSerializerTests.m; sourceTree = "<group>"; };
5F4323B31BF63741003B8749 /* Equifax_Secure_Certificate_Authority_Root.cer */ = {isa = PBXFileReference; lastKnownFileType = file; path = Equifax_Secure_Certificate_Authority_Root.cer; sourceTree = "<group>"; };
5F4323B41BF63741003B8749 /* GeoTrust_Global_CA-cross.cer */ = {isa = PBXFileReference; lastKnownFileType = file; path = "GeoTrust_Global_CA-cross.cer"; sourceTree = "<group>"; };
5F4323B51BF63741003B8749 /* google.com.cer */ = {isa = PBXFileReference; lastKnownFileType = file; path = google.com.cer; sourceTree = "<group>"; };
Expand Down Expand Up @@ -425,8 +429,9 @@
298D7C821BC2C88F00FD3B3E /* AFHTTPResponseSerializationTests.m */,
298D7C831BC2C88F00FD3B3E /* AFHTTPSessionManagerTests.m */,
298D7C851BC2C88F00FD3B3E /* AFJSONSerializationTests.m */,
298D7C871BC2C88F00FD3B3E /* AFNetworkReachabilityManagerTests.m */,
298D7C881BC2C88F00FD3B3E /* AFPropertyListResponseSerializerTests.m */,
29D3413E1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m */,
298D7C871BC2C88F00FD3B3E /* AFNetworkReachabilityManagerTests.m */,
298D7C891BC2C88F00FD3B3E /* AFSecurityPolicyTests.m */,
298D7C8F1BC2C88F00FD3B3E /* AFURLSessionManagerTests.m */,
);
Expand Down Expand Up @@ -949,6 +954,7 @@
2987B0CE1BC40A7600179A4C /* AFNetworkReachabilityManagerTests.m in Sources */,
2987B0E01BC40B0900179A4C /* AFAutoPurgingImageCacheTests.m in Sources */,
2987B0CA1BC40A7600179A4C /* AFHTTPRequestSerializationTests.m in Sources */,
29D341411C20D46400A7D266 /* AFCompoundResponseSerializerTests.m in Sources */,
2987B0E11BC40B0900179A4C /* AFImageDownloaderTests.m in Sources */,
2987B0CF1BC40A7600179A4C /* AFPropertyListResponseSerializerTests.m in Sources */,
2987B0D21BC40AD800179A4C /* AFTestCase.m in Sources */,
Expand All @@ -960,6 +966,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
29D3413F1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m in Sources */,
2960BAC31C1B2F1A00BA02F0 /* AFUIButtonTests.m in Sources */,
298D7C961BC2C94400FD3B3E /* AFTestCase.m in Sources */,
298D7CB11BC2CA6E00FD3B3E /* AFHTTPRequestSerializationTests.m in Sources */,
Expand All @@ -984,6 +991,7 @@
buildActionMask = 2147483647;
files = (
298D7CD41BC2CAE900FD3B3E /* AFHTTPResponseSerializationTests.m in Sources */,
29D341401C20D46400A7D266 /* AFCompoundResponseSerializerTests.m in Sources */,
298D7CB21BC2CA6E00FD3B3E /* AFHTTPRequestSerializationTests.m in Sources */,
298D7CDE1BC2CAF800FD3B3E /* AFSecurityPolicyTests.m in Sources */,
298D7C971BC2C94500FD3B3E /* AFTestCase.m in Sources */,
Expand Down
89 changes: 89 additions & 0 deletions Tests/Tests/AFCompoundResponseSerializerTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// AFURLSessionManagerTests.m
// Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <XCTest/XCTest.h>
#import "AFTestCase.h"
#import "AFURLResponseSerialization.h"

@interface AFCompoundResponseSerializerTests : AFTestCase

@end

@implementation AFCompoundResponseSerializerTests

- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}

#pragma mark - Compound Serializers

- (void)testCompoundSerializerProperlySerializesResponse {

AFImageResponseSerializer *imageSerializer = [AFImageResponseSerializer serializer];
AFJSONResponseSerializer *jsonSerializer = [AFJSONResponseSerializer serializer];
AFCompoundResponseSerializer *compoundSerializer = [AFCompoundResponseSerializer compoundSerializerWithResponseSerializers:@[imageSerializer, jsonSerializer]];

NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"key":@"value"} options:0 error:nil];
NSURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"http://test.com"]
statusCode:200
HTTPVersion:@"1.1"
headerFields:@{@"Content-Type":@"application/json"}];

NSError *error = nil;
id responseObject = [compoundSerializer responseObjectForResponse:response data:data error:&error];

XCTAssertTrue([responseObject isKindOfClass:[NSDictionary class]]);
XCTAssertNil(error);
}

- (void)testCompoundSerializerCanBeCopied {
AFImageResponseSerializer *imageSerializer = [AFImageResponseSerializer serializer];
AFJSONResponseSerializer *jsonSerializer = [AFJSONResponseSerializer serializer];
AFCompoundResponseSerializer *compoundSerializer = [AFCompoundResponseSerializer compoundSerializerWithResponseSerializers:@[imageSerializer, jsonSerializer]];

AFCompoundResponseSerializer *copiedSerializer = [compoundSerializer copy];
XCTAssertNotEqual(compoundSerializer, copiedSerializer);
XCTAssertTrue(compoundSerializer.responseSerializers.count == copiedSerializer.responseSerializers.count);
XCTAssertTrue([NSStringFromClass([[copiedSerializer.responseSerializers objectAtIndex:0] class]) isEqualToString:NSStringFromClass([AFImageResponseSerializer class])]);
XCTAssertTrue([NSStringFromClass([[copiedSerializer.responseSerializers objectAtIndex:1] class]) isEqualToString:NSStringFromClass([AFJSONResponseSerializer class])]);
}

- (void)testCompoundSerializerCanBeArchivedAndUnarchived {
AFImageResponseSerializer *imageSerializer = [AFImageResponseSerializer serializer];
AFJSONResponseSerializer *jsonSerializer = [AFJSONResponseSerializer serializer];
AFCompoundResponseSerializer *compoundSerializer = [AFCompoundResponseSerializer compoundSerializerWithResponseSerializers:@[imageSerializer, jsonSerializer]];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:compoundSerializer];
XCTAssertNotNil(data);
AFCompoundResponseSerializer *unarchivedSerializer = [NSKeyedUnarchiver unarchiveObjectWithData:data];
XCTAssertNotNil(unarchivedSerializer);
XCTAssertNotEqual(unarchivedSerializer, compoundSerializer);
XCTAssertTrue(compoundSerializer.responseSerializers.count == compoundSerializer.responseSerializers.count);
XCTAssertTrue([NSStringFromClass([[unarchivedSerializer.responseSerializers objectAtIndex:0] class]) isEqualToString:NSStringFromClass([AFImageResponseSerializer class])]);
XCTAssertTrue([NSStringFromClass([[unarchivedSerializer.responseSerializers objectAtIndex:1] class]) isEqualToString:NSStringFromClass([AFJSONResponseSerializer class])]);
}

@end
22 changes: 22 additions & 0 deletions Tests/Tests/AFHTTPResponseSerializationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,26 @@ - (void)testThatAFHTTPResponseSerializationFailsAll4XX5XXStatusCodes {
}];
}

- (void)testResponseIsValidated {
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"http://test.com"]
statusCode:200
HTTPVersion:@"1.1"
headerFields:@{@"Content-Type":@"text/html"}];
NSData *data = [@"text" dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
XCTAssertTrue([self.responseSerializer validateResponse:response data:data error:&error]);
}

- (void)testCanBeCopied {
AFHTTPResponseSerializer *copiedSerializer = [self.responseSerializer copy];
XCTAssertNotNil(copiedSerializer);
XCTAssertNotEqual(copiedSerializer, self.responseSerializer);
XCTAssertTrue(copiedSerializer.acceptableContentTypes.count == self.responseSerializer.acceptableContentTypes.count);
XCTAssertTrue(copiedSerializer.acceptableStatusCodes.count == self.responseSerializer.acceptableStatusCodes.count);
}

- (void)testSupportsSecureCoding {
XCTAssertTrue([AFHTTPResponseSerializer supportsSecureCoding]);
}

@end
17 changes: 17 additions & 0 deletions Tests/Tests/AFJSONSerializationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,21 @@ - (void)testThatJSONResponseSerializerReturnsErrorForInvalidJSON {
XCTAssertNotNil(error, @"Serialization error should not be nil");
}

- (void)testThatJSONRemovesKeysWithNullValues {
self.responseSerializer.removesKeysWithNullValues = YES;
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL statusCode:200 HTTPVersion:@"1.1" headerFields:@{@"Content-Type":@"text/json"}];
NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"key":@"value",@"nullkey":[NSNull null],@"array":@[@{@"subnullkey":[NSNull null]}]}
options:0
error:nil];

NSError *error = nil;
NSDictionary *responseObject = [self.responseSerializer responseObjectForResponse:response
data:data
error:&error];
XCTAssertNil(error);
XCTAssertNotNil(responseObject[@"key"]);
XCTAssertNil(responseObject[@"nullkey"]);
XCTAssertNil(responseObject[@"array"][0][@"subnullkey"]);
}

@end
18 changes: 18 additions & 0 deletions Tests/Tests/AFPropertyListResponseSerializerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,22 @@ - (void)testThatPropertyListResponseSerializerHandles204 {
XCTAssertNil(error, @"Error handling application/x-plist");
}

- (void)testResponseSerializerCanBeCopied {
AFPropertyListResponseSerializer *copiedSerializer = [self.responseSerializer copy];
XCTAssertNotNil(copiedSerializer);
XCTAssertNotEqual(copiedSerializer, self.responseSerializer);
XCTAssertTrue(copiedSerializer.format == self.responseSerializer.format);
XCTAssertTrue(copiedSerializer.readOptions == self.responseSerializer.readOptions);
}

- (void)testResponseSerializerCanBeArchivedAndUnarchived {
NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:self.responseSerializer];
XCTAssertNotNil(archive);
AFPropertyListResponseSerializer *unarchivedSerializer = [NSKeyedUnarchiver unarchiveObjectWithData:archive];
XCTAssertNotNil(unarchivedSerializer);
XCTAssertNotEqual(unarchivedSerializer, self.responseSerializer);
XCTAssertTrue(unarchivedSerializer.format == self.responseSerializer.format);
XCTAssertTrue(unarchivedSerializer.readOptions == self.responseSerializer.readOptions);
}

@end

0 comments on commit 6031cb6

Please sign in to comment.