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

Commit

Permalink
Add json/plist serializer tests for better code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
tclementdev committed Oct 6, 2016
1 parent 2201d2c commit 290ec35
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
8 changes: 8 additions & 0 deletions AFNetworking.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@
5F4323DD1BF63CCC003B8749 /* GeoTrust_Global_CA_Root.cer in Resources */ = {isa = PBXBuildFile; fileRef = 5F4323DC1BF63CCC003B8749 /* GeoTrust_Global_CA_Root.cer */; };
5F4323DE1BF63CCC003B8749 /* GeoTrust_Global_CA_Root.cer in Resources */ = {isa = PBXBuildFile; fileRef = 5F4323DC1BF63CCC003B8749 /* GeoTrust_Global_CA_Root.cer */; };
5F4323DF1BF63CCC003B8749 /* GeoTrust_Global_CA_Root.cer in Resources */ = {isa = PBXBuildFile; fileRef = 5F4323DC1BF63CCC003B8749 /* GeoTrust_Global_CA_Root.cer */; };
E91164651DA6A7AE00DFFF56 /* AFPropertyListRequestSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E91164641DA6A7AE00DFFF56 /* AFPropertyListRequestSerializerTests.m */; };
E91164661DA6A7AE00DFFF56 /* AFPropertyListRequestSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E91164641DA6A7AE00DFFF56 /* AFPropertyListRequestSerializerTests.m */; };
E91164671DA6A7AE00DFFF56 /* AFPropertyListRequestSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E91164641DA6A7AE00DFFF56 /* AFPropertyListRequestSerializerTests.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -305,6 +308,7 @@
5F4323D41BF63CB0003B8749 /* GoogleComServerTrustChainPath1 */ = {isa = PBXFileReference; lastKnownFileType = folder; path = GoogleComServerTrustChainPath1; sourceTree = "<group>"; };
5F4323D81BF63CBA003B8749 /* GoogleComServerTrustChainPath2 */ = {isa = PBXFileReference; lastKnownFileType = folder; path = GoogleComServerTrustChainPath2; sourceTree = "<group>"; };
5F4323DC1BF63CCC003B8749 /* GeoTrust_Global_CA_Root.cer */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = GeoTrust_Global_CA_Root.cer; sourceTree = "<group>"; };
E91164641DA6A7AE00DFFF56 /* AFPropertyListRequestSerializerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFPropertyListRequestSerializerTests.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -436,6 +440,7 @@
298D7C831BC2C88F00FD3B3E /* AFHTTPSessionManagerTests.m */,
298D7C851BC2C88F00FD3B3E /* AFJSONSerializationTests.m */,
298D7C881BC2C88F00FD3B3E /* AFPropertyListResponseSerializerTests.m */,
E91164641DA6A7AE00DFFF56 /* AFPropertyListRequestSerializerTests.m */,
29D3413E1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m */,
1BF9F95F1C87832B00F1F35A /* AFImageResponseSerializerTests.m */,
298D7C871BC2C88F00FD3B3E /* AFNetworkReachabilityManagerTests.m */,
Expand Down Expand Up @@ -970,6 +975,7 @@
2987B0CF1BC40A7600179A4C /* AFPropertyListResponseSerializerTests.m in Sources */,
2987B0D21BC40AD800179A4C /* AFTestCase.m in Sources */,
2987B0CD1BC40A7600179A4C /* AFJSONSerializationTests.m in Sources */,
E91164671DA6A7AE00DFFF56 /* AFPropertyListRequestSerializerTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -980,6 +986,7 @@
29D3413F1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m in Sources */,
2960BAC31C1B2F1A00BA02F0 /* AFUIButtonTests.m in Sources */,
298D7C961BC2C94400FD3B3E /* AFTestCase.m in Sources */,
E91164651DA6A7AE00DFFF56 /* AFPropertyListRequestSerializerTests.m in Sources */,
298D7CB11BC2CA6E00FD3B3E /* AFHTTPRequestSerializationTests.m in Sources */,
297824AE1BC2DBD80041C395 /* AFUIActivityIndicatorViewTests.m in Sources */,
297824AD1BC2DBA40041C395 /* AFNetworkActivityManagerTests.m in Sources */,
Expand All @@ -1006,6 +1013,7 @@
298D7CD41BC2CAE900FD3B3E /* AFHTTPResponseSerializationTests.m in Sources */,
29D341401C20D46400A7D266 /* AFCompoundResponseSerializerTests.m in Sources */,
298D7CB21BC2CA6E00FD3B3E /* AFHTTPRequestSerializationTests.m in Sources */,
E91164661DA6A7AE00DFFF56 /* AFPropertyListRequestSerializerTests.m in Sources */,
298D7CDE1BC2CAF800FD3B3E /* AFSecurityPolicyTests.m in Sources */,
1BF9F9611C87843200F1F35A /* AFImageResponseSerializerTests.m in Sources */,
298D7C971BC2C94500FD3B3E /* AFTestCase.m in Sources */,
Expand Down
10 changes: 10 additions & 0 deletions Tests/Tests/AFJSONSerializationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ - (void)testThatJSONRequestSerializationHandlesParametersArray {
XCTAssertTrue([@"[{\"key\":\"value\"}]" isEqualToString:body], @"Parameters were not encoded correctly");
}

