Ben Copsey

Added requestStarted delegate methods to ASIHTTPRequest and ASINetworkQueue

Added delegate method tests
Thanks to Lu for the code!

Also, fixed progress tests on Mac OS, broken as a result of the recent fix relating to setMaxValue: for iPhone
... ... @@ -234,6 +234,9 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
// This lock prevents the operation from being cancelled at an inopportune moment
NSRecursiveLock *cancelledLock;
// Called on the delegate when the request starts
SEL didStartSelector;
// Called on the delegate when the request completes successfully
SEL didFinishSelector;
... ... @@ -394,6 +397,9 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
#pragma mark handling request complete / failure
// Called when a request starts, lets the delegate now via didStartSelector
- (void)requestStarted;
// Called when a request completes successfully, lets the delegate now via didFinishSelector
- (void)requestFinished;
... ... @@ -588,6 +594,7 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
@property (assign) BOOL useSessionPersistance;
@property (retain) NSString *downloadDestinationPath;
@property (retain) NSString *temporaryFileDownloadPath;
@property (assign) SEL didStartSelector;
@property (assign) SEL didFinishSelector;
@property (assign) SEL didFailSelector;
@property (retain,readonly) NSString *authenticationRealm;
... ...
... ... @@ -185,6 +185,7 @@ static BOOL isiPhoneOS2;
[self setUseCookiePersistance:YES];
[self setValidatesSecureCertificate:YES];
[self setRequestCookies:[[[NSMutableArray alloc] init] autorelease]];
[self setDidStartSelector:@selector(requestStarted:)];
[self setDidFinishSelector:@selector(requestFinished:)];
[self setDidFailSelector:@selector(requestFailed:)];
[self setURL:newURL];
... ... @@ -643,6 +644,8 @@ static BOOL isiPhoneOS2;
return;
}
[self requestStarted];
[self setAuthenticationLock:[[[NSConditionLock alloc] initWithCondition:1] autorelease]];
[self setComplete:NO];
... ... @@ -1252,6 +1255,23 @@ static BOOL isiPhoneOS2;
#pragma mark handling request complete / failure
- (void)requestStarted
{
if ([self error] || [self mainRequest]) {
return;
}
// Let the queue know we have started
if ([[self queue] respondsToSelector:@selector(requestDidStart:)]) {
[[self queue] performSelectorOnMainThread:@selector(requestDidStart:) withObject:self waitUntilDone:[NSThread isMainThread]];
}
// Let the delegate know we have started
if ([self didStartSelector] && [[self delegate] respondsToSelector:[self didStartSelector]]) {
[[self delegate] performSelectorOnMainThread:[self didStartSelector] withObject:self waitUntilDone:[NSThread isMainThread]];
}
}
// Subclasses might override this method to process the result in the same thread
// If you do this, don't forget to call [super requestFinished] to let the queue / delegate know we're done
- (void)requestFinished
... ... @@ -3108,6 +3128,7 @@ static BOOL isiPhoneOS2;
@synthesize useCookiePersistance;
@synthesize downloadDestinationPath;
@synthesize temporaryFileDownloadPath;
@synthesize didStartSelector;
@synthesize didFinishSelector;
@synthesize didFailSelector;
@synthesize authenticationRealm;
... ...
... ... @@ -13,6 +13,9 @@
// Delegate will get didFail + didFinish messages (if set)
id delegate;
// Will be called when a request starts with the request as the argument
SEL requestDidStartSelector;
// Will be called when a request completes with the request as the argument
SEL requestDidFinishSelector;
... ... @@ -99,6 +102,7 @@
@property (assign,setter=setUploadProgressDelegate:) id uploadProgressDelegate;
@property (assign,setter=setDownloadProgressDelegate:) id downloadProgressDelegate;
@property (assign) SEL requestDidStartSelector;
@property (assign) SEL requestDidFinishSelector;
@property (assign) SEL requestDidFailSelector;
@property (assign) SEL queueDidFinishSelector;
... ...
... ... @@ -177,6 +177,13 @@
}
- (void)requestDidStart:(ASIHTTPRequest *)request
{
if ([self requestDidStartSelector]) {
[[self delegate] performSelector:[self requestDidStartSelector] withObject:request];
}
}
- (void)requestDidFail:(ASIHTTPRequest *)request
{
[self setRequestsCount:[self requestsCount]-1];
... ... @@ -331,6 +338,7 @@
@synthesize shouldCancelAllRequestsOnFailure;
@synthesize uploadProgressDelegate;
@synthesize downloadProgressDelegate;
@synthesize requestDidStartSelector;
@synthesize requestDidFinishSelector;
@synthesize requestDidFailSelector;
@synthesize queueDidFinishSelector;
... ...
... ... @@ -12,9 +12,13 @@
@interface ASIHTTPRequestTests : ASITestCase {
float progress;
BOOL started;
BOOL finished;
BOOL failed;
}
- (void)testBasicDownload;
- (void)testDelegateMethods;
- (void)testConditionalGET;
- (void)testException;
- (void)testTimeOut;
... ...
... ... @@ -65,6 +65,85 @@
GHAssertTrue(success,@"Failed to generate an error for a bad host");
}
- (void)testDelegateMethods
{
started = NO;
finished = NO;
failed = NO;
// Test default delegate methods
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]];
[request setDelegate:self];
[request start];
GHAssertTrue(started,@"Failed to call the delegate method when the request started");
GHAssertTrue(finished,@"Failed to call the delegate method when the request finished");
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]];
[request setDelegate:self];
[request setTimeOutSeconds:0.01];
[request start];
GHAssertTrue(failed,@"Failed to call the delegate method when the request failed");
started = NO;
finished = NO;
failed = NO;
// Test custom delegate methods
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]];
[request setDelegate:self];
[request setDidStartSelector:@selector(delegateTestStarted:)];
[request setDidFinishSelector:@selector(delegateTestFinished:)];
[request start];
// Hacky, but this test won't run on the main thread, we have to hope the delegate methods will be called in this time
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
GHAssertTrue(started,@"Failed to call the delegate method when the request started");
GHAssertTrue(finished,@"Failed to call the delegate method when the request finished");
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]];
[request setDidFailSelector:@selector(delegateTestFailed:)];
[request setDelegate:self];
[request setTimeOutSeconds:0.01];
[request start];
// Hacky, but this test won't run on the main thread, we have to hope the delegate methods will be called in this time
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
GHAssertTrue(failed,@"Failed to call the delegate method when the request failed");
}
- (void)requestStarted:(ASIHTTPRequest *)request
{
started = YES;
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
finished = YES;
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
failed = YES;
}
- (void)delegateTestStarted:(ASIHTTPRequest *)request
{
started = YES;
}
- (void)delegateTestFinished:(ASIHTTPRequest *)request
{
finished = YES;
}
- (void)delegateTestFailed:(ASIHTTPRequest *)request
{
failed = YES;
}
- (void)testConditionalGET
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/i/logo.png"]];
... ...
... ... @@ -41,6 +41,10 @@ IMPORTANT
ASINetworkQueue *addMoreRequestsQueue;
int requestsFinishedCount;
BOOL started;
BOOL finished;
BOOL failed;
}
- (void)testFailure;
... ...
... ... @@ -53,6 +53,56 @@ IMPORTANT
}
- (void)testDelegateMethods
{
started = NO;
finished = NO;
failed = NO;
ASINetworkQueue *networkQueue = [ASINetworkQueue queue];
[networkQueue setDelegate:self];
[networkQueue setRequestDidStartSelector:@selector(delegateTestStarted:)];
[networkQueue setRequestDidFinishSelector:@selector(delegateTestFinished:)];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]];
[networkQueue addOperation:request];
[networkQueue go];
[networkQueue waitUntilAllOperationsAreFinished];
GHAssertTrue(started,@"Failed to call the delegate method when the request started");
GHAssertTrue(finished,@"Failed to call the delegate method when the request finished");
networkQueue = [ASINetworkQueue queue];
[networkQueue setDelegate:self];
[networkQueue setRequestDidFailSelector:@selector(delegateTestFailed:)];
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]];
[request setTimeOutSeconds:0.01];
[networkQueue addOperation:request];
[networkQueue go];
[networkQueue waitUntilAllOperationsAreFinished];
GHAssertTrue(failed,@"Failed to call the delegate method when the request failed");
}
- (void)delegateTestStarted:(ASIHTTPRequest *)request
{
started = YES;
}
- (void)delegateTestFinished:(ASIHTTPRequest *)request
{
finished = YES;
}
- (void)delegateTestFailed:(ASIHTTPRequest *)request
{
failed = YES;
}
- (void)testDownloadProgress
{
... ...