Skip to content

Commit

Permalink
Added requestStarted delegate methods to ASIHTTPRequest and ASINetwor…
Browse files Browse the repository at this point in the history
…kQueue

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
  • Loading branch information
pokeb committed Nov 25, 2009
1 parent d4e036d commit fc29d80
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Classes/ASIHTTPRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions Classes/ASIHTTPRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ - (id)initWithURL:(NSURL *)newURL
[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];
Expand Down Expand Up @@ -643,6 +644,8 @@ - (void)startRequest
return;
}

[self requestStarted];

[self setAuthenticationLock:[[[NSConditionLock alloc] initWithCondition:1] autorelease]];

[self setComplete:NO];
Expand Down Expand Up @@ -1252,6 +1255,23 @@ + (void)setProgress:(double)progress forProgressIndicator:(id)indicator

#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
Expand Down Expand Up @@ -3108,6 +3128,7 @@ + (NSString*)base64forData:(NSData*)theData {
@synthesize useCookiePersistance;
@synthesize downloadDestinationPath;
@synthesize temporaryFileDownloadPath;
@synthesize didStartSelector;
@synthesize didFinishSelector;
@synthesize didFailSelector;
@synthesize authenticationRealm;
Expand Down
4 changes: 4 additions & 0 deletions Classes/ASINetworkQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions Classes/ASINetworkQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ - (void)addOperation:(NSOperation *)operation

}

- (void)requestDidStart:(ASIHTTPRequest *)request
{
if ([self requestDidStartSelector]) {
[[self delegate] performSelector:[self requestDidStartSelector] withObject:request];
}
}

- (void)requestDidFail:(ASIHTTPRequest *)request
{
[self setRequestsCount:[self requestsCount]-1];
Expand Down Expand Up @@ -331,6 +338,7 @@ - (BOOL)respondsToSelector:(SEL)selector
@synthesize shouldCancelAllRequestsOnFailure;
@synthesize uploadProgressDelegate;
@synthesize downloadProgressDelegate;
@synthesize requestDidStartSelector;
@synthesize requestDidFinishSelector;
@synthesize requestDidFailSelector;
@synthesize queueDidFinishSelector;
Expand Down
4 changes: 4 additions & 0 deletions Classes/Tests/ASIHTTPRequestTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
79 changes: 79 additions & 0 deletions Classes/Tests/ASIHTTPRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,85 @@ - (void)testBasicDownload
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"]];
Expand Down
4 changes: 4 additions & 0 deletions Classes/Tests/ASINetworkQueueTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ IMPORTANT

ASINetworkQueue *addMoreRequestsQueue;
int requestsFinishedCount;

BOOL started;
BOOL finished;
BOOL failed;
}

- (void)testFailure;
Expand Down
50 changes: 50 additions & 0 deletions Classes/Tests/ASINetworkQueueTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,56 @@ - (void)testDelegateAuthenticationCredentialsReuse
}


- (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
{
Expand Down

0 comments on commit fc29d80

Please sign in to comment.