- (void)testThatJSONRequestSerializationHandlesInvalidParameters {
NSString *string = [[NSString alloc] initWithBytes:"\xd8\x00" length:2 encoding:NSUTF16StringEncoding];

NSDictionary *parameters = @{@"key":string};
NSError *error = nil;
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST" URLString:AFNetworkingTestsBaseURLString parameters:parameters error:&error];

XCTAssertNil(request, @"Expected nil request.");
}

@end

#pragma mark -
Expand Down
55 changes: 55 additions & 0 deletions Tests/Tests/AFPropertyListRequestSerializerTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// AFPropertyListResponseSerializerTests.m
// Copyright (c) 2011–2016 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 "AFTestCase.h"

#import "AFURLRequestSerialization.h"

@interface AFPropertyListRequestSerializerTests : AFTestCase
@property (nonatomic, strong) AFPropertyListRequestSerializer *requestSerializer;
@end

@implementation AFPropertyListRequestSerializerTests

- (void)setUp {
[super setUp];
self.requestSerializer = [AFPropertyListRequestSerializer serializer];
}

#pragma mark -

- (void)testThatPropertyListRequestSerializerAcceptsPlist {
NSDictionary *parameters = @{@"key":@"value"};
NSError *error = nil;
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST" URLString:AFNetworkingTestsBaseURLString parameters:parameters error:&error];

XCTAssertNotNil(request, @"Expected non-nil request.");
}

- (void)testThatPropertyListRequestSerializerHandlesInvalidPlist {
NSDictionary *parameters = @{@42:@"value"};
NSError *error = nil;
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST" URLString:AFNetworkingTestsBaseURLString parameters:parameters error:&error];

XCTAssertNil(request, @"Expected nil request.");
}

@end
21 changes: 21 additions & 0 deletions Tests/Tests/AFPropertyListResponseSerializerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ - (void)setUp {

#pragma mark -

- (void)testThatPropertyListResponseSerializerAcceptsPlistData {
NSData *data = [NSPropertyListSerialization dataWithPropertyList:@{@"foo": @"bar"} format:NSPropertyListXMLFormat_v1_0 options:0 error:NULL];

NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL statusCode:200 HTTPVersion:@"1.1" headerFields:@{@"Content-Type": @"application/x-plist"}];
NSError *error = nil;
id responseObject = [self.responseSerializer responseObjectForResponse:response data:data error:&error];

XCTAssertTrue([responseObject isKindOfClass:[NSDictionary class]], @"Expected valid dictionary.");
}

- (void)testThatPropertyListResponseSerializerHandlesInvalidPlistData {
NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"foo": @"bar"} options:(NSJSONWritingOptions)0 error:nil];

NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL statusCode:200 HTTPVersion:@"1.1" headerFields:@{@"Content-Type": @"application/x-plist"}];
NSError *error = nil;
id responseObject = [self.responseSerializer responseObjectForResponse:response data:data error:&error];

XCTAssertNil(responseObject, @"Expected nil responseObject.");
XCTAssertNotNil(error, @"Expected non-nil error.");
}

- (void)testThatPropertyListResponseSerializerHandles204 {
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL statusCode:204 HTTPVersion:@"1.1" headerFields:@{@"Content-Type": @"application/x-plist"}];
NSError *error;
Expand Down

0 comments on commit 290ec35

Please sign in to comment.