Skip to content

Commit

Permalink
Merge pull request #73 from 8W9aG/test-async-consumption
Browse files Browse the repository at this point in the history
Add testConsumptionObserverTakesIntoAccountResponseHeaders
  • Loading branch information
rastersize committed Feb 1, 2016
2 parents bbbc792 + 850386f commit 10a6c6d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions SPTDataLoaderTests/NSURLSessionTaskMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,12 @@
@property (nonatomic, assign) NSUInteger numberOfCallsToResume;
@property (nonatomic, assign) NSUInteger numberOfCallsToCancel;
@property (nonatomic, strong, readwrite, nullable) dispatch_block_t resumeCallback;
@property (nonatomic, strong, readwrite, nullable) NSURLResponse *mockResponse;

#pragma mark NSURLSessionTask

@property (atomic, readonly) int64_t countOfBytesSent;
@property (atomic, readonly) int64_t countOfBytesReceived;
@property (atomic, nullable, readonly, copy) NSURLRequest *currentRequest;

@end
13 changes: 13 additions & 0 deletions SPTDataLoaderTests/NSURLSessionTaskMock.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

@implementation NSURLSessionTaskMock

#pragma mark NSURLSessionTaskMock

- (void)resume
{
self.numberOfCallsToResume++;
Expand All @@ -35,4 +37,15 @@ - (void)cancel
self.numberOfCallsToCancel++;
}

- (NSURLResponse *)response
{
return self.mockResponse;
}

#pragma mark NSURLSessionTask

@synthesize countOfBytesSent;
@synthesize countOfBytesReceived;
@synthesize currentRequest;

@end
2 changes: 2 additions & 0 deletions SPTDataLoaderTests/SPTDataLoaderConsumptionObserverMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
@interface SPTDataLoaderConsumptionObserverMock : NSObject <SPTDataLoaderConsumptionObserver>

@property (nonatomic, assign) NSInteger numberOfCallsToEndedRequest;
@property (nonatomic, strong, readwrite, nullable) dispatch_block_t endedRequestCallback;
@property (nonatomic, assign, readwrite) NSInteger lastBytesDownloaded;

@end
4 changes: 4 additions & 0 deletions SPTDataLoaderTests/SPTDataLoaderConsumptionObserverMock.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ - (void)endedRequestWithResponse:(SPTDataLoaderResponse *)response
bytesUploaded:(int)bytesUploaded
{
self.numberOfCallsToEndedRequest++;
self.lastBytesDownloaded = bytesDownloaded;
if (self.endedRequestCallback) {
self.endedRequestCallback();
}
}

@end
23 changes: 23 additions & 0 deletions SPTDataLoaderTests/SPTDataLoaderServiceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#import "SPTDataLoaderConsumptionObserverMock.h"
#import "NSURLSessionDataTaskMock.h"
#import "SPTDataLoaderRequest+Private.h"
#import "NSURLSessionTaskMock.h"

@interface SPTDataLoaderService () <NSURLSessionDataDelegate, SPTDataLoaderRequestResponseHandlerDelegate, SPTCancellationTokenDelegate, NSURLSessionTaskDelegate>

Expand Down Expand Up @@ -410,4 +411,26 @@ - (void)testWillCacheResponseWithNilCompletionHandler
completionHandler:willCacheResponseCompletionBlock];
}

- (void)testConsumptionObserverTakesIntoAccountResponseHeaders
{
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Test consumption observer response headers"];
SPTDataLoaderConsumptionObserverMock *consumptionObserver = [SPTDataLoaderConsumptionObserverMock new];
consumptionObserver.endedRequestCallback = ^ {
[expectation fulfill];
};
[self.service addConsumptionObserver:consumptionObserver
on:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)];
NSDictionary *headerFields = @{ @"Content-Size" : @"1000" };
NSURL *URL = [NSURL URLWithString:@"http://www.spotify.com"];
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:URL
statusCode:400
HTTPVersion:@"1.1"
headerFields:headerFields];
NSURLSessionTaskMock *task = [NSURLSessionTaskMock new];
task.mockResponse = response;
[self.service URLSession:self.session task:task didCompleteWithError:nil];
[self waitForExpectationsWithTimeout:1.0 handler:nil];
XCTAssertEqual(consumptionObserver.lastBytesDownloaded, 19, @"The last bytes downloaded is incorrect");
}

@end

0 comments on commit 10a6c6d

Please sign in to comment.