Skip to content

Commit

Permalink
Added Unit Tests for when NSURLSession uses a custom delegate (NSURLS…
Browse files Browse the repository at this point in the history
…essionDataDelegate)

All fine here (cc #40)
  • Loading branch information
AliSoftware committed Oct 18, 2013
1 parent 4f59b00 commit bd06a75
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
4 changes: 2 additions & 2 deletions OHHTTPStubs/OHHTTPStubs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "UnitTests/UnitTests-Prefix.pch";
INFOPLIST_FILE = "UnitTests/UnitTests-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = octest;
Expand All @@ -493,7 +493,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "UnitTests/UnitTests-Prefix.pch";
INFOPLIST_FILE = "UnitTests/UnitTests-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = octest;
Expand Down
55 changes: 48 additions & 7 deletions OHHTTPStubs/UnitTests/Test Suites/NSURLSessionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@
// Copyright (c) 2013 AliSoftware. All rights reserved.
//

#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000) \
|| (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090)

#import "AsyncSenTestCase.h"
#import "OHHTTPStubs.h"
#import "OHHTTPStubsResponse+JSON.h"

@interface NSURLSessionTests : AsyncSenTestCase @end
@interface NSURLSessionTests : AsyncSenTestCase <NSURLSessionDataDelegate> @end

@implementation NSURLSessionTests
{
NSMutableData* _receivedData;
}

- (void)setUp
{
[super setUp];
[OHHTTPStubs removeAllStubs];
_receivedData = nil;
}

#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000) \
|| (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090)

- (void)_test_NSURLSession:(NSURLSession*)session
jsonForStub:(id)json
completion:(void(^)(NSError* errorResponse,id jsonResponse))completion
Expand All @@ -41,7 +45,7 @@ - (void)_test_NSURLSession:(NSURLSession*)session

__block __strong id dataResponse = nil;
__block __strong NSError* errorResponse = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://unknownhost:666"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"foo://unknownhost:666"]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
Expand Down Expand Up @@ -148,9 +152,46 @@ - (void)test_NSURLSessionDefaultConfig_Disabled
}
}

- (void)test_NSURLSession_DataTask_DelegateMethods
{
NSData* expectedResponse = [NSStringFromSelector(_cmd) dataUsingEncoding:NSUTF8StringEncoding];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.scheme isEqualToString:@"stub"];
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [[OHHTTPStubsResponse responseWithData:expectedResponse statusCode:200 headers:nil]
responseTime:0.5];
}];

NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession* session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

[[session dataTaskWithURL:[NSURL URLWithString:@"stub://foo"]] resume];

[self waitForAsyncOperationWithTimeout:5];

STAssertEqualObjects(_receivedData, expectedResponse, @"Unexpected response");
}

//---------------------------------------------------------------
#pragma mark - Delegate Methods

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
_receivedData = [NSMutableData new];
completionHandler(NSURLSessionResponseAllow);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
[_receivedData appendData:data];
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
[self notifyAsyncOperationDone];
}

@end

#else
#warning Unit Tests using NSURLSession won't be run because NSURLSession is only available on iOS7/OSX10.9 minimum. \
-------- Launch the tests on the iOS7 simulator or an OSX10.9 target for them to be executed.
#endif

@end

0 comments on commit bd06a75

Please sign in to comment.